0
点赞
收藏
分享

微信扫一扫

MYBatis-动态SQL


MyBatis动态SQL

MYBatis-动态SQL_sql语句

什么是动态SQL?

官方给出动态SQL的解释是一个基于OGNL的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。

前置操作:环境准备
关联表操作,两张表,员工表和部门表,MyBatis进行一对多的关联操作
从多表角度出发使用association

<resultMap id="maps" type="emp01">
<id column="empno" property="empNo" />
<result column="ename" property="eName" />
<result column="job" property="job" />
<result column="sal" property="salary" />
<result column="mgr" property="mgr" />
<association property="dept" column="deptno"
javaType="Dept01">
<id column="id" property="id" />
<result column="name" property="name" />
</association>
</resultMap>

提取公用的sql

<sql id="stu_pre">
empno,ename,job,mgr,sal,d.id,d.name
</sql>
<select id="queryAll" resultMap="maps">
select <include refid="stu_pre"></include> from emp01 e inner join dept01 d
on e.deptno=d.id
</select>

IF标签

使用If标签的作用是通常结合where字句来完成的,但是这里先不介绍使用where标签,看以下的实例:

/**
* 如果姓名为null或者为空字串判断部门编号是否为null,如果部门编号也为null
* 则查询所有
* 如果给了姓名 依据姓名查
* 如果给了部门编号则查询制定部门的员工信息
*/

List<Emp01> selectEmpByNameDeptNo(@Param("eName") String eName,@Param("deptNo") Integer deptNo);

<select id="selectEmpByNameDeptNo" resultMap="maps">
select
e.empno,
e.ename,
e.job,
e.mgr,
e.sal,
d.id,
d.name
from emp01 e
inner join
dept01 d
on e.deptno=d.id
where 1=1
<if test="eName!=null and eName!=''">
and e.ename=#{eName}
</if>
<if test="deptNo!=null and deptNo!=''">
and e.deptno = #{deptNo}
</if>
</select>

MYBatis-动态SQL_sql_02


查看所封装的sql语句

-- 传入了一个deptno
select e.empno, e.ename, e.job, e.mgr, e.sal, d.id, d.name from emp01 e inner join dept01 d on e.deptno=d.id where 1=1 and e.deptno = ?
-- 传入了一个ename
select e.empno, e.ename, e.job, e.mgr, e.sal, d.id, d.name from emp01 e inner join dept01 d on e.deptno=d.id where 1=1 and e.ename=?
-- 传入了两个参数
select e.empno, e.ename, e.job, e.mgr, e.sal, d.id, d.name from emp01 e inner join dept01 d on e.deptno=d.id where 1=1 and e.ename=? and e.deptno =

这种操作有一种鸡肋之处,就是你会多一个查询的条件1=1。不推荐使用,原因就是影响执行效率。
等后面讲了where标签就可以避免

Where标签

还是上面的例子,我们使用where标签,则会有以下的写法

</select>
<select id="selectEmpByNameDeptNo" resultMap="maps">
select
e.empno,
e.ename,
e.job,
e.mgr,
e.sal,
d.id,
d.name
from emp01 e
inner join
dept01 d
on e.deptno=d.id

<where>
<if test="eName!=null and eName!=''">
and e.ename=#{eName}
</if>
<if test="deptNo!=null and deptNo!=''">
and e.deptno = #{deptNo}
</if>
</where>
</select>

这样写的好处就是利用了where标签的特性,它可以帮助我们动态的加上where 而且可以帮助我们去除子句中的多余字符 如and 和or

select e.empno, e.ename, e.job, e.mgr, e.sal, d.id, d.name from emp01 e inner join dept01 d on e.deptno=d.id WHERE e.ename=?

从上面看出,where标签帮助我们完成了添加where标签的工作,以及检查是否是第一个条件并判断是否合规等工作过

官方解释:
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为

choose、when、otherwise

这是一般一起使用的一套标签,其功能类似于从switch
具体详见下面的实例:

/**
* choose when otherwise
* 如果姓名为null或者为空字串判断部门编号是否为null,如果部门编号也为null
* 则查询所有
* 如果给了姓名 依据姓名查
* 如果给了部门编号则查询制定部门的员工信息
*/

List<Emp01> selectEmpByNameDeptNo2(@Param("eName") String eName,@Param("deptNo") Integer deptNo);

<select id="selectEmpByNameDeptNo2" resultMap="maps">
select
e.empno,
e.ename,
e.job,
e.mgr,
e.sal,
d.id,
d.name
from emp01 e
inner join
dept01 d
on e.deptno=d.id
<where>
<choose>
<when test="eName!=null and eName!=''">
and e.ename = #{eName}
</when>
<when test="deptNo!=null and deptNo!=''">
and e.deptno = #{deptNo}
</when>
<otherwise>and 1=1</otherwise>
</choose>
</where>
</select>

注意与if的区别就是 如果if中给出的参数都有 则会按照两个参数的规则查询,而choose whern otherwise是如果两个参数同时给出只会按照一个去查询

如下所示:
测试代码:

@Test
public void testQueryByNameDeptNo2() throws Exception {
SqlSession session = SessionUtils.getSession();
List<Emp01> list = session.getMapper(EmpMapper.class).selectEmpByNameDeptNo2("MARTIN", 30);
for (Emp01 emp01 : list) {
System.out.println(emp01);
}
session.commit();
session.close();
}

查询结果所对应的sql语句

select e.empno, e.ename, e.job, e.mgr, e.sal, d.id, d.name from emp01 e inner join dept01 d on e.deptno=d.id WHERE e.ename =

MYBatis-动态SQL_数据库_03

SET标签

set标签用于维护update语句中的set字句,功能可以说很强大:见下案例:

  1. 满足条件时,会自动添加set关键字
  2. 会去除set子句中多余的逗号
  3. 不满足条件时,不会生成set关键字

int updateStudentOne(Student student);

<update id="updateStudentOne" parameterType="student">
update student
<set>
<if test="name!=null">
name=#{name},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="age!=null">
age=#{age},
</if>
</set>
<where>
id = #{id}
</where>
</update>

MYBatis-动态SQL_sql_04


拼接的sql语句如下:

update student SET name=?, email=?, age=? WHERE id =

TRIM标签

定制where或者set标签,在Java中是取出前后的空格
trim标签的4个属性:

  1. prefix:前缀:在前面添加内容
  2. prefixOverrides :从前面去除内容
  3. suffix:向后面添加内容
  4. suffixOverrides:向后面去除内容

int updateEmp(Emp01 emp01);

<update id="updateEmp" parameterType="emp01">
update emp01
<trim prefix="set" suffixOverrides=",">
<if test="eName!=null and eName !=''">
ename=#{eName},
</if>
<if test="salary!=null and salary >0">
sal = #{salary},
</if>
</trim>
where empno = #{empNo}
</update>

MYBatis-动态SQL_sql_05

BIND标签 数据进行加工绑定

bind标签主要用于模糊匹配来拼接其余字串 定制化参数

/**
* bind标签完成数据的在加工,用于模糊匹配
*/

List<Emp01> queryByNameLike(@Param("name") String nameLike);

<select id="queryByNameLike" parameterType="string" resultMap="maps">
select
empno,ename,job,mgr,sal,d.id,d.name from emp01 e inner join dept01 d
on e.deptno=d.id
<where>
<if test="name!=null and name !=''">
<bind name="name" value="'%'+name+'%'"/>
and ename like #{name}
</if>
</where>
</select>

测试代码

@Test
public void testQueryByNameLike() throws Exception {
SqlSession session = SessionUtils.getSession();
List<Emp01> list = session.getMapper(EmpMapper.class).queryByNameLike("M");
for (Emp01 emp01 : list) {
System.out.println(emp01);
}
session.commit();
session.close();
}

MYBatis-动态SQL_数据库_06

foreach标签 循环遍历

官网解释:
动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符,看它多智能!

用于在SQL语句中遍历集合参数,在in查询中使用

  1. collection:待遍历的集合
  2. open:设置开始符号
  3. item:迭代变量
  4. separator:项目分隔符
  5. close:设置结束符

List<Emp01> selectByIds(@Param("ids") List<Integer> ids);

<select id="selectByIds" resultMap="maps" parameterType="list">
select
empno,ename,job,mgr,sal,d.id,d.name from emp01 e inner join dept01 d
on e.deptno=d.id
<where>
empno in
<foreach collection="ids" item="empno" open="(" separator="," close=")">
#{empno}
</foreach>
</where>
</select>

MYBatis-动态SQL_sql语句_07


举报

相关推荐

0 条评论