常用标签:<if>、<where>、<trim>、<foreach>
作用 :输出 where 语句,可以智能的过滤掉条件中多出来的 and 或者or。若条件都不满足,则会查出所有记录。
作用 :用于添加 SQL 语句的前缀或者后缀
所以可利用 <trim>来代替 <where>的功能
作用 :用于在 SQL 语句中迭代一个集合,可用在构建 in 条件中
Sql释义:根据 id 值的不同,设置对应的状态值和创建时间
参考链接: http://c.biancheng.net/view/4378.html
<trim>元素的主要作用是可以在自己添加的内容前添加某些前缀,内容之后添加某些后缀,对应的元素为:前缀 prefix 后缀 suffix.
prefixOverrides 把某些首部的元素进行覆盖,suffixOverrides 把某些尾部的元素进行覆盖
(1)使用案例 替代where
类似于sql: select * from users where name = ? and address =?
(2)替代set
打印sql: update user set name="xxx" ,gender="yyy" where id ="w"
suffixoverride="," 意思是重写最后一个逗号 suffix意思是后缀
(3)插入insert
执行sql: insert into sys_user_role(id,user_id,role_id) values(id,userId,roleId)
[html]view
plain
copy
如果
userqueryvo中传入查询条件,再进行sql拼接
test中usercustom.username表示从userqueryvo读取属性值
and
username
like
'%${usercustom.username}%'
and
sex
=
#{usercustom.sex}
根据id集合查询用户信息
最终拼接的效果:
select
id
,username
,birthday
from
user
where
username
like
'%小明%'
and
id
in
(16,22,25)
collection:集合的属性
open:开始循环拼接的串
close:结束循环拼接的串
item:每次循环取到的对象
separator:每两次循环中间拼接的串
#{id}
select
id
,username
,birthday
from
user
where
username
like
'%小明%'
and
(id
=
16
or
id
=
22
or
id
=
25)
<foreach
collection="ids"
open="
and
(
"
close=")"
item="id"
separator="or">
id
=
#{id}
还有很的查询条件
[html]
view
plain
copy
select
id,username,birthday
from
user
where标签相当
于where关键字,可以自动去除第一个and
引用sql片段,如果sql片段和引用处不在同一个mapper必须前边加namespace
下边还有很其它的条件
<include
refid="其它的sql片段">
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)