mybatis中的动态sql
来源:互联网 发布:方便面 数据报告 编辑:程序博客网 时间:2024/05/01 04:00
mybatis中的动态sql:
1、
<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容
举例 :
下面是一个往购物车表中插入数据的mybatis语句
<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">
insert into cart
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userId != null">
user_id,
</if>
<if test="dealId != null">
deal_id,
</if>
<if test="dealSkuId != null">
deal_sku_id,
</if>
<if test="count != null">
count,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
<if test="dealId != null">
#{dealId,jdbcType=BIGINT},
</if>
<if test="dealSkuId != null">
#{dealSkuId,jdbcType=BIGINT},
</if>
<if test="count != null">
#{count,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
suffixOverrides = "," 代指去除了最后一个逗号
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
假设没有指定
suffixOverrides=","
执行的sql语句也许是这样的:insert into cart (id,user_id,deal_id,) values(1,2,1,);显然是错误的
指定之后语句就会变成insert into cart (id,user_id,deal_id) values(1,2,1);这样就将“,”去掉了。
前缀也是一个道理这里就不说了。
注意: 如果需要前缀或者后缀去除多个,需要使用管道 |
suffixOverrides=",|;"
2、if
<select id="findActiveBlogLike"
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>
在使用if的时候,需要注意
SELECT * FROM BLOG
WHERE
或者
SELECT * FROM BLOG
WHERE
AND title like ‘someTitle’
这种情况,所以一般都会加上
1=1这种恒成立的方法
3、choose (when, 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>
也可以根据查询语句中传入进来的参数进行判断,组成不同的sql语句
<select id="findMyTopicList" resultMap="BaseResultMap" parameterType="com.pa.market.common.vo.TopicListVO">
select
<include refid="Base_Column_List"/>
from gsaledata.T_FORUM_TOPIC
where CREATION_BY =#{creationBy,jdbcType=VARCHAR}
<choose>
<when test="sortByTime == 'asc'">
order by CREATION_DATE asc
</when>
<otherwise>
order by CREATION_DATE desc
</otherwise>
</choose>
</select>
sortByTime 这个变量是通过接口传入进来的参数。
接口如下:其中sortByTime是TopicListVO中的属性值
List<ForumTopic> findMyTopicList(TopicListVO topicListVO);
4、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>
update tablename
set tablecolum.age = 10 , tablecolum.name = "c"
where id = #{id}
需要指定where查询条件,不然会更新表中所有的数据。
<update id="updateAuthorIfNecessary">
update Author
<trim prefix="set" suffixOverrides=",">
<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>
</trim>
where id=#{id}
</update>
5、foreach
一般都是使用在in的集合条件中。
<!-- 批量修改推送表报案状态 -->
<update id="modifyStatus" parameterType="java.util.List">
<foreach collection="list" index="index" item="item" open="begin" close=";end;" separator=";">
update gsaledata.t_push_record_customer_info p
<set>
p.push_record_status= 'SUCCESS',
<if test="item.lastUpdateDate != null" >
p.LAST_UPDATE_DATE = #{item.lastUpdateDate},
</if>
<if test="item.lastUpdatedBy != null" >
p.LAST_UPDATED_BY = #{item.lastUpdatedBy}
</if>
</set>
where p.business_id = #{item.businessId} and p.user_id = #{l.userId}
</foreach>
</update>
批量更新,直接在sql中一次性全部更新,通过begin和end分开sql执行的块。
还有一种方法是,在代码中for循环多次update语句。
<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>
1、
<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
prefix:在trim标签内sql语句加上前缀。
suffix:在trim标签内sql语句加上后缀。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容
举例 :
下面是一个往购物车表中插入数据的mybatis语句
<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">
insert into cart
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userId != null">
user_id,
</if>
<if test="dealId != null">
deal_id,
</if>
<if test="dealSkuId != null">
deal_sku_id,
</if>
<if test="count != null">
count,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
<if test="dealId != null">
#{dealId,jdbcType=BIGINT},
</if>
<if test="dealSkuId != null">
#{dealSkuId,jdbcType=BIGINT},
</if>
<if test="count != null">
#{count,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
suffixOverrides = "," 代指去除了最后一个逗号
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
假设没有指定
suffixOverrides=","
执行的sql语句也许是这样的:insert into cart (id,user_id,deal_id,) values(1,2,1,);显然是错误的
指定之后语句就会变成insert into cart (id,user_id,deal_id) values(1,2,1);这样就将“,”去掉了。
前缀也是一个道理这里就不说了。
注意: 如果需要前缀或者后缀去除多个,需要使用管道 |
suffixOverrides=",|;"
2、if
<select id="findActiveBlogLike"
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>
在使用if的时候,需要注意
SELECT * FROM BLOG
WHERE
或者
SELECT * FROM BLOG
WHERE
AND title like ‘someTitle’
这种情况,所以一般都会加上
1=1这种恒成立的方法
3、choose (when, 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>
也可以根据查询语句中传入进来的参数进行判断,组成不同的sql语句
<select id="findMyTopicList" resultMap="BaseResultMap" parameterType="com.pa.market.common.vo.TopicListVO">
select
<include refid="Base_Column_List"/>
from gsaledata.T_FORUM_TOPIC
where CREATION_BY =#{creationBy,jdbcType=VARCHAR}
<choose>
<when test="sortByTime == 'asc'">
order by CREATION_DATE asc
</when>
<otherwise>
order by CREATION_DATE desc
</otherwise>
</choose>
</select>
sortByTime 这个变量是通过接口传入进来的参数。
接口如下:其中sortByTime是TopicListVO中的属性值
List<ForumTopic> findMyTopicList(TopicListVO topicListVO);
4、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>
update tablename
set tablecolum.age = 10 , tablecolum.name = "c"
where id = #{id}
需要指定where查询条件,不然会更新表中所有的数据。
<update id="updateAuthorIfNecessary">
update Author
<trim prefix="set" suffixOverrides=",">
<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>
</trim>
where id=#{id}
</update>
5、foreach
一般都是使用在in的集合条件中。
<!-- 批量修改推送表报案状态 -->
<update id="modifyStatus" parameterType="java.util.List">
<foreach collection="list" index="index" item="item" open="begin" close=";end;" separator=";">
update gsaledata.t_push_record_customer_info p
<set>
p.push_record_status= 'SUCCESS',
<if test="item.lastUpdateDate != null" >
p.LAST_UPDATE_DATE = #{item.lastUpdateDate},
</if>
<if test="item.lastUpdatedBy != null" >
p.LAST_UPDATED_BY = #{item.lastUpdatedBy}
</if>
</set>
where p.business_id = #{item.businessId} and p.user_id = #{l.userId}
</foreach>
</update>
批量更新,直接在sql中一次性全部更新,通过begin和end分开sql执行的块。
还有一种方法是,在代码中for循环多次update语句。
<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>
阅读全文
0 0
- mybatis中的动态sql
- MyBatis中的动态SQL
- mybatis中的动态SQL
- MyBatis中的动态SQL
- Mybatis中的动态Sql
- MyBatis中的动态SQL
- mybatis中的动态sql
- mybatis中的动态sql
- MyBatis中的动态SQL例子
- 【Mybatis学习】Mybatis框架中的动态sql
- mybatis--动态sql中的where语句
- Mybatis中的update动态SQL语句
- Mybatis中的update动态SQL语句
- JAVA mybatis:mapper 中的动态SQL
- Mybatis中的update动态SQL语句
- 【mybatis深度历险系列】mybatis中的动态sql
- ssm框架学习---mybatis中动态sql中的sql片段
- MyBatis动态SQL使用,传入参数Map中的Key判断
- 2017.12.21
- JSP学习心得之一:JSP开发环境下载与配置
- es6浅学-let和const
- 数据结构课程设计——学生成绩管理系统
- Android 异步回调中操作UI线程,UI同步、卡死阻塞等性能问题
- mybatis中的动态sql
- 弘智教育-效果图10-建模最后一节课
- 【七月Python入门】 第五课面文件访问与函数式编程入门
- 打造浪漫的Android表白程序
- pandas—Dataframe操作(选取 插入 删除)
- 存储器层次结构1
- LDD3 study note 3
- 把android中图片局部区域改为透明
- C++ 双向链表简单实现通讯录