1. 傳入簡(jiǎn)單類型
Java代碼:
- public User get(Long id) {
- return (User) getSqlSession().selectOne("com.liulanghan.get" , id);
- }
MAPPER :
- <select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">
- select * from user where id = #{id};
- </select>
2. 傳入List
JAVA代碼:
- public List<Area> findUserListByIdList(List<Long> idList) {
- return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);
- }
MAPPER :
- <select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">
- select * from user user
- <where>
- user.ID in (
- <foreach item="guard" index="index" collection="list"
- separator=","> #{guard} </foreach>
- )
- </where>
- </select>
單獨(dú)傳入list時(shí),foreach中的collection必須是list,不不管變量的具體名稱是什么。比如這里變量名為idList, collection卻是是list。 3. 傳入數(shù)組
JAVA代碼:
- public List<Area> findUserListByIdList(int[] ids) {
- return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);
- }
MAPPER :
- <select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">
- select * from user user
- <where>
- user.ID in (
- <foreach item="guard" index="index" collection="array"
- separator=","> #{guard} </foreach>
- )
- </where>
- </select>
單獨(dú)傳入數(shù)組時(shí),foreach中的collection必須是array,不不管變量的具體名稱是什么。比如這里變量名為ids, collection卻是是array
4. 傳入map JAVA代碼:
- public boolean exists(Map<String, Object> map){
- Object count = getSqlSession().selectOne("com.liulanghan.exists", map);
- int totalCount = Integer.parseInt(count.toString());
- return totalCount > 0 ? true : false;
- }
MAPPER :
- <select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">
- SELECT COUNT(*) FROM USER user
- <where>
- <if test="code != null">
- and user.CODE = #{code}
- </if>
- <if test="id != null">
- and user.ID = #{id}
- </if>
- <if test="idList !=null ">
- and user.ID in (
- <foreach item="guard" index="index" collection="idList"
- separator=","> #{guard} </foreach>
- )
- </if>
- </where>
- </select>
MAP中有l(wèi)ist或array時(shí),foreach中的collection必須是具體list或array的變量名。比如這里MAP含有一個(gè) 名為idList的list,所以MAP中用idList取值,這點(diǎn)和單獨(dú)傳list或array時(shí)不太一樣。 5. 傳入JAVA對(duì)象 JAVA代碼:
- public boolean findUserListByDTO(UserDTO userDTO){
- Object count = getSqlSession().selectOne("com.liulanghan.exists", userDTO);
- int totalCount = Integer.parseInt(count.toString());
- return totalCount > 0 ? true : false;
- }
MAPPER :
- <select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer">
- SELECT COUNT(*) FROM USER user
- <where>
- <if test="code != null">
- and user.CODE = #{code}
- </if>
- <if test="id != null">
- and user.ID = #{id}
- </if>
- <if test="idList !=null ">
- and user.ID in (
- <foreach item="guard" index="index" collection="idList"
- separator=","> #{guard} </foreach>
- )
- </if>
- </where>
- </select>
JAVA對(duì)象中有l(wèi)ist或array時(shí),foreach中的collection必須是具體list或array的變量名。比如這里UserDTO含有一個(gè) 名為idList的list,所以UserDTO中用idList取值,這點(diǎn)和單獨(dú)傳list或array時(shí)不太一樣。
6.取值
由上面可以看出,取值的時(shí)候用的是#{}。它具體的意思是告訴MyBatis創(chuàng)建一個(gè)預(yù)處理語(yǔ)句參數(shù)。 使用JDBC,這樣的一個(gè)參數(shù)在SQL中會(huì)由一個(gè)“?”來(lái)標(biāo)識(shí),并被傳遞到一個(gè)新的預(yù)處理語(yǔ)句中,就像這樣:
- // Similar JDBC code, NOT MyBatis…
- String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;
- PreparedStatement ps = conn.prepareStatement(selectPerson);
- ps.setInt(1,id);
可以看到這個(gè)寫法比較簡(jiǎn)單,MyBatis為我們做了很多默認(rèn)的事情,具體的寫法應(yīng)該如下:
- #{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler,mode=OUT,resultMap=User}
property:屬性名,即代碼傳入的變量名。 javaType:該字段在JAVA中的類型,比如int。 jdbcType:該字段在JDBC中的類型,比如NUMERIC。 typeHandler:類型處理器 mode:參數(shù)類型為IN,OUT或INOUT參數(shù) resultMap:結(jié)果。 還好,MyBatis比較體諒我們,一般我們只需寫一個(gè)屬性名即可,如#{id},其他的如javaType和typeHandlerMybatis 會(huì)自動(dòng)幫我們填好。可是這樣有時(shí)也會(huì)出問題,比如出現(xiàn)CLOB字段時(shí)。 由于JAVA代碼中的String類型對(duì)應(yīng)的默認(rèn)typeHandler為StringTypeHandler,當(dāng)用String類型處理時(shí),如果String長(zhǎng)度超過一定長(zhǎng)度,就會(huì)報(bào)如下錯(cuò)誤:
setString can only process strings of less than 32766 chararacters
解決辦法是指定該屬性的typeHandler,如下:
#{message,typeHandler=org.apache.ibatis.type.ClobTypeHandler}
我們也可以自定義typeHandler來(lái)處理需要的數(shù)據(jù),具體這里詳述。 JDBC類型是僅僅需要對(duì)插入,更新和刪除操作可能為空的列進(jìn)行處理。這是JDBC的需要,而不是MyBatis的。一般不需要配置 mode、resultMap一般不需要,在寫存儲(chǔ)過程時(shí)會(huì)用到,這里不詳述。 7.字符串替換
一般情況下,我們采用#{}取值,產(chǎn)生預(yù)處理語(yǔ)句,但是有時(shí)我們可能不希望Mybatis來(lái)幫我們預(yù)處理,比如ORDER BY時(shí),可以 采用如下寫法: ORDER BY ${columnName} 這里MyBatis不會(huì)修改或轉(zhuǎn)義字符串。而是直接拼接到SQL字符串后面。 重要:接受從用戶輸出的內(nèi)容并提供給語(yǔ)句中不變的字符串,這樣做是不安全的。這會(huì)導(dǎo)致潛在的SQL注入攻擊,因此你 不應(yīng)該允許用戶輸入這些字段,或者通常自行轉(zhuǎn)義并檢查。
|