mybatis动态sql

mybatis动态查询

#+实现动态的多条件查询
1、标签
接口中的方法,有两个参数

public List<User> selectnyannot(@Param("userName") String username,@Param("userPassword") String password);

映射文件中的SQL语句

<select id="selectnyannot" resultType="com.wj.dao.pojo.User">
<!--
使用if标签,test表示满足条件后才进行语句拼接,test=true
注意 where 1=1这个条件,是为了防止第一个参数非空时拼接语句时防止and 报错
where 1=1 and 。。。。。
否则就成了
where and 。。。
-->
select * from tb_user where 1=1 
        <if test="userName != null">
        and userName like concat('%',#{userName},'%')  
        </if>
        <if test="userPassword">
        and userPassword = #{userPassword}
        </if>
</select>
	```
2、<where>标签
自动识别标签中是否有返回值,若有就会自动插入where ,若以and 或 or开头会自动剔除,解决了if标签的问题

```xml
<select id="selectnyannot" resultType="com.wj.dao.pojo.User">
select * from tb_user 
		<!--where标签中使用if标签不用 1=1的条件也不会报错-->
       <where>
               <if test="userName != null">
                       and userName like concat('%',#{userName},'%')
               </if>
               <if test="userPassword">
                       and userPassword = #{userPassword}
               </if>
       </where>
</select>

#+标签实现多条件查询
trim标签判断标签中内容是否有返回值,若有则会为语句添加前缀或后缀,并覆盖指定内容,代替了where标签,并且更加灵活
trim属性
prefix 前缀
suffix 后缀
prefixOvereides 覆盖指定前缀(首部的)
suffixOverrides 覆盖指定后缀(尾部的)
改造上述查询语句

<select id="selectnyannot" resultType="com.wj.dao.pojo.User">
select * from tb_user
       <trim prefix="where" prefixOverrides="and | or"> <!--  添加where 覆盖首部and-->
               <if test="userName != null">
                       and userName like concat('%',#{userName},'%')
               </if>
               <if test="userPassword">
                       and userPassword = #{userPassword}
               </if>
       </trim>
</select>
</mapper>

#+实现多条件选择
+ 类似于java的
switch + case + default
要注意的是 条件不在choose中,而在when 标签的 test中,类似if 的test
当每个when都不满足条件时,执行otherwise标签中的内容

<select id="selectnyannot" resultType="com.wj.dao.pojo.User">
select * from tb_user
       <trim prefix="where" prefixOverrides="and | or">
               <choose> <!--  若不使用where或trim标签 则需要 1=1 的条件,防止and拼接报错-->
                <when test="userName != null">
                and userName like  concat('%',#{userName},'%')
                </when>
                <when test="userPassword != null">
                and userPassword = #{userPassword}
                </when>
                <otherwise>
                and gender = 2
                </otherwise>
               </choose>
       </trim>
</select>

两个参数都为空
在这里插入图片描述
执行otherwise中的内容
在这里插入图片描述
#动态更新标签
标签在sql语句前输出set,并去除最后一句的逗号

<update id="upset" >
    update tb_user <set> <!--  需要拼接的包裹在set中,where不在其中,在mysql中DML语言不加where会修改所有数据(除了insert)-->
        <if test="userName != null">userName = #{userName},</if>
        <if test="userPassword != null">userPassword =#{userPassword}, </if>
    </set>
        where id = #{id}
</update>

使用trim改造

<update id="upset" >
    <!--update tb_user <set>-->
    <!--    <if test="userName != null">userName = #{userName},</if>-->
    <!--    <if test="userPassword != null">userPassword =#{userPassword}, </if>-->
    <!--</set>-->
    <!--    where id = #{id}-->
    update tb_user
    <trim prefix="set" suffix="where id=#{id}" suffixOverrides=",">
    <if test="userName != null">userName = #{userName},</if>
    <if test="userPassword != null">userPassword =#{userPassword}, </if>
    </trim>
</update>

#循环可迭代对象,生成 in的条件
属性
collection 必须指定,三种 list,array,map对应的键值
item 迭代元素的别名
index
open 开始,’(‘
close 结束,’)‘
separator 分隔符 ’,‘
数组作为参数

<select id="selectbyarray" resultType="com.wj.dao.pojo.User">
    select * from tb_user where id in
        <foreach collection="array" open="(" separator="," close=")" item="roleId">
        #{roleId}
        </foreach>
</select>

测试

 Integer[] roleIds={2,3,4,5};
            for (User user : mapper.selectbyarray(roleIds)) {
                System.out.println(user.getUserName()+"/"+user.getUserPassword()+"/");
            }

结果
在这里插入图片描述
集合作为参数

<select id="selectbyMap" resultType="com.wj.dao.pojo.User">
select * from tb_user where id in  --此处roleIds为map的键
    <foreach collection="roleIds"  open="(" separator="," close=")" item="roleId">
        #{roleId}
    </foreach>
</select>
  HashMap<String, Integer[]> map = new HashMap<>();
            Integer[] roleIds={2,3,4,5};
            map.put("roleIds",roleIds);
            for (User user : mapper.selectbyarray(roleIds)) {
                System.out.println(user.getUserName()+"/"+user.getUserPassword()+"/");
            }

请添加图片描述
#标签完成模糊查询
不使用concat(只适用于masql),${}(无法防止sql注入)。bind的使用提高了程序的可移植性

<select id="selectnyannot" resultType="com.wj.dao.pojo.User">
select * from tb_user
       <trim prefix="where" prefixOverrides="and | or">
       <bind name="asname" value="'%'+userName+'%'"/>
                <!--
                name 定义了一个名为asname的变量
                其值为 传入的参数 userName
                -->
               <choose>
                <when test="userName != null">
                <!--and userName like  concat('%',#{userName},'%')-->
                and userName like #{asname}  <!--这里使用bind中定义的变量名-->
                </when>
                <when test="userPassword != null">
                and userPassword = #{userPassword}
                </when>
                <otherwise>
                and gender = 2
                </otherwise>
               </choose>
       </trim>
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值