Mybatis动态SQL
2016-02-05 20:41
239 查看
1.动态SQL基本标签
•if
•choose (when, otherwise)
•trim (where, set)
•foreach
2.IF 具体用法
多个IF注意第二个if语句里的表达式
3.类似 if ,else用法例子
4.Where用法例子
6.trim用法
7.set用法注意set是sql语句中update table set.....不是将一个参数set到一个属性中
8.foreach用法
•if
•choose (when, otherwise)
•trim (where, set)
•foreach
2.IF 具体用法
<select id="XX" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select>
多个IF注意第二个if语句里的表达式
<select id="XX" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </select>
3.类似 if ,else用法例子
<select id="XX" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose>
4.Where用法例子
<select id="XX" resultType="Blog"> SELECT * FROM BLOG WHERE <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </select>
<select id="XX" resultType="Blog"> SELECT * FROM BLOG <where> <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select>
6.trim用法
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
7.set用法注意set是sql语句中update table set.....不是将一个参数set到一个属性中
<update id="updateAuthorIfNecessary"> update Author <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id} </update>
8.foreach用法
<select id="selectPostIn" resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
相关文章推荐
- httpwebrequest异步参考
- Java字节流
- AMS1117 5v 变3.5
- 算法导论例程——快速排序
- 【jQuery基础学习】11 jQuery性能简单优化
- 小蚂蚁学习数据结构(30)——图的其他知识点简介
- bootstrap之WaitForIdle&&Clear
- 关于SAE没有file_put_contents写权限的问题
- POJ 1922:Ride to School 【水】
- 快速排序
- matlab BP神经网络 机器学习 函数逼近
- Codeblocks如何调试DLL
- HDU 2151 Worm (动态规划)
- zoj 1221 Risk【最短路 3种方法】
- nyoj458小光棍数
- poj3254 Corn Fields
- ecshop 微信支付插件
- Mybatis 的日志管理
- 微信后台数据服务器
- ubuntu亮度调节失效