服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - MyBatis 详细讲解动态 SQL的使用

MyBatis 详细讲解动态 SQL的使用

2022-11-08 11:22托马斯-酷涛 Java教程

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以

MyBatis 框架动态 SQL

动态 SQL,通过 MyBatis 提供的各种标签对条件作出判断以实现动态拼接 SQL 语句。这里的条件判断使用的表达式为 OGNL 表达式。常用的动态 SQL 标签有<if>、<where>、<choose/>、<foreach>等。

MyBatis 的动态 SQL 语句,与 JSTL 中的语句非常相似。

动态 SQL,主要用于解决查询条件不确定的情况:在程序运行期间,根据 用户提交的查询条件进行查询。提交的查询条件不同,执行的 SQL 语句不同。 若将每种可能的情况均逐一列出,对所有条件进行排列组合,将会出现大量的 SQL 语句。此时,可使用动态 SQL 来解决这样的问题。

环境准备

1、创建新的 maven 项目,加入 mybatis , mysql 驱动依赖

2、创建实体类 Student , StudentDao 接口,StudentDao.xml ,mybatis.xml , 测试类

3、使用之前的表 student。

在 mapper 的动态 SQL 中若出现大于号(>)、小于号(=),小于等于号(<=)等符号,最好将其转换为实体符号。否则, XML 可能会出现解析出错问题。

特别是对于小于号(<),在 XML 中是绝不能出现的。否则解析 mapper 文件会出错。

实体符号表:

< 小于 &lt
> 大于 &gt
>= 大于等于 &gt;=
<= 小于等于 &lt;=

动态 SQL 之 if

对于该标签的执行,当 test 的值为 true 时,会将其包含的 SQL 片断拼接 到其所在的 SQL 语句中。

语法: <if test="条件">  sql 语句的部分</if>

接口方法:

?
1
List<Student> selectStudentIf(Student student);

mapper文件:

?
1
2
3
4
5
6
7
8
9
10
<select id="selectStudentIf" resultType="com.bjpowernode.domain.Student">
 select id,name,email,age from student
 where 1=1
 <if test="name != null and name !='' ">
 and name = #{name}
 </if>
 <if test="age > 0 ">
 and age &gt; #{age}
 </if>
</select>

测试方法:

?
1
2
3
4
5
6
7
8
@Test
public void testSelect() throws IOException {
 Student param = new Student();
 param.setName("李力");
 param.setAge(18);
 List<Student> studentList = studentDao.selectStudentIf(param);
 studentList.forEach( stu -> System.out.println(stu));
}

动态 SQL 之 where

<if/>标签的中存在一个比较麻烦的地方:需要在 where 后手工添加 1=1 的子句。因为,若 where 后的所有<if/>条件均为 false,而 where 后若又没 有 1=1 子句,则 SQL 中就会只剩下一个空的 where,SQL 出错。

所以,在 where 后,需要添加永为真子句 1=1,以防止这种情况的发生。但当数据量很大时,会严重影响查询效率。

使用<where/>标签,在有查询条件时,可以自动添加上 where 子句;没 有查询条件时,不会添加 where 子句。需要注意的是,第一个<if/>标签中的 SQL 片断,可以不包含 and。

不过,写上 and 也不错,系统会将多出的 and 去掉。但其它<if/>中 SQL 片断的 and,必须要求写上。否则 SQL 语句将拼接出错。

语法:<where> 其他动态 sql</where>

接口方法:

?
1
List<Student> selectStudentWhere(Student student);

mapper文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
<select id="selectStudentWhere"
resultType="com.bjpowernode.domain.Student">
 select id,name,email,age from student
 <where>
 <if test="name != null and name !='' ">
 and name = #{name}
 </if>
 <if test="age > 0 ">
 and age &gt; #{age}
 </if>
 </where>
</select>

测试方法:

?
1
2
3
4
5
6
7
8
@Test
public void testSelectWhere() throws IOException {
 Student param = new Student();
 param.setName("李力");
 param.setAge(18);
 List<Student> studentList = studentDao.selectStudentWhere(param);
 studentList.forEach( stu -> System.out.println(stu));
}

动态 SQL 之 foreach

<foreach/>标签用于实现对于数组与集合的遍历。对其使用,需要注意:

  • collection 表示要遍历的集合类型, list ,array 等。
  • open、close、separator 为对遍历内容的 SQL 拼接。

语法:

?
1
2
3
4
<foreach collection="集合类型" open="开始的字符" close="结束的字符"
 item="集合中的成员" separator="集合成员之间的分隔符">
 #{item 的值}
</foreach>

1、遍历List<简单类型>

表达式中的 List 使用 list 表示,其大小使用 list.size 表示。

需求:查询学生 id 是 1002,1005,1006

接口方法:

?
1
List<Student> selectStudentForList(List<Integer> idList);

mapper文件:

?
1
2
3
4
5
6
7
8
9
10
11
<select id="selectStudentForList"
resultType="com.bjpowernode.domain.Student">
 select id,name,email,age from student
 <if test="list !=null and list.size > 0 ">
 where id in
 <foreach collection="list" open="(" close=")"
 item="stuid" separator=",">
 #{stuid}
 </foreach>
 </if>
</select>

测试方法:

?
1
2
3
4
5
6
7
8
9
@Test
public void testSelectForList() {
 List<Integer> list = new ArrayList<>();
 list.add(1002);
 list.add(1005);
 list.add(1006);
 List<Student> studentList = studentDao.selectStudentForList(list);
 studentList.forEach( stu -> System.out.println(stu));
}

2、遍历List<对象类型>

接口方法:

?
1
List<Student> selectStudentForList2(List<Student> stuList);

mapper文件:

?
1
2
3
4
5
6
7
8
9
10
11
<select id="selectStudentForList2"
resultType="com.bjpowernode.domain.Student">
 select id,name,email,age from student
 <if test="list !=null and list.size > 0 ">
 where id in
 <foreach collection="list" open="(" close=")"
 item="stuobject" separator=",">
 #{stuobject.id}
 </foreach>
 </if>
</select>

测试方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void testSelectForList2() {
 List<Student> list = new ArrayList<>();
 Student s1 = new Student();
 s1.setId(1002);
 list.add(s1);
 s1 = new Student();
 s1.setId(1005);
 list.add(s1);
 List<Student> studentList = studentDao.selectStudentForList2(list);
 studentList.forEach( stu -> System.out.println(stu));
}

动态 SQL 之代码片段

<sql/>标签用于定义 SQL 片断,以便其它 SQL 标签复用。而其它标签使 用该 SQL 片断,需要使用<include/>子标签。该<sql/>标签可以定义 SQL 语句中的任何部分,所以<include/>子标签可以放在动态 SQL 的任何位置。

接口方法:

?
1
List<Student> selectStudentSqlFragment(List<Student> stuList);

mapper文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--创建 sql 片段 id:片段的自定义名称-->
<sql id="studentSql">
 select id,name,email,age from student
</sql>
<select id="selectStudentSqlFragment"
resultType="com.bjpowernode.domain.Student">
 <!-- 引用 sql 片段 -->
 <include refid="studentSql"/>
 <if test="list !=null and list.size > 0 ">
 where id in
 <foreach collection="list" open="(" close=")"
 item="stuobject" separator=",">
 #{stuobject.id}
 </foreach>
 </if>
</select>

测试方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void testSelectSqlFragment() {
 List<Student> list = new ArrayList<>();
 Student s1 = new Student();
 s1.setId(1002);
 list.add(s1);
 s1 = new Student();
 s1.setId(1005);
 list.add(s1);
 List<Student> studentList = studentDao.selectStudentSqlFragment(list);
 studentList.forEach( stu -> System.out.println(stu));
}

到此这篇关于MyBatis 详细讲解动态 SQL的使用的文章就介绍到这了,更多相关MyBatis 动态 SQL内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_54925305/article/details/123607625

延伸 · 阅读

精彩推荐