閲嶆柊瀛︿範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"/>