1. 傳入簡單類型java
Java代碼:apache
- 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代碼:less
- public List<Area> findUserListByIdList(List<Long> idList) {
- return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);
- }
MAPPER :ui
- <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>
單獨傳入list時,foreach中的collection必須是list,不無論變量的具體名稱是什麼。好比這裏變量名爲idList,
collection倒是是list。
3. 傳入數組spa
JAVA代碼:.net
- public List<Area> findUserListByIdList(int[] ids) {
- return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);
- }
MAPPER :code
- <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>
單獨傳入數組時,foreach中的collection必須是array,不無論變量的具體名稱是什麼。好比這裏變量名爲ids,
collection倒是是arrayxml
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中有list或array時,foreach中的collection必須是具體list或array的變量名。好比這裏MAP含有一個
名爲idList的list,因此MAP中用idList取值,這點和單獨傳list或array時不太同樣。
5. 傳入JAVA對象
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對象中有list或array時,foreach中的collection必須是具體list或array的變量名。好比這裏UserDTO含有一個
名爲idList的list,因此UserDTO中用idList取值,這點和單獨傳list或array時不太同樣。
6.取值
由上面能夠看出,取值的時候用的是#{}。它具體的意思是告訴MyBatis建立一個預處理語句參數。
使用JDBC,這樣的一個參數在SQL中會由一個「?」來標識,並被傳遞到一個新的預處理語句中,就像這樣:
- String selectPerson = 「SELECT * FROM PERSON WHERE ID=?」;
- PreparedStatement ps = conn.prepareStatement(selectPerson);
- ps.setInt(1,id);
能夠看到這個寫法比較簡單,MyBatis爲咱們作了不少默認的事情,具體的寫法應該以下:
- #{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler,mode=OUT,resultMap=User}
property:屬性名,即代碼傳入的變量名。
javaType:該字段在JAVA中的類型,好比int。
jdbcType:該字段在JDBC中的類型,好比NUMERIC。
typeHandler:類型處理器
mode:參數類型爲IN,OUT或INOUT參數
resultMap:結果。
還好,MyBatis比較體諒咱們,通常咱們只需寫一個屬性名便可,如#{id},其餘的如javaType和typeHandlerMybatis
會自動幫咱們填好。但是這樣有時也會出問題,好比出現CLOB字段時。
因爲JAVA代碼中的String類型對應的默認typeHandler爲StringTypeHandler,當用String類型處理時,若是String長度超過必定長度,就會報以下錯誤:
setString can only process strings of less than 32766 chararacters
解決辦法是指定該屬性的typeHandler,以下:
#{message,typeHandler=org.apache.ibatis.type.ClobTypeHandler}
咱們也能夠自定義typeHandler來處理須要的數據,具體這裏詳述。
JDBC類型是僅僅須要對插入,更新和刪除操做可能爲空的列進行處理。這是JDBC的須要,而不是MyBatis的。通常不須要配置
mode、resultMap通常不須要,在寫存儲過程時會用到,這裏不詳述。
7.字符串替換
通常狀況下,咱們採用#{}取值,產生預處理語句,可是有時咱們可能不但願Mybatis來幫咱們預處理,好比ORDER BY時,能夠 採用以下寫法: ORDER BY ${columnName} 這裏MyBatis不會修改或轉義字符串。而是直接拼接到SQL字符串後面。 重要:接受從用戶輸出的內容並提供給語句中不變的字符串,這樣作是不安全的。這會致使潛在的SQL注入攻擊,所以你 不該該容許用戶輸入這些字段,或者一般自行轉義並檢查。