目录

    前言

    我们知道在项目开发中之前使用数据库查询,都是基于jdbc,进行连接查询,然后是高级一点jdbctemplate进行查询,但是我们发现还是不是很方便,有大量重复sql语句,与代码偶合,效率低下,于是就衍生出来orm框架,如mybatis,hibernate,还有springboot的,spring data jpa

    条件查询

    我们知道在mybatis mapper文件中条件查询符,如>=,<,之类是不能直接写的会报错的需要转移一下 如下图表

    详细内容

    常见的条件查询操作有

    我们通过mybatis 提供的特有标签进行条件判断,达到动态拼接sql语句

    if标签 where标签 choose when otherwise标签 foreach标签

    快速入门

    if标签

    语法:

    <if test=”xxx != null and xxx != ””>

    test中写判断条件 参数直接paramn或者别名 多个条件使用and或者or连接

    只要条件成立就拼接在sql语句中,都成立就全部都拼接

    注意where子句中加上1=1来规避and的风险

    如下例子:

    <select id="selg" resulttype="log">
        select * from log where 1=1
        <if test="param1!=null and param1!=''">
        and outno=#{param1}
        </if>
        <if test="param2!=null and param2!=''">
        and inno=#{param2}
        </if>
    </select>

    where标签

    对上面if标签条件判断where连接做了处理会自动的给sql语句添加where关键字,并将第一个and去除

    上面sql可以改造成如下:

    <select id="selg" resulttype="log">
          select * from log 
      <where>
           <if test="param1!=null and param1!=''">
          and outno=#{param1}
          </if>
          <if test="param2!=null and param2!=''">
          and inno=#{param2}
          </if>
      </where>
         
    </select>

    choose when otherwise标签

    类似于java语法中的,case,switch语句判断

    条件只要有一个成立,其他的就不会再判断了。如果没有成立的条件则默认执行otherwise中的内容

    上面sql可以改造成如下:

    <select id="selg" resulttype="log">
          select * from log 
      <where>
        <choose>
           <when test="param1!=null and param1!=''">
          and outno=#{param1}
          </when>
          <when test="param2!=null and param2!=''">
          and inno=#{param2}
          </when>
          <otherwise>
            and 1=1
          </otherwise>
        </choose>
      </where>
         
    </select>

    foreach标签

    语法:

     <foreach collection=”idlist” item=”id” open=”(” separator=”,” close=”)”>
    </foreach>

    • collection:要遍历的集合对象
    • item:记录每次遍历的结果
    • open:在结果的左边添加内容
    • separator:结果和结果之间的内容
    • close:在最后添加的内容

    常用于in查询,和批量插入操作 如下案例:

    <select id="self" parametertype="list" resulttype="account">
        select * from account where ano in
        <foreach collection="list" item="item" open="(" separator="," close=")">
        #{item}
        </foreach>
       </select>
    
    
    <insert id="insertbatch">
            insert into t_user
            (id, name, password)
            values
            <foreach collection ="userlist" item="user" separator =",">
                (#{user.id}, #{user.name}, #{user.password})
            </foreach >
        </insert>

    其他标签使用参考

    场景案例

    1.当我们需要对多张表的关联数据进行复杂动态条件查询的时候,就需要用到 if标签进行判断 如下

    根据用户手机号姓名年龄性别,等进行动态条件检索,这个时候我们需要动态通过调节去拼接sql 当条件满足sql语句加上对应条件差许

    <select id="findusersbyuser" resulttype="cn.soboys.kmall.sys.entity.user">
            select tu.user_id,tu.username,tu.ssex,td.dept_name,tu.mobile,tu.email,tu.status,tu.create_time,
            td.dept_id
            from t_user tu left join t_dept td on tu.dept_id = td.dept_id
            where tu.admin_type_id  &gt;= 0 and tu.admin_type_id  &lt;= #{userparams.admintype}
            <if test="userparams.roleid != null and userparams.roleid != ''">
               and (select group_concat(ur.role_id)
                from t_user u
                right join t_user_role ur on ur.user_id = u.user_id,
                t_role r
                where r.role_id = ur.role_id
                and u.user_id = tu.user_id and r.role_id=#{userparams.roleid})
            </if>
    
    
            <if test="userparams.mobile != null and userparams.mobile != ''">
                and tu.mobile =#{userparams.mobile}
            </if>
            <if test="userparams.username != null and userparams.username != ''">
                and tu.username   like concat('%',#{userparams.username},'%')
            </if>
            <if test="userparams.ssex != null and userparams.ssex != ''">
                and tu.ssex  =#{userparams.ssex}
            </if>
            <if test="userparams.status != null and userparams.status != ''">
                and tu.status =#{userparams.status}
            </if>
            <if test="userparams.deptid != null and userparams.deptid != ''">
                and td.dept_id =#{userparams.deptid}
            </if>
            <if test="userparams.createtime != null and userparams.createtime != ''">
                and date_format(tu.create_time,'%y%m%d') between substring_index(#{userparams.createtime},'#',1) and substring_index(#{userparams.createtime},'#',-1)
            </if>
        </select>

    对应mapper对应的方法

    <t> ipage<user> findusersbyuser(page<t> page, @param("userparams") searchuserparams userparams);

    对应参数实体对象

    @data
    public class searchuserparams {
        private string username;
        private string mobile;
        private string status;
        private string ssex;
        private long deptid;
        private string createtime;
        private long admintype;
        private string roleid;
    }

    通过if标签去判断条件是否满足,满足就拼接对应sql

    注意在上面我们提到的条件拼接第一个是where连接,而不是and应规避and风险保证sql语法正确 如下

    <select id="findsearchcouponspage" parametertype="cn.soboys.kmall.bean.web.params.searchcouponparams" resulttype="coupon">
            select *
            from coupon c
            left join user_coupon uc on c.coupon_id = uc.coupon_id
            where 1 = 1
            <if test="couponparams.userid != null and couponparams.userid != ''">
               and uc.user_id =#{couponparams.userid}
            </if>
            <if test="couponparams.status != null and couponparams.status != ''">
                and c.status =#{couponparams.status}
            </if>
            <if test="couponparams.couponid != null and couponparams.couponid != ''">
                and c.coupon_id =#{couponparams.couponid}
            </if>
            <if test="couponparams.coupontype != null and couponparams.coupontype != ''">
                and c.type =#{couponparams.coupontype}
            </if>
        </select>

    我们可以通过假定给他一个默认条件 where 1 = 1来解决,也可以通过嵌套where标签来解决

    到此这篇关于mybatis的mapper特殊字符转移及动态sql条件查询的文章就介绍到这了,更多相关mybatis的mapper特殊字符转移内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!