mybatis配置文件动态sql简化开发

在mybatis跟实体类的映射sql文件中,有时候好多重复的sql语句,那么我们怎么简化少些这些语句?如下中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- usermapper.xml对用user表的操作 -->
<!-- namespace:用于隔离sql语句 -->
<!-- namesapce是类路径名 -->
<mapper namespace="com.hpe.mapper.UserMapper">

	<!-- 当条件表达式条件为真,才会拼接上if中的sql -->
	<select id="findUserByUser" parameterType="User" resultType="User">
		select* from user
		<where><!-- 自动去掉第一个and -->
		<if test="username!=null and username!=''">
			and username like '%${username}%'
		</if>
		<if test="sex!=null and sex!=''">
			and sex=#{sex}
		</if>
		</where>
	</select>

	<select id="findUserByUser1" parameterType="User" resultType="User">
		select* from user
		<where><!-- 自动去掉第一个and -->
		<if test="username!=null and username!=''">
			and username like '%${username}%'
		</if>

		<if test="sex!=null and sex!=''">
			and sex=#{sex}
		</if>
			<!-- <if test="ids!=null and ids.size()>0"> and id in (<foreach collection="ids" 
				item="item" separator=","> #{item} </foreach> ) </if> -->
			<if test="ids!=null and ids.size()>0">
				and (
				<foreach collection="ids" item="item" separator="or">
					id=#{item}
				</foreach>
				)
			</if>
		</where>

	</select>

	<select id="findUserCountByUser" parameterType="User"
		resultType="int">
		select count(*) from user
		<where><!-- where ognl标签自动去掉第一个and -->
		<if test="username!=null and username!=''">
			and username like '%${username}%'
		</if>
		<if test="sex!=null and sex!=''">
			and sex=#{sex}
		</if>
		</where>
	</select>
</mapper>

可见每个sql标签中,都有一段共同得sql语句

<if test="username!=null and username!=''">
			and username like '%${username}%'
		</if>
		<if test="sex!=null and sex!=''">
			and sex=#{sex}
</if>

这样一来重复的代码量很大,那么我们把这段公共代码提取出来,放在<sql>标签中,并给个id,以便在引用来减少代码量

<sql id="query_user">
		<if test="username!=null and username!=''">
			and username like '%${username}%'
		</if>

		<if test="sex!=null and sex!=''">
			and sex=#{sex}
		</if>
</sql>

那么,整体代码就可以写成这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- usermapper.xml对用user表的操作 -->
<!-- namespace:用于隔离sql语句 -->
<!-- namesapce是类路径名 -->
<mapper namespace="com.hpe.mapper.UserMapper">
	
		<sql id="query_user">
		<if test="username!=null and username!=''">
			and username like '%${username}%'
		</if>

		<if test="sex!=null and sex!=''">
			and sex=#{sex}
		</if>
	</sql>
	
	<!-- 当条件表达式条件为真,才会拼接上if中的sql -->
	<select id="findUserByUser" parameterType="User" resultType="User">
		select* from user
		<where><!-- 自动去掉第一个and -->
			<include refid="query_user"></include>
		</where>
	</select>

	<select id="findUserByUser1" parameterType="User" resultType="User">
		select* from user
		<where><!-- 自动去掉第一个and -->
			<include refid="query_user"></include>

			<!-- <if test="ids!=null and ids.size()>0"> and id in (<foreach collection="ids" 
				item="item" separator=","> #{item} </foreach> ) </if> -->

			<if test="ids!=null and ids.size()>0">
				and (
				<foreach collection="ids" item="item" separator="or">
					id=#{item}
				</foreach>
				)
			</if>
		</where>

	</select>

	<select id="findUserCountByUser" parameterType="User"
		resultType="int">
		select count(*) from user
		<where><!-- 自动去掉第一个and -->
			<include refid="query_user"></include>
		</where>
	</select>
</mapper>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值