Mybatis FAQ

来源:互联网 发布:天津淘宝商会会长 编辑:程序博客网 时间:2024/06/05 18:33

返回null

Mybatis在查询返回的所有列均为空时,默认返回null,而不是属性为空的对象,mybatis-3.4.2 中新增了一个选项returnInstanceForEmptyRow,可以控制返回null还是返回属性为空的对象。

参考:Pull Request #800

分号问题

若在mapper.xml文件中的sql语句结尾加分号,oracle会报ORA-00911: invalid character错误。mysql中并不会报错。另外若将myql连接中添加属性allowMultiQueries=true,还允许一次执行多个操作,各statement间用分号分隔。

allowMultiQueries

Allow the use of ‘;’ to delimit multiple queries during one statement (true/false), defaults to ‘false’, and does not affect the addBatch() and executeBatch() methods, which instead rely on rewriteBatchStatements.

Default: false

Since version: 3.1.1

—— reference

OGNL运算符

mybatis动态sql支持基于ognl的表达式,有可能发生表达式中的业务字段与ognl运算符冲突的情况。个人曾碰到过业务字段“审核人(shr)”与ognl右移位运算符“shr(>>)”冲突导致错误。

参考:
1. OGNL - Wikipedia
2. apache commons-ognl language guide

text symbol desc or || Logical or operator bor | Bitwise or operator xor ^ Bitwise exclusive-or operator and && Logical and operator band & Bitwise and operator not ! Logical not eq = Equality test neq != Inequality test lt < Less than comparison lte <= Less than or equals comparison gt > Greater than comparison gte >= Greater than or equals comparison in = List membership comparison not in = Equality test shl << Bit shift left shr >> Bit shift right ushr >>> Logical shift right eq = Equality test eq = Equality test

if 判断的坑

当 if 判断中比较的值是字符串且只有一个字符时,该字符串一定要使用双引号括起来,因为如果使用单引号,ognl会将其解析为Char而不是String,导致达不到想要的效果。
如,要使用<if test = 'type == "y"'> 而不要使用 <if test = "type == 'y'">

参考:IF判断的坑

原创粉丝点击