In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
今天小编给大家分享一下MyBatis动态SQL怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
MyBatis 框架动态 SQL
动态 SQL,通过 MyBatis 提供的各种标签对条件作出判断以实现动态拼接 SQL 语句。这里的条件判断使用的表达式为 OGNL 表达式。常用的动态 SQL 标签有、、、等。
MyBatis 的动态 SQL 语句,与 JSTL 中的语句非常相似。
动态 SQL,主要用于解决查询条件不确定的情况:在程序运行期间,根据 用户提交的查询条件进行查询。提交的查询条件不同,执行的 SQL 语句不同。 若将每种可能的情况均逐一列出,对所有条件进行排列组合,将会出现大量的 SQL 语句。此时,可使用动态 SQL 来解决这样的问题。
环境准备
1、创建新的 maven 项目,加入 mybatis , mysql 驱动依赖
2、创建实体类 Student , StudentDao 接口,StudentDao.xml ,mybatis.xml , 测试类
3、使用之前的表 student。
在 mapper 的动态 SQL 中若出现大于号(>)、小于号(=),小于等于号(= #{age}
测试方法:
@Testpublic void testSelect() throws IOException { Student param = new Student(); param.setName("李力"); param.setAge(18); List studentList = studentDao.selectStudentIf(param); studentList.forEach( stu -> System.out.println(stu));}动态 SQL 之 where
标签的中存在一个比较麻烦的地方:需要在 where 后手工添加 1=1 的子句。因为,若 where 后的所有条件均为 false,而 where 后若又没 有 1=1 子句,则 SQL 中就会只剩下一个空的 where,SQL 出错。
所以,在 where 后,需要添加永为真子句 1=1,以防止这种情况的发生。但当数据量很大时,会严重影响查询效率。
使用标签,在有查询条件时,可以自动添加上 where 子句;没 有查询条件时,不会添加 where 子句。需要注意的是,第一个标签中的 SQL 片断,可以不包含 and。
不过,写上 and 也不错,系统会将多出的 and 去掉。但其它中 SQL 片断的 and,必须要求写上。否则 SQL 语句将拼接出错。
语法: 其他动态 sql
接口方法:
List selectStudentWhere(Student student);
mapper文件:
select id,name,email,age from student and name = #{name} and age > #{age}
测试方法:
@Testpublic void testSelectWhere() throws IOException { Student param = new Student(); param.setName("李力"); param.setAge(18); List studentList = studentDao.selectStudentWhere(param); studentList.forEach( stu -> System.out.println(stu));}动态 SQL 之 foreach
标签用于实现对于数组与集合的遍历。对其使用,需要注意:
collection 表示要遍历的集合类型, list ,array 等。
open、close、separator 为对遍历内容的 SQL 拼接。
语法:
#{item 的值}
1、遍历List
表达式中的 List 使用 list 表示,其大小使用 list.size 表示。
需求:查询学生 id 是 1002,1005,1006
接口方法:
List selectStudentForList(List idList);
mapper文件:
select id,name,email,age from student where id in #{stuid}
测试方法:
@Testpublic void testSelectForList() { List list = new ArrayList(); list.add(1002); list.add(1005); list.add(1006); List studentList = studentDao.selectStudentForList(list); studentList.forEach( stu -> System.out.println(stu));}
2、遍历List
接口方法:
List selectStudentForList2(List stuList);
mapper文件:
select id,name,email,age from student where id in #{stuobject.id}
测试方法:
@Testpublic void testSelectForList2() { List list = new ArrayList(); Student s1 = new Student(); s1.setId(1002); list.add(s1); s1 = new Student(); s1.setId(1005); list.add(s1); List studentList = studentDao.selectStudentForList2(list); studentList.forEach( stu -> System.out.println(stu));}动态 SQL 之代码片段
标签用于定义 SQL 片断,以便其它 SQL 标签复用。而其它标签使 用该 SQL 片断,需要使用子标签。该标签可以定义 SQL 语句中的任何部分,所以子标签可以放在动态 SQL 的任何位置。
接口方法:
List selectStudentSqlFragment(List stuList);
mapper文件:
select id,name,email,age from student where id in #{stuobject.id}
测试方法:
@Testpublic void testSelectSqlFragment() { List list = new ArrayList(); Student s1 = new Student(); s1.setId(1002); list.add(s1); s1 = new Student(); s1.setId(1005); list.add(s1); List studentList = studentDao.selectStudentSqlFragment(list); studentList.forEach( stu -> System.out.println(stu));}以上就是"MyBatis动态SQL怎么使用"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.