Mybatis-动态Sql

mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

1.if

<select id="findUserList" parameterType="user" resultType="user">
    select * from user 
    where 1=1 
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="username!=null and username!=''">
        and username like '%${username}%'
    </if>
</select>

2.where

上边的sql也可以改为:

<select id="findUserList" parameterType="user" resultType="user">
		select * from user 
		<where>
		<if test="id!=null and id!=''">
		and id=#{id}
		</if>
		<if test="username!=null and username!=''">
		and username like '%${username}%'
		</if>
		</where>
	</select>

注意: <where />可以自动处理第一个and。or

3.set

 

	<!-- 修改用户 -->
	<update id="updateUser" parameterType="org.csmf.mybatis.entity.User">
		update t_user 
		<set>
			<if test="name!=null and name !='' ">
				username =#{name},
			</if>
			<if test="password !=null and password !='' ">
				password =#{password},
			</if>
			<if test="sex!=null and sex !='' ">
				sex =#{sex},
			</if>
			<if test="brithday!=null and brithday !='' ">
				brithday =#{brithday},
			</if>
			<if test="address!=null and address !='' ">
				address =#{address},
			</if>
		</set>
		<where>
			<if test="id!=null and id!=''">
				id = #{id}
			</if>
		</where>
	</update>

<set>可以处理最后一个逗号

4.foreach

向sql传递List,mybatis使用foreach解析.

现在需要通过一组ID查询用户,该sql语句有两种

 select * from t_user where id =1 or id=2 or id=3 or id=4;
 select * from t_user where id in (1,2,3,4);

那么对应的Sql映射文件中的sql语句也有两种写法:

第一种:

UserMapper接口:

public List<User> findUserById(List<Integer> ids) throws Exception

 Sql映射文件:

	<!-- 根据一组Id查询用户 -->
	<select id="findUserByIds"  resultType="User" parameterType="QueryVo">
		select * from t_user 
		<where>
			<!--select * from t_user where id=? or id=? or =? ...  -->
			<foreach collection="list" item="id" open="and ( " close=")" separator="or">
				id = #{id}
			</foreach> 
		</where>
	</select>

注意:

<foreach>标签中collection属性不是写参数名,而是写list/array: List集合的参数写: list, 数组类型的参数写: arra

 

5.sql片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:

<!--综合查询用户信息 -->
    <select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        <where>
        <if test="id!=null and id!=''">
        and id=#{id}
        </if>
        <if test="username!=null and username!=''">
        and username like '%${username}%'
        </if>
        </where>
    </select>

我们可以把上面的sql的条件单独抽取出来,方便其他的sql复用:

<!--将where条件抽取出来:-->
<sql id="query_user_where">
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="username!=null and username!=''">
        and username like '%${username}%'
    </if>
</sql>

然后在其他需要使用到该sql片段的sql中使用<include>标签引入

<select id="findUserList" parameterType="user" resultType="user">
    select * from user 
    <where>
        <include refid="query_user_where"/>
    </where>
</select>

注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:

<include refid="namespace.sql片段”/>

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值