各位用户为了找寻关于Mybatis中的动态SQL语句解析的资料费劲了很多周折。这里教程网为您整理了关于Mybatis中的动态SQL语句解析的相关资料,仅供查阅,以下为您介绍关于Mybatis中的动态SQL语句解析的详细内容
这篇文章主要介绍了Mybatis中的动态SQL语句解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Mybatis中配置SQL有两种方式,一种是利用xml 方式进行配置,一种是利用注解进行配置。
Mybatis使用注解配置SQL,但是由于配置功能受限,而且对于复杂的SQL而言可读性很差,所以很少使用。
Mybatis常用xml配置的方式,使用xml的几个简单的元素,便能完成动态SQL的功能,大量的判断都可以在mybaties的映射xml里面配置,以达到许多需要大量代码才能实现的功能,大大减少了代码量,体现了Mybatis的灵活、高度可配置性和维护性。
元素 作用 备注 if 判断语句 单条件分支判断 choose(when,otherwise) 相当于Java中的switch和case语句 多条件分支判断 trim 辅助元素,用于处理特定的SQL拼装问题 用于处理SQL拼装的问题 foreach 循环语句 在in语句等列表条件常用
if元素
if元素是最常用的判断语句,相当于Java中国的 if 语句,它常常与test属性联合使用。
? 1 2 3 4 5 6<
select
id=
"findRole1"
parameterType=
"string"
resultMap=
"roleResultMap"
>
select
role_no, role_name, note
from
t_role
where
1=1
<if test=
"roleName != null and roleName !=''"
>
and
role_name
like
concat(
'%'
, #{roleName},
'%'
)
</if>
</
select
>
当参数roleName传递进映射器时,如果参数不为空,则采取构造对 roleName 的模糊查询,否则就不要去构造这个条件。通过Mybaties的 if 元素节省了许多拼接SQL的工作,集中在 xml 里面维护。
choose、when、otherwise元素
如果在判断时有更多的选择,不只是两种选择,也就是类似switch...case...default...功能的语句。在映射的SQL语句中,使用choose、when、otherwise元素承担这个功能。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15<
select
id=
"findRole2"
parameterType=
"role"
resultMap=
"roleResultMap"
>
select
role_no, role_name, note
from
t_role
where
1=1
<choose>
<
when
test=
"roleNo != null and roleNo !=''"
>
AND
role_no = #{roleNo}
</
when
>
<
when
test=
"roleName != null and roleName !=''"
>
AND
role_name
like
concat(
'%'
, #{roleName},
'%'
)
</
when
>
<otherwise>
AND
note
is
not
null
</otherwise>
</choose>
</
select
>
上述的场景就是:
首先,如果角色编号不为空,则只用角色编号作为条件查询。
当角色编号为空,而角色名称不为空,则使用角色名称作为条件进行模糊查询。
当角色编号和角色编号都为空,则要求角色备注不为空。
trim、where、set元素
在前面的SQL语句中加入了“1=1”,这样可以实现其功能,但是有一个更好的实现,那就是使用where。当where元素里面的条件成立时,才会加入where这个SQL关键字到组装的SQL里面,否则不会加入。
? 1 2 3 4 5 6 7 8 9 10 11<
select
id=
"findRole3"
parameterType=
"role"
resultMap=
"roleResultMap"
>
select
role_no, role_name, note
from
t_role
<
where
>
<if test=
"roleName != null and roleName !=''"
>
and
role_name
like
concat(
'%'
, #{roleName},
'%'
)
</if>
<if test=
"note != null and note !=''"
>
and
note
like
concat(
'%'
, #{note},
'%'
)
</if>
</
where
>
</
select
>
有时需要去掉一些特殊的SQL语法,比如常见的and、or等。使用trim元素也可以达到预期效果。其中prefix代表的语句的前缀,prefixOverrides代表的是需要去掉哪种字符串。与前面的where语句是等效的。
? 1 2 3 4 5 6 7 8<
select
id=
"findRole4"
parameterType=
"string"
resultMap=
"roleResultMap"
>
select
role_no, role_name, note
from
t_role
<trim prefix=
"where"
prefixOverrides=
"and"
>
<if test=
"roleName != null and roleName !=''"
>
and
role_name
like
concat(
'%'
, #{roleName},
'%'
)
</if>
</trim>
</
select
>
在hibernate中如果因为更新某一个字段而不得已发送所有的字段给持久化对象,这样影响了SQL语句的执行效率。最佳的方法是把主键和更新字段的值传递给SQL去更新。set元素就可以实现此功能。set元素遇到逗号,它会自动将对应的逗号去掉。
? 1 2 3 4 5 6 7 8 9 10 11 12<
update
id=
"updateRole"
parameterType=
"role"
>
update
t_role
<
set
>
<if test=
"roleName != null and roleName !=''"
>
role_name = #{roleName},
</if>
<if test=
"note != null and note != ''"
>
note = #{note}
</if>
</
set
>
where
role_no = #{roleNo}
</
update
>
foreach元素
foreach元素是一个循环语句,它的作用是遍历集合,它能很好的支持数组和List、Set接口的集合,对此提供遍历的功能,它往往用于SQL中的in关键字。
? 1 2 3 4 5 6 7<
select
id=
"findRoleByNums"
resultMap=
"roleResultMap"
>
select
role_no, role_name, note
from
t_role
where
role_no
in
<foreach item=
"roleNo"
index
=
"index"
collection=
"roleNoList"
open
=
"("
separator=
","
close
=
")"
>
#{roleNo}
</foreach>
</
select
>
collection配置的roleNoList是传递进来的参数名称,它可以是一个数组、List、Set等集合。
item配置的是循环中当前的元素。
index配置的是当前元素在集合的位置下标。
open和close配置的是以什么符号将这些集合元素包装起来。
separator是各个元素的分隔符。
用test的属性判断字符串
test用于条件判断语句,其作用相当于判断真假,在大多数场景下,主要用于判断空和非空的。
? 1 2 3 4 5 6<
select
id=
"getRoleTest"
parameterType=
"string"
resultMap=
"roleResultMap"
>
select
role_no, role_name, note
from
t_role
<if test=
"type == 'Y'.toString()"
>
where
1=1
</if>
</
select
>
如果把 type='Y'传递给SQL,就可以实现Mybatis加入了条件 where 1=1,所以对于字符串的判断,可以加入 toString ()的方法进行比较。
bind元素
bind元素的作用是通过OGNL表达式去定义一个上下文变量,这样更方便使用。
例如在模糊查询时,如果是MySQL数据库,常常用到一个concat,它用 % 和 参数相连。然而在Oracle数据库中则没有,Oracle数据库用连接符号是 “||”,这样SQL就需要两种形式去实现,用bind元素,就不用使用数据库语言。
? 1 2 3 4 5<
select
id=
"findRole5"
parameterType=
"string"
resultMap=
"roleResultMap"
>
<bind
name
=
"pattern"
value=
"'%' + _parameter + '%'"
/>
SELECT
role_no, role_name, note
FROM
t_role
where
role_name
like
#{pattern}
</
select
>
以上就是我在学习过程中对于Mybatis中的动态SQL语句的常见知识点总结,希望大家可以一起学习进步!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://www.cnblogs.com/Demrystv/p/9208777.html