武昌做网站,wordpress 付费剧集网站,服务平台管理系统,网站过程中遇到问题可以利用动态SQL摆脱凭借SQL语句的痛苦。 MyBatis 3 大大精简了元素种类#xff0c;现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
ifchoose (when, otherwise)trim (where, set)foreach
if
动态 SQL 通常要做的事情是…可以利用动态SQL摆脱凭借SQL语句的痛苦。 MyBatis 3 大大精简了元素种类现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
ifchoose (when, otherwise)trim (where, set)foreach
if
动态 SQL 通常要做的事情是根据条件包含 where 子句的一部分。
select idfindActiveBlogWithTitleLikeresultTypeBlogSELECT * FROM BLOG WHERE state ‘ACTIVE’ if testtitle ! nullAND title like #{title}/if
/select如果希望通过“title”和“author”两个参数进行可选搜索该怎么办呢首先改变语句的名称让它更具实际意义然后只要加入另一个条件即可。
select idfindActiveBlogLikeresultTypeBlogSELECT * FROM BLOG WHERE state ‘ACTIVE’ if testtitle ! nullAND title like #{title}/ifif testauthor ! null and author.name ! nullAND author_name like #{author.name}/if
/selectchoose, when, otherwise
有点像java的switch只能选一项。 下面的例子说明要么按title查找要么按author查找。
select idfindActiveBlogLikeresultTypeBlogSELECT * FROM BLOG WHERE state ‘ACTIVE’choosewhen testtitle ! nullAND title like #{title}/whenwhen testauthor ! null and author.name ! nullAND author_name like #{author.name}/whenotherwiseAND featured 1/otherwise/choose
/selecttrim, where, set
在上面例子中如果没有一个匹配上则sql会变成
SELECT * FROM BLOG
WHERE或仅仅第二个条件匹配会变成
SELECT * FROM BLOG
WHERE
AND title like ‘someTitle’Mybatis的 where 标签 可以解决这样的问题。
select idfindActiveBlogLikeresultTypeBlogSELECT * FROM BLOG where if teststate ! nullstate #{state}/if if testtitle ! nullAND title like #{title}/ifif testauthor ! null and author.name ! nullAND author_name like #{author.name}/if/where
/selectwhere 元素只会在至少一个子素的条件下才会插入“where”子句若语句开头为“and”或“or”where元素也会将其去除。 trim 元素用来定制 where 元素的功能。
trim prefixWHERE prefixOverridesAND |OR ...
/trim类似的用于动态更新语句的解决方案叫做 set。
update idupdateAuthorIfNecessaryupdate Authorsetif testusername ! nullusername#{username},/ifif testpassword ! nullpassword#{password},/ifif testemail ! nullemail#{email},/ifif testbio ! nullbio#{bio}/if/setwhere id#{id}
/updateset 元素会动态前置 SET 关键字同时也会删掉无关的逗号。比如低四条没有时会余留逗号。
set的trim写法如下
trim prefixSET suffixOverrides,...
/trim注意这里我们删去的是后缀值同时添加了前缀值。
foreach
动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历通常是在构建 IN 条件语句的时候。
select idselectPostIn resultTypedomain.blog.PostSELECT *FROM POST PWHERE ID inforeach itemitem indexindex collectionlistopen( separator, close)#{item}/foreach
/selectforeach 元素的功能非常强大它允许你指定一个集合声明可以在元素体内使用的集合项item和索引index变量。它也允许你指定开头与结尾的字符串以及在迭代结果之间放置分隔符。 你可以将任何可迭代对象如 List、Set 等、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时index 是当前迭代的次数item 的值是本次迭代获取的元素。当使用 Map 对象或者 Map.Entry 对象的集合时index 是键item 是值。 bind
bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如
select idselectBlogsLike resultTypeBlogbind namepattern value% _parameter.getTitle() % /SELECT * FROM BLOGWHERE title LIKE #{pattern}
/select多数据库支持
一个配置了“_databaseId”变量的 databaseIdProvider 可用于动态代码中这样就可以根据不同的数据库厂商构建特定的语句。
insert idinsertselectKey keyPropertyid resultTypeint orderBEFOREif test_databaseId oracleselect seq_users.nextval from dual/ifif test_databaseId db2select nextval for seq_users from sysibm.sysdummy1/if/selectKeyinsert into users values (#{id}, #{name})
/insert动态SQL中的可插拔脚本语言
MyBatis 从 3.2 开始支持可插拔脚本语言这允许你插入一种脚本语言驱动并基于这种语言来编写动态 SQL 查询语句。 详情http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html