閲嶆柊瀛︿範Mybatis(浜?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了閲嶆柊瀛︿範Mybatis(浜?相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/rac' title='rac'>rac   鏄犲皠   val   fir   鎿嶄綔   entryset   娉ㄨВ   ring   ids   

涓€銆丮ybatis鍙傛暟澶勭悊

鍗曚釜鍙傛暟锛歮ybatis涓嶄細鍋氱壒娈婂鐞嗭紝
#{鍙傛暟鍚?浠绘剰鍚峿锛氬彇鍑哄弬鏁板€笺€?br />
澶氫釜鍙傛暟锛歮ybatis浼氬仛鐗规畩澶勭悊銆?br /> 澶氫釜鍙傛暟浼氳灏佽鎴?涓€涓猰ap锛?br /> key锛歱aram1...paramN,鎴栬€呭弬鏁扮殑绱㈠紩涔熷彲浠?br /> value锛氫紶鍏ョ殑鍙傛暟鍊?br /> #{}灏辨槸浠巑ap涓幏鍙栨寚瀹氱殑key鐨勫€硷紱

寮傚父锛?br /> org.apache.ibatis.binding.BindingException:
Parameter 鈥榠d鈥?not found.
Available parameters are [1, 0, param1, param2]
鎿嶄綔锛?br /> 鏂规硶锛歱ublic Employee getEmpByIdAndLastName(Integer id,String lastName);
鍙栧€硷細#{id},#{lastName}

鎹㈡垚#{param1}鍜?{param2}灏卞彲浠ヨВ鍐筹紝褰撴椂鏇存帹鑽愬涓嬫搷浣滐細

Employee getEmployeeByIdAndName(@Param("id")Integer id, @Param("lastName")String lastName);

銆愬懡鍚嶅弬鏁般€戯細鏄庣‘鎸囧畾灏佽鍙傛暟鏃秏ap鐨刱ey锛汙Param("id")
澶氫釜鍙傛暟浼氳灏佽鎴?涓€涓猰ap锛?br /> key锛氫娇鐢ˊParam娉ㄨВ鎸囧畾鐨勫€?br /> value锛氬弬鏁板€?br />#{鎸囧畾鐨刱ey}鍙栧嚭瀵瑰簲鐨勫弬鏁板€?/p>

 

POJO锛?br />濡傛灉澶氫釜鍙傛暟姝eソ鏄笟鍔¢€昏緫鐨勬暟鎹ā鍨嬶紝灏卞彲浠ョ洿鎺ヤ紶鍏ojo锛?br /> #{灞炴€у悕}锛氬彇鍑轰紶鍏ョ殑pojo鐨勫睘鎬у€?/p>

Employee emp = mapper.getEmployeeByIdAndName(1, "tom");
    <select id="getEmployeeByIdAndName" resultType="com.it.mybatis.bean.Employee">
        select id,last_name,gender,email from tbl_employee where id = #{id} and last_name = #{lastName}
    </select>

Map锛?br />濡傛灉澶氫釜鍙傛暟涓嶆槸涓氬姟妯″瀷涓殑鏁版嵁锛屾病鏈夊搴旂殑pojo锛屼笉缁忓父浣跨敤锛屼负浜嗘柟渚匡紝鎴戜滑涔熷彲浠ヤ紶鍏ap
#{key}锛氬彇鍑簃ap涓搴旂殑鍊?/p>

            Map<String, Object> map = new HashMap<>();
            map.put("id", 2);
            map.put("lastName", "tom");
            Employee emp = mapper.getEmployeeByMap(map);
    <!-- Employee getEmployeeByMap(Map<String,Object> map); -->
    <select id="getEmployeeByMap" resultType="com.it.mybatis.bean.Employee">
        select id,last_name,gender,email from tbl_employee where id = ${id} and last_name = #{lastName}
    </select>

TO锛?br />濡傛灉澶氫釜鍙傛暟涓嶆槸涓氬姟妯″瀷涓殑鏁版嵁锛屼絾鏄粡甯歌浣跨敤锛屾帹鑽愭潵缂栧啓涓€涓猅O锛圱ransfer Object锛夋暟鎹紶杈撳璞?br />Page{
int index;
int size;
}

灏忕粨锛?/p>

public Employee getEmp(@Param("id")Integer id,String lastName);
鍙栧€硷細id==>#{id/param1} lastName==>#{param2}

public Employee getEmp(Integer id,@Param("e")Employee emp);
鍙栧€硷細id==>#{param1} lastName===>#{param2.lastName/e.lastName}

##鐗瑰埆娉ㄦ剰锛氬鏋滄槸Collection锛圠ist銆丼et锛夌被鍨嬫垨鑰呮槸鏁扮粍锛?br /> 涔熶細鐗规畩澶勭悊銆備篃鏄妸浼犲叆鐨刲ist鎴栬€呮暟缁勫皝瑁呭湪map涓€?br /> key锛欳ollection锛坈ollection锛?濡傛灉鏄疞ist杩樺彲浠ヤ娇鐢ㄨ繖涓猭ey(list)
鏁扮粍(array)
public Employee getEmpById(List<Integer> ids);
鍙栧€硷細鍙栧嚭绗竴涓猧d鐨勫€硷細 #{list[0]}

 

====缁撳悎婧愮爜锛宮ybatis澶勭悊鍙傛暟

鎬荤粨锛氬弬鏁板鏃朵細灏佽map锛屼负浜嗕笉娣蜂贡锛屾垜浠彲浠ヤ娇鐢ˊParam鏉ユ寚瀹氬皝瑁呮椂浣跨敤鐨刱ey锛?br />#{key}灏卞彲浠ュ彇鍑簃ap涓殑鍊硷紱

(@Param("id")Integer id,@Param("lastName")String lastName);
ParamNameResolver瑙f瀽鍙傛暟灏佽map鐨勶紱
//1銆乶ames锛歿0=id, 1=lastName}锛涙瀯閫犲櫒鐨勬椂鍊欏氨纭畾濂戒簡

纭畾娴佺▼锛?br /> 1.鑾峰彇姣忎釜鏍囦簡param娉ㄨВ鐨勫弬鏁扮殑@Param鐨勫€硷細id锛宭astName锛? 璧嬪€肩粰name;
2.姣忔瑙f瀽涓€涓弬鏁扮粰map涓繚瀛樹俊鎭細锛坘ey锛氬弬鏁扮储寮曪紝value锛歯ame鐨勫€硷級
name鐨勫€硷細
鏍囨敞浜唒aram娉ㄨВ锛氭敞瑙g殑鍊?br /> 娌℃湁鏍囨敞锛?br /> 1.鍏ㄥ眬閰嶇疆锛歶seActualParamName锛坖dk1.8锛夛細name=鍙傛暟鍚?br /> 2.name=map.size()锛涚浉褰撲簬褰撳墠鍏冪礌鐨勭储寮?br /> {0=id, 1=lastName,2=2}

public Object getNamedParams(Object[] args) {
    final int paramCount = names.size();
    //1銆佸弬鏁颁负null鐩存帴杩斿洖
    if (args == null || paramCount == 0) {
      return null;
     
    //2銆佸鏋滃彧鏈変竴涓厓绱狅紝骞朵笖娌℃湁Param娉ㄨВ锛沘rgs[0]锛氬崟涓弬鏁扮洿鎺ヨ繑鍥?/span>
    } else if (!hasParamAnnotation && paramCount == 1) {
      return args[names.firstKey()];
      
    //3銆佸涓厓绱犳垨鑰呮湁Param鏍囨敞
    } else {
      final Map<String, Object> param = new ParamMap<Object>();
      int i = 0;
      
      //4銆侀亶鍘唍ames闆嗗悎锛泏0=id, 1=lastName,2=2}
      for (Map.Entry<Integer, String> entry : names.entrySet()) {
      
          //names闆嗗悎鐨剉alue浣滀负key;  names闆嗗悎鐨刱ey鍙堜綔涓哄彇鍊肩殑鍙傝€僡rgs[0]:args銆?锛?Tom"銆?
          //eg:{id=args[0]:1,lastName=args[1]:Tom,2=args[2]}
        param.put(entry.getValue(), args[entry.getKey()]);
        
        
        // add generic param names (param1, param2, ...)param
        //棰濆鐨勫皢姣忎竴涓弬鏁颁篃淇濆瓨鍒癿ap涓紝浣跨敤鏂扮殑key锛歱aram1...paramN
        //鏁堟灉锛氭湁Param娉ㄨВ鍙互#{鎸囧畾鐨刱ey}锛屾垨鑰?{param1}
        final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
        // ensure not to overwrite parameter named with @Param
        if (!names.containsValue(genericParamName)) {
          param.put(genericParamName, args[entry.getKey()]);
        }
        i++;
      }
      return param;
    }
  }
}

鍙傛暟鍊肩殑鑾峰彇锛?/p>

#{}锛氬彲浠ヨ幏鍙杕ap涓殑鍊兼垨鑰卲ojo瀵硅薄灞炴€х殑鍊硷紱
${}锛氬彲浠ヨ幏鍙杕ap涓殑鍊兼垨鑰卲ojo瀵硅薄灞炴€х殑鍊硷紱


select * from tbl_employee where id=${id} and last_name=#{lastName}
Preparing: select * from tbl_employee where id=2 and last_name=?
鍖哄埆锛?br /> #{}:鏄互棰勭紪璇戠殑褰㈠紡锛屽皢鍙傛暟璁剧疆鍒皊ql璇彞涓紱PreparedStatement锛涢槻姝ql娉ㄥ叆
${}:鍙栧嚭鐨勫€肩洿鎺ユ嫾瑁呭湪sql璇彞涓紱浼氭湁瀹夊叏闂锛?br /> 澶у鎯呭喌涓嬶紝鎴戜滑鍘诲弬鏁扮殑鍊奸兘搴旇鍘讳娇鐢?{}锛?br />
鍘熺敓jdbc涓嶆敮鎸佸崰浣嶇鐨勫湴鏂规垜浠氨鍙互浣跨敤${}杩涜鍙栧€?br /> 姣斿鍒嗚〃銆佹帓搴忋€傘€傘€傦紱鎸夌収骞翠唤鍒嗚〃鎷嗗垎
select * from ${year}_salary where xxx;
select * from tbl_employee order by ${f_name} ${order}

#{}:鏇翠赴瀵岀殑鐢ㄦ硶锛?br /> 瑙勫畾鍙傛暟鐨勪竴浜涜鍒欙細
javaType銆?jdbcType銆?mode锛堝瓨鍌ㄨ繃绋嬶級銆?numericScale銆?br /> resultMap銆?typeHandler銆?jdbcTypeName銆?expression锛堟湭鏉ュ噯澶囨敮鎸佺殑鍔熻兘锛夛紱

jdbcType閫氬父闇€瑕佸湪鏌愮鐗瑰畾鐨勬潯浠朵笅琚缃細
鍦ㄦ垜浠暟鎹负null鐨勬椂鍊欙紝鏈変簺鏁版嵁搴撳彲鑳戒笉鑳借瘑鍒玬ybatis瀵筺ull鐨勯粯璁ゅ鐞嗐€傛瘮濡侽racle锛堟姤閿欙級锛?br />
JdbcType OTHER锛氭棤鏁堢殑绫诲瀷锛涘洜涓簃ybatis瀵规墍鏈夌殑null閮芥槧灏勭殑鏄師鐢烰dbc鐨凮THER绫诲瀷锛宱racle涓嶈兘姝g‘澶勭悊;

鐢变簬鍏ㄥ眬閰嶇疆涓細jdbcTypeForNull=OTHER锛沷racle涓嶆敮鎸侊紱涓ょ鍔炴硶
1銆?{email,jdbcType=OTHER};
2銆乯dbcTypeForNull=NULL
<setting name="jdbcTypeForNull" value="NULL"/>

 

以上是关于閲嶆柊瀛︿範Mybatis(浜?的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis妗嗘灦 鍏ラ棬瀛︿範

macOS閿洏鏄犲皠

鍚浣犳兂鍐欎釜 React - virtual dom

vista x64 vs2010 win32娣诲姞璧勬簮 鏈兘瀹屾垚鎿嶄綔瑙e喅鍔炴硶