网站设计中怎么显示链接内容,厦门网站seo外包,做美容行业的网站哪个好,手机在线制作网站目录
动态sql
if
where
trim
foreach
choose、when、otherwise
set
bind
sql
MyBatis常用OGNL表达式 动态sql
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架#xff0c;你应该能理解根据不同条件拼接 SQL 语句有多痛苦#xff0c;例如…目录
动态sql
if
where
trim
foreach
choose、when、otherwise
set
bind
sql
MyBatis常用OGNL表达式 动态sql
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架你应该能理解根据不同条件拼接 SQL 语句有多痛苦例如拼接时要确保不能忘记添加必要的空格还要注意去掉列表最后一个列名的逗号。利用动态 SQL可以彻底摆脱这种痛苦。
使用动态 SQL 并非一件易事但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言MyBatis 显著地提升了这一特性的易用性。
如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式MyBatis 3 替换了之前的大部分元素大大精简了元素种类现在要学习的元素种类比原来的一半还要少。
ifchoose (when, otherwise)trim (where, set)foreachbindsql片段
if
select idgetEmpByCondition resultTypecn.tulingxueyuan.bean.Empselect * from emp where if testempno!nullempno #{empno} and/ifif testename!nullename like #{ename} and/ifif testsal!nullsal #{sal}/if
/select
where
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且若子句的开头为 “AND” 或 “OR”where 元素也会将它们去除。
select idgetEmpByCondition resultTypecn.tulingxueyuan.bean.Empselect * from empwhereif testempno!nullempno #{empno}/ifif testename!nulland ename like #{ename}/ifif testsal!nulland sal #{sal}/if/where/select
现在看起来没有什么问题了但是我们的条件添加到了拼接sql语句的前后那么我们该如何处理呢
trim
!--trim截取字符串prefix前缀为sql整体添加一个前缀prefixOverrides:去除整体字符串前面多余的字符suffixOverrides:去除后面多余的字符串--select idgetEmpByCondition resultTypecn.tulingxueyuan.bean.Empselect * from emptrim prefixwhere prefixOverridesand suffixOverridesandif testempno!nullempno #{empno} and/ifif testename!nullename like #{ename} and/ifif testsal!nullsal #{sal} and/if/trim/select
foreach
动态 SQL 的另一个常见使用场景是对集合进行遍历尤其是在构建 IN 条件语句的时候。
!--foreach是对集合进行遍历collectiondeptnos 指定要遍历的集合close 表示以什么结束index 给定一个索引值item 遍历的每一个元素的值open 表示以什么开始separator 表示多个元素的分隔符--select idgetEmpByDeptnos resultTypeEmpselect * from emp where deptno inforeach collectiondeptnos close) indexidx itemdeptno open( separator,#{deptno}/foreach/select
choose、when、otherwise
有时候我们不想使用所有的条件而只是想从多个条件中选择一个使用。针对这种情况MyBatis 提供了 choose 元素它有点像 Java 中的 switch 语句。
select idgetEmpByConditionChoose resultTypecn.tulingxueyuan.bean.Empselect * from empwherechoosewhen testempno!nullempno #{empno}/whenwhen testename!nullename like #{ename}/whenwhen testsal!nullsal #{sal}/whenotherwise11/otherwise/choose/where/select
set
用于动态更新语句的类似解决方案叫做 set。set 元素可以用于动态包含需要更新的列忽略其它不更新的列。
update idupdateEmpByEmpnoupdate empsetif testempno!nullempno#{empno},/ifif testename!nullename #{ename},/ifif testsal!nullsal #{sal}/if/setwhereempno #{empno}/where
/update
bind
bind 元素允许你在 OGNL 表达式以外创建一个变量并将其绑定到当前的上下文。比如
select idselectBlogsLike resultTypeBlogbind namepattern value% _parameter.getTitle() % /SELECT * FROM BLOGWHERE title LIKE #{pattern}
/select
sql
这个元素可以用来定义可重用的 SQL 代码片段以便在其它语句中使用。 参数可以静态地在加载的时候确定下来并且可以在不同的 include 元素中定义不同的参数值。比如
sql iduserColumns ${alias}.id,${alias}.username,${alias}.password /sql
这个 SQL 片段可以在其它语句中使用例如
select idselectUsers resultTypemapselectinclude refiduserColumnsproperty namealias valuet1//include,include refiduserColumnsproperty namealias valuet2//includefrom some_table t1cross join some_table t2
/select
MyBatis常用OGNL表达式
e1 or e2
e1 and e2
e1 e2,e1 eq e2
e1 ! e2,e1 neq e2
e1 lt e2小于
e1 lte e2小于等于其他gt大于,gte大于等于
e1 in e2
e1 not in e2
e1 e2,e1 * e2,e1/e2,e1 - e2,e1%e2
!e,not e非求反
e.method(args)调用对象方法
e.property对象属性值
e1[ e2 ]按索引取值List,数组和Map
classmethod(args)调用类的静态方法
classfield调用类的静态字段值