博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis动态SQL语句
阅读量:7287 次
发布时间:2019-06-30

本文共 4843 字,大约阅读时间需要 16 分钟。

一 if标签

1
2
3
4
5
6
<select id=
" getStudentListLikeName " 
parameterType=
"StudentEntity" 
resultMap=
"studentResultMap"
>
 
SELECT * from STUDENT_TBL ST
 
<
if 
test=
"studentName!=null and studentName!='' "
>
  
WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(
'%'
, #{studentName}),
'%'
)
 
</
if
>
</select>

二 where标签

1
2
3
4
5
6
7
8
9
10
11
<select id=
"getStudentListWhere" 
parameterType=
"StudentEntity" 
resultMap=
"studentResultMap"
>
 
SELECT * from STUDENT_TBL ST
 
<where>
  
<
if 
test=
"studentName!=null and studentName!='' "
>
   
ST.STUDENT_NAME LIKE CONCAT(CONCAT(
'%'
, #{studentName}),
'%'
)
  
</
if
>
  
<
if 
test=
"studentSex!= null and studentSex!= '' "
>
   
AND ST.STUDENT_SEX = #{studentSex}
  
</
if
>
 
</where>
</select>

如果它包含的标签中有返回值的话就插入一个where。此外如果标签返回的内容是以AND或OR开头的,则它会剔除掉。

三 set 标签

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<update id=
"updateStudent" 
parameterType=
"StudentEntity"
>
 
UPDATE STUDENT_TBL
 
<set>
  
<
if 
test=
"studentName!=null and studentName!='' "
>
   
STUDENT_TBL.STUDENT_NAME = #{studentName},
  
</
if
>
  
<
if 
test=
"studentSex!=null and studentSex!='' "
>
   
STUDENT_TBL.STUDENT_SEX = #{studentSex},
  
</
if
>
  
<
if 
test=
"studentBirthday!=null "
>
   
STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  
</
if
>
  
<
if 
test=
"classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "
>
   
STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  
</
if
>
 
</set>
 
WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
</update>

四 trim标签

trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果,where例子的等效trim语句

1
2
3
4
5
6
7
8
9
10
11
<select id=
"getStudentListWhere" 
parameterType=
"StudentEntity" 
resultMap=
"studentResultMap"
>
 
SELECT * from STUDENT_TBL ST
 
<trim prefix=
"WHERE" 
prefixOverrides=
"AND|OR"
>
  
<
if 
test=
"studentName!=null and studentName!='' "
>
   
ST.STUDENT_NAME LIKE CONCAT(CONCAT(
'%'
, #{studentName}),
'%'
)
  
</
if
>
  
<
if 
test=
"studentSex!= null and studentSex!= '' "
>
   
AND ST.STUDENT_SEX = #{studentSex}
  
</
if
>
 
</trim>
</select>

set例子的等效trim语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<update id=
"updateStudent" 
parameterType=
"StudentEntity"
>
 
UPDATE STUDENT_TBL
 
<trim prefix=
"SET" 
suffixOverrides=
","
>
  
<
if 
test=
"studentName!=null and studentName!='' "
>
   
STUDENT_TBL.STUDENT_NAME = #{studentName},
  
</
if
>
  
<
if 
test=
"studentSex!=null and studentSex!='' "
>
   
STUDENT_TBL.STUDENT_SEX = #{studentSex},
  
</
if
>
  
<
if 
test=
"studentBirthday!=null "
>
   
STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  
</
if
>
  
<
if 
test=
"classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "
>
   
STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  
</
if
>
 
</trim>
 
WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
</update>

五 choose (when, otherwise)

有时候并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch语句,choose为switch,when为case,otherwise则为default。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<select id=
"getStudentListChooseEntity" 
parameterType=
"StudentEntity" 
resultMap=
"studentResultMap"
>
 
SELECT * from STUDENT_TBL ST
 
<where>
  
<choose>
   
<when test=
"studentName!=null and studentName!='' "
>
    
ST.STUDENT_NAME LIKE CONCAT(CONCAT(
'%'
, #{studentName}),
'%'
)
   
</when>
   
<when test=
"studentSex!= null and studentSex!= '' "
>
    
AND ST.STUDENT_SEX = #{studentSex}
   
</when>
   
<when test=
"studentBirthday!=null"
>
    
AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
   
</when>
   
<when test=
"classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "
>
    
AND ST.CLASS_ID = #{classEntity.classID}
   
</when>
   
<otherwise></otherwise>
  
</choose>
 
</where>
</select>

六 foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。List实例将使用“list”做为键,数组实例以“array”做为键。

 

1 参数为list实例的写法

SqlMapper.xml

1
2
3
4
5
6
7
<select id=
"getStudentListByClassIDs" 
resultMap=
"studentResultMap"
>
 
SELECT * FROM STUDENT_TBL ST
  
WHERE ST.CLASS_ID IN 
  
<foreach collection=
"list" 
item=
"classList"  
open=
"(" 
separator=
"," 
close=
")"
>
   
#{classList}
  
</foreach> 
</select>

Java

1
2
3
4
5
6
7
List<String> classList = 
new 
ArrayList<String>();
classList.add(
"20000002"
);
classList.add(
"20000003"
);
List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);
for
(StudentEntity entityTemp : studentList){
 
System.out.println(entityTemp.toString());
}

2 参数为Array实例的写法

 

SqlMapper.xml

1
2
3
4
5
6
7
<select id=
"getStudentListByClassIDs" 
resultMap=
"studentResultMap"
>
 
SELECT * FROM STUDENT_TBL ST
  
WHERE ST.CLASS_ID IN 
  
<foreach collection=
"array" 
item=
"ids"  
open=
"(" 
separator=
"," 
close=
")"
>
   
#{ids}
  
</foreach>
</select>

Java

1
2
3
4
5
6
7
String[] ids = 
new 
String[
2
];
ids[
0
] = 
"20000002"
;
ids[
1
] = 
"20000003"
;
List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids);
for
(StudentEntity entityTemp : studentList){
 
System.out.println(entityTemp.toString());
}

转载于:https://www.cnblogs.com/zhoushihui/p/5210425.html

你可能感兴趣的文章
Web3与智能合约交互实战(附代码)
查看>>
选择日期:年月日时分秒 插件
查看>>
从零到有的突破:BCH爱好者聚集地BCH.Club公测上线
查看>>
自建容器映像登录也行?Quay整合多种进阶功能
查看>>
如何快速打造一款高清又极速的短视频APP?
查看>>
总结sqlserver2012删除语法:delete数据表中的某一项内容
查看>>
网站建设就要像2018世界杯的俄罗斯队大杀四方[图]
查看>>
118.kubectl命令用法
查看>>
药品监管系统架构揭秘:海量溯源数据存储与查询
查看>>
父传子,关于清楚子组件提交之后页面的数据
查看>>
Java 命令行交互输入库 JLine 入门
查看>>
举个栗子看如何做MySQL 内核深度优化
查看>>
asp.net 虹软 人脸识别 实现刷脸住宿、刷脸签到、刷脸进入等
查看>>
tomcat服务器输入localhost可以访问,ip无法访问解决办法
查看>>
js 实现异步上传图片+预览
查看>>
Java 趣史-差点把 Java 命名成了 Silk(丝绸)
查看>>
死磕 java集合之ConcurrentLinkedQueue源码分析
查看>>
ubuntu安装sun jdk6
查看>>
phalapi-入门篇4(国际化高可用和自动生成文档)
查看>>
xcode报错集锦_1
查看>>