网络

教育改变生活

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 261|回复: 0
打印 上一主题 下一主题

MyBatis动态sql语句

[复制链接]

397

主题

398

帖子

1625

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1625
跳转到指定楼层
楼主
发表于 4 天前 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
本帖最后由 却尘 于 2024-10-18 08:57 编辑

MyBatis动态sql语句(OGNL语法)1、if
<select id="select"     resultType="Blog">  
  SELECT * FROM BLOG  WHERE state = ‘ACTIVE’  
<if test="title != null">   
  AND title like #{title}  
</if>
<if test="name!= null">   
  AND name like #{title}  
</if>
</select>
2、where
像上面的那种情况,如果where后面没有条件,然后需要直接写if判断(开头如果是 and / or 的话,会去除掉)
<select id="select"     resultType="Blog">  
SELECT * FROM BLOG  <where>      
<if test="title != null">        
AND title like #{title}      
</if>      
<if test="name!= null">        
AND name like #{title}      
</if>  
</where>
</select>
3、choose(when、otherwise)
choose 相当于java 里面的 switch 语句。otherwise(其他情况)
<select id="findActiveBlogLike"     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>
</select>
4、trim
prefix:前缀prefixoverride:去掉第一个and或者是or
select * from test
<trim prefix="WHERE" prefixoverride="AND丨OR">      
<if test="a!=null and a!=' '">AND a=#{a}<if>      
<if test="b!=null and b!=' '">AND a=#{a}<if>
</trim>

5、set
set 元素主要是用在更新操作的时候,如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。
<update id="dynamicSetTest" parameterType="Blog">     
update t_blog     
<set>          
<if test="title != null">              
title = #{title},          
</if>          
<if test="content != null">              
content = #{content},          
</if>          
<if test="owner != null">              
owner = #{owner}          
</if>     
</set>     
where id = #{id}  
</update>
6、foreach
foreach主要用在构建in条件中
<select id="dynamicForeachTest" resultType="Blog">          
select * from t_blog where id in          
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">              
#{item}          
</foreach>     
</select>  
open separator close
相当于是in (?,?,?)
如果是个map怎么办
<select id="dynamicForeach3Test" resultType="Blog">          
select * from t_blog where title like "%"#{title}"%" and id in          
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">              
#{item}          
</foreach>     
</select>  
collection对应map的键,像这样
List<Integer> ids = new ArrayList<Integer>();          
ids.add(1);          
ids.add(2);          
ids.add(3);          
ids.add(6);          
ids.add(7);          
ids.add(9);          
Map<String, Object> params = new HashMap<String, Object>();          
params.put("ids", ids);  



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

WEB前端

QQ|手机版|小黑屋|金桨网|助学堂  咨询请联系站长。

GMT+8, 2024-10-22 20:31 , Processed in 0.036423 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表