MyBatis 动态SQL
2018-01-12 22:59
274 查看
动态SQL,主要用于解决查询条件不确定的情况。
例如:
select id,name,age,score from student where id=?name=?
age=?score=?
每一个条件都可有可无,那where条件有4*3*2*1=24这么多情况,总不能写24个sql。
可使用动态SQL来解决这一的问题。
动态SQL,即通过MyBatis提供的各种标签对条件作出判断以实现动态拼接SQL语句。这里条件判断使用的表达式为OGNL表达式(Object
Graphic Navigation Language 对象图导航语言)
1、if
if标签,where条件要加 1=1.对于数据量大的查询而言,1=1也是判断条件,还是会影响查询效率。
2、where
where标签,会自动去掉第一个and
3、choose
choose标签,类似java里面的switch,when对应case,otherwise对应default。
4、foreach
foreach针对数组、List等集合形式的数据。
5、include
sql片段可以提高代码的复用性,但是降低了代码的可读性。
6、mapper对应的Dao接口
例如:
select id,name,age,score from student where id=?name=?
age=?score=?
每一个条件都可有可无,那where条件有4*3*2*1=24这么多情况,总不能写24个sql。
可使用动态SQL来解决这一的问题。
动态SQL,即通过MyBatis提供的各种标签对条件作出判断以实现动态拼接SQL语句。这里条件判断使用的表达式为OGNL表达式(Object
Graphic Navigation Language 对象图导航语言)
1、if
<select id="selectByIf" resultType="Student"> select id,name,age,score from student where 1 = 1 <if test="name != null and name != ''"> and name like '%' #{name} '%' </if> <if test="age > 0"> and age > #{age} </if> </select>
if标签,where条件要加 1=1.对于数据量大的查询而言,1=1也是判断条件,还是会影响查询效率。
2、where
<select id="selectByWhere" resultType="Student"> select id,name,age,score from student <where> <if test="name != null and name != ''"> and name like '%' #{name} '%' </if> <if test="age > 0"> and age > #{age} </if> </where> </select>
where标签,会自动去掉第一个and
3、choose
<select id="selectByChoose" resultType="Student"> select id,name,age,score from student <where> <choose> <when test="name != null and name != ''"> and name like '%' #{name} '%' </when> <when test="age > 0"> and age > #{age} </when> <otherwise> 1 = 2 </otherwise> </choose> </where> </select>
choose标签,类似java里面的switch,when对应case,otherwise对应default。
4、foreach
<select id="selectByForeach" resultType="Student"> select id,name,age,score from student <if test="array.length > 0"> where id in <foreach collection="array" item="myid" open="(" close=")" separator=","> #{myid} </foreach> </if> </select> <select id="selectByForeach2" resultType="Student"> select id,name,age,score from student <if test="list.size > 0"> where id in <foreach collection="list" item="myid" open="(" close=")" separator=","> #{myid} </foreach> </if> </select> <select id="selectByForeach3" resultType="Student"> select id,name,age,score from student <if test="list.size > 0"> where id in <foreach collection="list" item="myid" open="(" close=")" separator=","> #{myid.id} </foreach> </if> </select>
foreach针对数组、List等集合形式的数据。
5、include
<select id="selectBySqlFragment" resultType="Student"> <include refid="selectFront" /> from student <if test="list.size > 0"> where id in <foreach collection="list" item="myid" open="(" close=")" separator=","> #{myid.id} </foreach> </if> </select> <!-- sql片段可以替换Sql任何部分 --> <sql id="selectFront"> select id,name,age,score </sql>
sql片段可以提高代码的复用性,但是降低了代码的可读性。
6、mapper对应的Dao接口
@Test public void test01() { // Student stu = new Student("张", 23, 89); // Student stu = new Student("", 23, 89); Student stu = new Student("", 0, 89); List<Student> students = dao.selectByIf(stu); for (Student student : students) { System.out.println(student); } } @Test public void test02() { Student stu = new Student("张", 23, 89); // Student stu = new Student("", 23, 89); // Student stu = new Student("", 0, 89); List<Student> students = dao.selectByWhere(stu); for (Student student : students) { System.out.println(student); } } @Test public void test03() { // Student stu = new Student("张", 23, 89); // Student stu = new Student("", 23, 89); Student stu = new Student("", 0, 89); List<Student> students = dao.selectByChoose(stu); for (Student student : students) { System.out.println(student); } } @Test public void test04() { int[] ids = { 1, 2, 3, 7 }; List<Student> students = dao.selectByForeach(ids); for (Student student : students) { System.out.println(student); } } @Test public void test05() { List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); ids.add(7); List<Student> students = dao.selectByForeach2(ids); for (Student student : students) { System.out.println(student); } } @Test public void test06() { List<Student> stus = new ArrayList<>(); Student stu1 = new Student(); stu1.setId(1); Student stu2 = new Student(); stu2.setId(2); Student stu3 = new Student(); stu3.setId(7); stus.add(stu1); stus.add(stu2); stus.add(stu3); List<Student> students = dao.selectByForeach3(stus); for (Student student : students) { System.out.println(student); } } @Test public void test07() { List<Student> stus = new ArrayList<>(); Student stu1 = new Student(); stu1.setId(1); Student stu2 = new Student(); stu2.setId(2); Student stu3 = new Student(); stu3.setId(7); stus.add(stu1); stus.add(stu2); stus.add(stu3); List<Student> students = dao.selectBySqlFragment(stus); for (Student student : students) { System.out.println(student); } }
相关文章推荐
- MyBatis利用动态SQL进行批量操作
- mybatis 动态sql讲解与示例分析
- MyBatis知多少(25)动态SQL
- 在mybatis的sqlMapper中使用Ognl判断动态sql语句
- MyBatis的动态SQL详解
- mybatis的动态sql-if和sql片段 foreach的使用
- Mybatis学习笔记(九)【动态sql】
- MyBatis 动态SQL使用心得之Map中Key判断
- MyBatis的动态SQL详解
- MyBatis动态SQL
- Mybatis学习之动态sql语句(7)
- mybatis的动态sql标签
- MyBatis的动态SQL详解
- MyBatis学习07】动态sql
- MyBatis的动态SQL操作--插入
- mybatis入门基础(五)----动态SQL
- Mybatis深入了解(五)----动态SQL
- MyBatis-动态SQL
- mybatis_user_guide(5) 动态 SQL
- 【Mybatis】动态SQL