网站开发课程设计建议,外链百科,在喵窝网站怎么做图,零基础 网站动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架#xff0c;你应该能理解根据不同条件拼接 SQL 语句有多痛苦#xff0c;例如拼接时要确保不能忘记添加必要的空格#xff0c;还要注意去掉列表最后一个列名的逗号。利用动态 SQL#xff0c;可以彻底…动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架你应该能理解根据不同条件拼接 SQL 语句有多痛苦例如拼接时要确保不能忘记添加必要的空格还要注意去掉列表最后一个列名的逗号。利用动态 SQL可以彻底摆脱这种痛苦。
if
choose (when, otherwise)
trim (where, set)
foreach1 if
使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分。比如
select idfindActiveBlogWithTitleLike resultTypeBlogSELECT * FROM BLOGWHERE state ‘ACTIVE’if testtitle ! nullAND title like #{title}/if
/select这条语句提供了可选的查找文本功能。如果不传入 “title”那么所有处于 “ACTIVE” 状态的 BLOG 都会返回如果传入了 “title” 参数那么就会对 “title” 一列进行模糊查找并返回对应的 BLOG 结果细心的读者可能会发现“title” 的参数值需要包含查找掩码或通配符字符。
如果希望通过 “title” 和 “author” 两个参数进行可选搜索该怎么办呢首先我想先将语句名称修改成更名副其实的名称接下来只需要加入另一个条件即可。
select idfindActiveBlogLike resultTypeBlogSELECT * FROM BLOG WHERE state ‘ACTIVE’if testtitle ! nullAND title like #{title}/ifif testauthor ! null and author.name ! nullAND author_name like #{author.name}/if
/select2 choose、when、otherwise
有时候我们不想使用所有的条件而只是想从多个条件中选择一个使用。针对这种情况MyBatis 提供了 choose 元素它有点像 Java 中的 switch 语句。
还是上面的例子但是策略变为传入了 “title” 就按 “title” 查找传入了 “author” 就按 “author” 查找的情形。若两者都没有传入就返回标记为 featured 的 BLOG这可能是管理员认为与其返回大量的无意义随机 Blog还不如返回一些由管理员精选的 Blog。
select idfindActiveBlogLike resultTypeBlogSELECT * 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
/select3 trim、where、set
前面几个例子已经方便地解决了一个臭名昭著的动态 SQL 问题。现在回到之前的 “if” 示例这次我们将 “state ‘ACTIVE’” 设置成动态条件看看会发生什么。
select idfindActiveBlogLike resultTypeBlogSELECT * FROM BLOGWHEREif teststate ! nullstate #{state}/ifif testtitle ! nullAND title like #{title}/ifif testauthor ! null and author.name ! nullAND author_name like #{author.name}/if
/select如果没有匹配的条件会怎么样最终这条 SQL 会变成这样
SELECT * FROM BLOG
WHERE这会导致查询失败。如果匹配的只是第二个条件又会怎样这条 SQL 会是这样:
SELECT * FROM BLOG
WHERE
AND title like ‘someTitle’这个查询也会失败。这个问题不能简单地用条件元素来解决。这个问题是如此的难以解决以至于解决过的人不会再想碰到这种问题。
MyBatis 有一个简单且适合大多数场景的解决办法。而在其他场景中可以对其进行自定义以符合需求。而这只需要一处简单的改动
select idfindActiveBlogLike resultTypeBlogSELECT * FROM BLOGwhereif teststate ! nullstate #{state}/ifif testtitle ! nullAND title like #{title}/ifif testauthor ! null and author.name ! nullAND author_name like #{author.name}/if/where
/selectwhere 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且若子句的开头为 “AND” 或 “OR”where 元素也会将它们去除。
如果 where 元素与你期望的不太一样你也可以通过自定义 trim 元素来定制 where 元素的功能。比如和 where 元素等价的自定义 trim 元素为 update idupdateBatch parameterTypejava.util.List!--mbg.generated--update bs_factory_calendartrim prefixset suffixOverrides,trim prefixSET_TIME case suffixend,foreach collectionlist indexindex itemitemwhen FACTORY_CALENDAR_ID #{item.factoryCalendarId,jdbcTypeVARCHAR} then #{item.setTime,jdbcTypeVARCHAR}/foreach/trimtrim prefixWEEK case suffixend,foreach collectionlist indexindex itemitemwhen FACTORY_CALENDAR_ID #{item.factoryCalendarId,jdbcTypeVARCHAR} then #{item.week,jdbcTypeVARCHAR}/foreach/trim/trimwhere FACTORY_CALENDAR_ID inforeach close) collectionlist itemitem open( separator, #{item.factoryCalendarId,jdbcTypeVARCHAR}/foreach/update insert idinsertOrUpdateSelective parameterTypecom.inspur.spring.pojo.BsFactoryCalendar!--mbg.generated--insert into bs_factory_calendartrim prefix( suffix) suffixOverrides,if testfactoryCalendarId ! nullFACTORY_CALENDAR_ID,/ifif testsetTime ! nullSET_TIME,/if/trimvaluestrim prefix( suffix) suffixOverrides,if testfactoryCalendarId ! null#{factoryCalendarId,jdbcTypeVARCHAR},/ifif testsetTime ! null#{setTime,jdbcTypeVARCHAR},/if/trimon duplicate key updatetrim suffixOverrides,if testfactoryCalendarId ! nullFACTORY_CALENDAR_ID #{factoryCalendarId,jdbcTypeVARCHAR},/ifif testsetTime ! nullSET_TIME #{setTime,jdbcTypeVARCHAR},/if/trim/insert update idupdateByPrimaryKeySelective parameterTypecom.inspur.spring.pojo.BsFactoryCalendar!--mbg.generated--update bs_factory_calendarsetif testupdateTime ! nullUPDATE_TIME #{updateTime,jdbcTypeTIMESTAMP},/ifif testcomId ! nullCOM_ID #{comId,jdbcTypeVARCHAR},/if/setwhere FACTORY_CALENDAR_ID #{factoryCalendarId,jdbcTypeVARCHAR}/update
4 foreach
select idselectPostIn resultTypedomain.blog.PostSELECT *FROM POST Pwhereforeach itemitem indexindex collectionlistopenID in ( separator, close) nullabletrue#{item}/foreach/where
/select