mybatis 接口綁定 和 動態SQL


一、MyBatis 接口綁定方案及多參數傳遞

 

1、作用:實現創建一個接口后把mapper.xml由mybatis生成接口的實現類,通過調用接口對象就可以獲取mapper.xml中編寫的sql

2、后面:mybatis和spring整合時使用的是這個方案

3、實現步驟:

  3.1 創建一個接口

    3.1.1 接口包名和接口名與xxxmapper.xml 中<mappers> namespace相同 

    3.1.2 接口中的方法名和xxxmapper.xml中的id名相同

   3.2 在mybatis.xml 中使用<package>  進行掃描接口和xxxmapper.xml

4、代碼實現步驟:

  4.1 在mybatis.xml中<mappers>下使用<package>

1     <mappers>
2         <package name="com.bjsxt.mapper"/>
3     </mappers>

  4.2 在com.bjsxt.mapper包下新建接口

1 public interface PeopleMapper {
2         查詢所有信息        
3     List<People> selall ();
4         查詢帶參數的                    這是使用注解
5      List<People> selcount(int age);       List<People> zhujie(@Param("ace")int id)
6 }

   4.3 在com.bjsxt.mapper包下新建xml

      注意:namespace = 必須和接口全限定路徑(包名+類名)一致

         id值 必須和接口中方法名相同

           如果接口中方法為多個參數,可以省略parameterType

1  <mapper namespace="com.bjsxt.mapper.PeopleMapper">
2      <select id="selall" resultType="People">
3          select * from people
4      </select>
5      <select id="selcount" resultType="People">
6          select * from people where age=#{0}
7      </select>
      這是使用注解                     使用注解原理是 mybatis把參數轉換為map,其中@Param("key")
                                        參數內容就是map的value
    <select id="zhujie" resultType="People">
       select * from people where id=#{ace}            id=#{} 里面的內容就是上面中@Param("ace")
    </select> 8 </mapper>

  

   實現方式:         接口綁定 的實現原理是  使用proxy代理類(作用是實現接口中的方法把通過代理類實現   反射實現)

1         PeopleMapper mapper = session.getMapper(PeopleMapper.class);
2         List<People> pel = mapper.selcount(25);
3         for(People li:pel){
4             System.out.println(li);
5         }

 

二、動態SQL

 

  1、根據不同的條件需要執行不同的SQL語句,稱之為 動態SQL

  2、MyBatis中動態SQL在mapper.xml中添加邏輯判斷等

  3、<if> 使用 

 1     <select id="selcount" resultType="People">
 2         select * from people where 1=1
 3         <!-- OGNL表達式  直接寫Key或對象屬性,不需要添加任何特定符號-->
 4         <if test="id!=null and id!=''">
 5             and id=#{id}
 6         </if>
 7         <if test="age!=null and age!=''">
 8             and age=#{age}
 9         </if>
10     </select>

  4、<where> 使用

    4.1 當編寫where 標簽時,如果內容中第一個是and去掉第一個 and

    4.2 如果<where> 中有內容會生成where關鍵字,如果沒有內容不生成where關鍵

    4.3 使用實例   使用where其實比if 少寫了where關鍵字,使用where比if效率更高,因為 where中少了一個條件判斷

 1     <select id="selcount" resultType="People">
 2         select * from people
 3         <where>
 4         <if test="id!=null and id!=''">
 5             and id=#{id}
 6         </if>
 7         <if test="age!=null and age!=''">
 8             and age=#{age}
 9         </if>
10         </where>
11     </select>

  5、<choose><when><otherwise>

    5.1 只有有一個成立,其他都不執行

    5.2 代碼實例

      5.2.1  如果 id 和 age 都不是null 或不是 " " 生成的sql語句中只有where id=?

 1 <select id="selcount" resultType="People">
 2         select * from people
 3     <where>
 4         <choose>
 5             <when test="id!=null and id!=''">
 6                     and id=#{id}
 7             </when>
 8             <when test="age!=null and age!=''">
 9                     and age=#{age}
10             </when>
11         </choose>
12     </where>
13 </select>

  6、<set> 用在修改SQL中 set 從句

    6.1 作用:去掉最后一個 逗號

    6.2 作用:如果<set> 里面有內容就會生成set關鍵字,沒有就不生成

    6.3 實例:

 1     <update id="upd" parameterType="People">
 2         update people 
 3         <set>
 4             id=#{id},   目的防止<set>中沒有內容,mybatis不生成set關鍵字,如果修改中沒有set從句SQL語法錯誤 5             <if test="name!=null and name!=''">
 6                 name=#{name},
 7             </if>
 8             <if test="age!=null and age!=''">
 9                 age=#{age},
10             </if>
11         </set>
12         where id=#{id}
13     </update>
14         

  7、<Trim>

    7.1 prefix 在前面添加內容

    7.2 prefixOverrides 去掉前面內容

    7.3 suffix 在后面添加內容

    7.4 suffixOverrides 去掉后面內容

    7.5 執行順序先去掉內容后添加內容

   代碼實例:

1         <update id="upd" parameterType="People">
2             update people
3             <trim prefix="set" suffixOverrides=",">
4                 name=#{name},
5             </trim>
6             where id=#{id}
7         </update>

  8 <bind>

    8.1 作用:給參數重新賦值

    8.2 場景:

      8.2.1 模糊查詢

      8.2.2 在原內容前或后添加內容

    8.3 代碼實例:  

1 <select id="selall" parameterType="People" resultType="People"
2         <bind name="name" value="'%'+name+'%'" />
3         #{name}
4 </select>

     9、<foreach>標簽

    9.1 循環參數內容,還具備在內容的前后添加內容,還具備分隔符功能

    9.2 使用場景:in查詢中,批量新增中(mybatis中foreach效率比較低  很低)

      9.2.1 如果希望批量新增SQL命令

1  insert into people values(default,'xu',15),(default,'ji',45),(default,'we',12)

      9.2.2 openSession() 必須指定

        底層實現是 JDBC的 PreparedStatement.addBatch();

1 SqlSession session=sql.openSession(ExecutorType.BATCH);

    代碼實例:collection=" " 要遍歷的集合

         item=" "  迭代變量,#{迭代變量名} 獲取內容

            open 循環后左側添加的內容

         close 循環后右側添加的內容

         separator 每次循環時,元素之間的分隔符

1     <select id="selIn" parameterType="list" resultType="People">
2             select * from people where id in 
3             <foreach collection="list" item="li" open="(" close=")" separator=",">
4                 #{li}
5             </foreach>
6     </select>

  10  <sql>和<include>

    10.1 某些SQL片段如果希望復用,可以使用<sql>

    <sql id="people">
        name,age
    </sql>
    

    10.2 在<select> 或<delete> 或<update> 或<insert> 中使用<include>引用

    <select id="seltwotable">
        select <include refid="people"></include>
        for people
    </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM