mybatis 接口绑定 和 动态SQL

时间:2023-03-09 17:55:21
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>

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

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

 public interface PeopleMapper {
查询所有信息
List<People> selall ();
查询带参数的             这是使用注解
List<People> selcount(int age); List<People> zhujie(@Param("ace")int id)
}

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

      注意:namespace = 必须和接口全限定路径(包名+类名)一致

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

           如果接口中方法为多个参数,可以省略parameterType

  <mapper namespace="com.bjsxt.mapper.PeopleMapper">
<select id="selall" resultType="People">
select * from people
</select>
<select id="selcount" resultType="People">
select * from people where age=#{0}
</select>
      这是使用注解                     使用注解原理是 mybatis把参数转换为map,其中@Param("key")
                                        参数内容就是map的value
    <select id="zhujie" resultType="People">
       select * from people where id=#{ace}            id=#{} 里面的内容就是上面中@Param("ace")
    </select>
</mapper>

  

   实现方式:         接口绑定 的实现原理是  使用proxy代理类(作用是实现接口中的方法把通过代理类实现   反射实现)

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

二、动态SQL

  1、根据不同的条件需要执行不同的SQL语句,称之为 动态SQL

  2、MyBatis中动态SQL在mapper.xml中添加逻辑判断等

  3、<if> 使用

     <select id="selcount" resultType="People">
select * from people where 1=1
<!-- OGNL表达式 直接写Key或对象属性,不需要添加任何特定符号-->
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
</select>

  4、<where> 使用

    4.1 当编写where 标签时,如果内容中第一个是and去掉第一个 and

    4.2 如果<where> 中有内容会生成where关键字,如果没有内容不生成where关键

    4.3 使用实例   使用where其实比if 少写了where关键字,使用where比if效率更高,因为 where中少了一个条件判断

     <select id="selcount" resultType="People">
select * from people
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="age!=null and age!=''">
and age=#{age}
</if>
</where>
</select>

  5、<choose><when><otherwise>

    5.1 只有有一个成立,其他都不执行

    5.2 代码实例

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

 <select id="selcount" resultType="People">
select * from people
<where>
<choose>
<when test="id!=null and id!=''">
and id=#{id}
</when>
<when test="age!=null and age!=''">
and age=#{age}
</when>
</choose>
</where>
</select>

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

    6.1 作用:去掉最后一个 逗号

    6.2 作用:如果<set> 里面有内容就会生成set关键字,没有就不生成

    6.3 实例:

     <update id="upd" parameterType="People">
update people
<set>
id=#{id},   目的防止<set>中没有内容,mybatis不生成set关键字,如果修改中没有set从句SQL语法错误 <if test="name!=null and name!=''">
name=#{name},
</if>
<if test="age!=null and age!=''">
age=#{age},
</if>
</set>
where id=#{id}
</update>

  7、<Trim>

    7.1 prefix 在前面添加内容

    7.2 prefixOverrides 去掉前面内容

    7.3 suffix 在后面添加内容

    7.4 suffixOverrides 去掉后面内容

    7.5 执行顺序先去掉内容后添加内容

   代码实例:

         <update id="upd" parameterType="People">
update people
<trim prefix="set" suffixOverrides=",">
name=#{name},
</trim>
where id=#{id}
</update>

  8 <bind>

    8.1 作用:给参数重新赋值

    8.2 场景:

      8.2.1 模糊查询

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

    8.3 代码实例:  

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

    9、<foreach>标签

    9.1 循环参数内容,还具备在内容的前后添加内容,还具备分隔符功能

    9.2 使用场景:in查询中,批量新增中(mybatis中foreach效率比较低  很低)

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

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

      9.2.2 openSession() 必须指定

        底层实现是 JDBC的 PreparedStatement.addBatch();

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

    代码实例:collection=" " 要遍历的集合

         item=" "  迭代变量,#{迭代变量名} 获取内容

            open 循环后左侧添加的内容

         close 循环后右侧添加的内容

         separator 每次循环时,元素之间的分隔符

     <select id="selIn" parameterType="list" resultType="People">
select * from people where id in
<foreach collection="list" item="li" open="(" close=")" separator=",">
#{li}
</foreach>
</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>