MyBatis动态SQL

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

MyBatis动态SQL

羡羡ˇ   2022-06-03 我要评论

mybatis最强大的功能之一便是它的动态sql能力

       借用官方文档的一段话 : 如果您以前有使用JDBC或者类似框架的 经历,您就会明白把SQL语句条件连接在一起是多么的痛苦,要确保不能忘记空格或者不要在 columns列后面省略一个逗号等。动态语句能够完全解决掉这些痛苦。

那么如果没有这种功能到底有多痛苦呢 ? 我们来举例说明

       这是一张表 , 试想如果我们通过 name 和 age来查询表信息时 , sql语句中肯定会存在 where和and字句 , 但是如果 name或者age 有一个为null或者都为null , 那么此时的 where 和and就会被孤立,那么这样肯定会出现很多问题 , 所以mybatis的动态sql功能帮助我们完美解决

MyBatis 中用于实现动态 SQL 的元素主要有:

If   where   trim   set  choose(when, otherwise)   foreach

if和where

<select id="selectAllEmployee1" resultMap="employeeMap2" parameterType="Employee">
   SELECT e.id,e.name ename,e.age,d.name dname FROM employee e
      LEFT JOIN dept d ON e.deptId = d.id
<where>
    <if test="name!=null &amp; name!=''">
         e.name =#{name}
    </if>
    <if test="age!=null &amp; age!=''">
         and e.age =#{age}
    </if>
</where>
</select>

使用这种标签 , 动态sql可以根据 条件来自动帮我们完善sql 

SqlSession sqlSession= MybatisUtil.getSqlSession();
EmployeeDao mapper=sqlSession.getMapper(EmployeeDao.class);
//创建一个对象,set值
Employee employee = new Employee();
employee.setName("");
employee.setAge(null);
List<Employee> employees = mapper.selectAllEmployee1(employee);
System.out.println(employees);
sqlSession.commit();
sqlSession.close();

第一次我们都设置null值, 表中数据完全被查询

第二次我们只查询年龄

employee.setName("");
employee.setAge(20);

查询到两条年龄为20的数据 , 这就是mybatis动态sql的强大之处

trim

上述的 where 与 if 我们也可以使用 trim 来代替where 

<select id="selectAllEmployee1" resultMap="employeeMap2" parameterType="Employee">
     SELECT e.id,e.name ename,e.age,d.name dname FROM employee e
     LEFT JOIN dept d ON e.deptId = d.id
     <trim prefix="where" prefixOverrides="or|and">
        <if test="name!=null &amp; name!=''">
            e.name =#{name}
        </if>
        <if test="age!=null &amp; age!=''">
            and e.age =#{age}
        </if>
     </trim>
</select>

这里有两个属性 prefix , prefixOverrides

prefix : 代表前缀 , 如果if 中有成立的条件, 就会在sql前面拼入where字句

prefixOverrides :  根据if 条件自动判断是否去除 or | and字句

相应的也有suffix与suffixOverrides , 代表对尾部的判断

Choose

choose代表多路选择(多选一)

<select id="selectAllEmployee1" resultMap="employeeMap2" parameterType="Employee">
        SELECT e.id,e.name ename,e.age,d.name dname FROM employee e
        LEFT JOIN dept d ON e.deptId = d.id
        <trim prefix="where" prefixOverrides="or|and">
            <choose>
                <when test="name!=null &amp; name!=''">
                    and e.name =#{name}
                </when>
                <when test="age!=null">
                    and e.age =#{age}
                </when>
                <otherwise>
                    and e.name ='李雷'
                </otherwise>
            </choose>
        </trim>
    </select>

当<when>中的条件成立时, 走when中的语句,都不成立走<otherwise>

Set

set 可以根据条件自动添加set字句,动态更新列,也可以来剔除追加到条件末尾的任何不相关的逗号

<update id="updateEmployee">
        update employee
        <set>
            <if test="name!=null &amp; name!=''">
                name=#{name},
            </if>
            <if test="age!=null">
                age=#{age},
            </if>
        </set>
        where id=#{id}
    </update>

foreach

       <foreach> 主要用在构建 in 条件中,它可以在 SQL 语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。 item 表示集合中每一个元素进行迭代时的别名,index 指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open 表示该语句以什么开始, separator 表示在每次进行迭代之间以什么符号作为分隔符,close 表示以什么结束,在使用 foreach 的时候最关键的也是最容易出错的就是 collection 属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的。

– 如果传入的是单参数且参数类型是一个 List 的时候,collection 属 性值为 list

– 如果传入的是单参数且参数类型是一个 array 数组的时候, collection 的属性值为array

//创建一个list集合
List<Integer> list = new ArrayList<>();
list.add(19);
list.add(20);
List<Employee> employees = mapper.selectAllEmployee2(list);

接口方法如下 : 

  List<Employee> selectAllEmployee2(List<Integer> list);

对应动态sql如下 : 

<select id="selectAllEmployee2" resultMap="employeeMap2" parameterType="Employee">
        SELECT e.id,e.name ename,e.age,d.name dname FROM employee e
        LEFT JOIN dept d ON e.deptId = d.id where age in
        <foreach collection="list" item="age" open="(" separator="," close=")">
             #{age}
        </foreach>
    </select>

这里我们传入的是一个集合, 所以参数选择 list , 通过foreach我们可以动态的根据集合里的值来查询

关于动态sql就介绍到这,感谢阅读 !

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们