原文:http://blog.csdn.net/shan9liang/article/details/40452375
先简单说下Mybatis的动态sql,这不是今天的重点。
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。
例如,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息
MyBatis中用于实现动态SQL的元素主要有:
- if
- choose(when,otherwise)
- trim
- where
- set
- foreach
<select id="findActiveBlogLike" parameterType="BLOG" resultType="BLOG"> SELECT * FROM BLOG WHERE <trim prefix="WHERE" prefixOverrides="AND |OR "> <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND title like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </trim> </select> <update id="updateAuthorIfNecessary" parameterType="Author"> update Author <trim prefix="where" prefixOverrides=","> <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email}</if> </set> where id=#{id} </trim> </update>
翻阅mybatis文档,在一个不起眼的地方发现update标签有一个属性statementType,根据jdbc的经验,这应该是控制sql预编译还是非预编译的,如果sql执行是预编译的,那么动态传入字段名,表名之类的,显然
是不行的,所以你必须改成非预编译的。
两者有什么区别呢?如果是预编译的,那么系统在初始化时就会读取这段sql代码,将指定的实体类中的字段替换了类似#{}这样的语句,就是形成了类似这样的语句:
"select * from tableName where id=?" 这个时候你在系统运行时再想向这句sql中替换tableName或者id,结果可想而知。如果是非预编译呢,结果刚好相反,他会在系统运行时才会去生成这样类似的语句。此时就可以去替换这些动态的字段或者表名之类。这样在结合之前所讲的返回类型的设置,我们的问题就解决了。
我们可以不用设定参数和返回类型的实体类,只需要形成一个动态的表名和字段名的列表类。就可以动态对那些未知的物理模型进行操作.如下代码可作参考:
<select id="queryMetaList" resultType="Map" statementType="STATEMENT"> select * from ${tableName} t where <foreach item="item" index="index" collection="field" open=" " separator="and" close=" "> <choose> <when test="item.fieldType == 'DATE' and item.dateQueryFlag == 0"> ${item.fieldCode} between to_date('${item.fieldValue}','yyyy-mm-dd hh24:mi:ss') </when> <when test="item.fieldType == 'DATE' and item.dateQueryFlag == 1"> to_date('${item.fieldValue}','yyyy-mm-dd hh24:mi:ss') </when> <when test="item.fieldItemCode != null and item.fieldItemCode != ''"> ${item.fieldCode} = '${item.fieldItemCode}' </when> <otherwise> ${item.fieldCode} = '${item.fieldValue}' </otherwise> </choose> </foreach> </select>