MyBatis动态SQL

动态SQL

在实际的编程时,很多传递进来的值都是需要多条件查询的,但是不同情况下进行的多条件是不同的,所以就需要动态SQL

MyBatis中动态SQL是在mapper.xml中添加逻辑判断

在这里插入图片描述
MyBaris官方文档做出的解释

if

 List<User> selectByNameAndPassword(@Param("username") String username, @Param("password") String password);
  <select id="selectByNameAndPassword" resultType="User">
        select * from user where 1=1
        <if test="username!=null and username!=''">
          and username=#{username}
        </if>
        <if test="password!=null and password!=''">
          and password=#{password}
        </if>
    </select>

在前面添加where 1=1 是因为动态连接条件时
如果在第一个If中添加where,当这个if不满足条件时,而第二个if成立时就会出现没有where的情况
其他情况类似

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> list=userMapper.selectByNameAndPassword(null,"123");
        System.out.println(list);

where

当编写where标签时,就可以不使用where 1=1的情况

   <select id="selectByNameAndPassword" resultType="User">
        select * from user 
        <where>
            <if test="username!=null and username!=''">
              and username=#{username}
            </if>
            <if test="password!=null and password!=''">
              and password=#{password}
            </if>
        </where>
    </select>

当编写where标签时,需要在每个if标签中添加and.
其原理是基于<trim>标签,如果内容第一个是and,则自动去掉第一个and

如果where标签中有内容则会生成where关键字,如果没有内容不会生产where关键字

choose

choose标签是只有一个成立,其他都不执行

  <select id="selectByNameAndPassword" resultType="User">
        select * from user
        <where>
            <choose>
                <when test="username!=null and username!=''">
                    and username=#{username}
                </when>
                <when test="password!=null and password!=''">
                    and password=#{password}
                </when>
            </choose>
        </where>
    </select>
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        List<User> list=userMapper.selectByNameAndPassword("reverie","123456");
        System.out.println(list);

在这里插入图片描述
可以看出只生成了一个where条件,虽然在调用时,传递了两个参数。
choose在有一个条件成立时,其他都不执行

当然在choose中也包含<otherwise>

set

where标签是去掉第一个and,而set则是去掉最后一个逗号
如果<set>里面有内容生成 set 关键字,没有就不生成set关键字

 int insert(Contracts contracts);

在接口中声明insert方法

   <update id="update" parameterType="Contracts">
        update contracts
        <set>
            cid=#{cid},
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="sex!=null and sex!=''">
                sex=#{sex},
            </if>
            <if test="age!=null and age!=''">
                age=#{age},
            </if>
        </set>
        where cid=#{cid}
    </update>

这里将第一个条件每天添加if标签,是因为第一个条件也添加If的话,当所有If都不成立的话,update语句就会出现错误
在这里插入图片描述
没有set,而去update就会报错

      UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        Contracts contracts=new Contracts();
        contracts.setCid("reverie001");
        contracts.setName("张三");
        contracts.setSex("男");
        contracts.setAge(20);
        contracts.setPhone("10086");
        int index=userMapper.update(contracts);
        sqlSession.commit();
        if(index>0)
            System.out.println("成功");
        else
            System.out.println("失败");

Trim

where和set都是基于trim的
where是在前面添加where关键字,并去掉前面的第一个and
set是在前面添加set关键字,并去掉最后面的逗号

trim的属性
在这里插入图片描述
prefix 在前面添加内容
prefixOverrides 去掉前面内容
suffix 在后面添加内容
suffixOverrieds 去掉后面内容

trim的执行顺序,先去掉内容后添加内容

 <update id="update" parameterType="Contracts">
        update contracts
        <trim prefix="set" suffixOverrides=",">
            cid=#{cid},
            <if test="name!=null and name!=''">
                name=#{name},
            </if>
            <if test="sex!=null and sex!=''">
                sex=#{sex},
            </if>
            <if test="age!=null and age!=''">
                age=#{age},
            </if>
        </trim>
        where cid=#{cid}
    </update>

bind

bind标签的作用是给参数重新赋值

应用于模糊查询和在原内容的前或后添加内容

<select id="selectLike" resultType="Contracts" parameterType="Contracts">
        <bind name="cid" value="'%'+cid+'%'"/>
        select * from contracts where cid like #{cid}
</select>

在这里插入图片描述

foreach

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

在in查询和批量新增的场景中比较适用,但MyBatis的foreach效率比较低

collection=”” 要遍历的集合
item 迭代变量, #{迭代变量名}获取内容
open 循环后左侧添加的内容
close 循环后右侧添加的内容
separator 每次循环时,元素之间的分隔符

<select id="selIn" parameterType="list" resultType="log"> 
   select * from contracts where id in 
   <foreach collection="list" item="u" open="(" close=")" separator=","> 
       #{u} 
   </foreach> 
 </select>
        List<String> in=new ArrayList<>();
        in.add("reverie001");
        in.add("reverie002");
        in.add("zhangsan001");
        List<Contracts> list=userMapper.selectIn(in);
        System.out.println(list);

在这里插入图片描述

sql和include

某些 SQL 片段如果希望复用,可以使用定义这个片段

    <sql id="mysql">
        cid,name,sex,age
    </sql>
    
     <select id="selAllContracts" resultType="Contracts">
        select <include refid="mysql"/>
        from contracts
    </select>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值