Mybatis动态sql

Mybatis动态sql

1.if标签-多条查询,获取用户列表

正常多条查询代码

<select id="selectUserListByUser" parameterType="User" resultType="User">
select *
from user where
u_sex=#{u_sex}
and u_username like "%"#{u_username}"%"
and u_cid=#{u_cid}
</select>
@Test
	public void test07() throws Exception {
		String resource="sqlMapConfig.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
		SqlSessionFactory build = builder.build(inputStream);
		SqlSession openSession = build.openSession();
		UserMapper mapper = openSession.getMapper(UserMapper.class);
		User user=new User();
		user.setU_cid(1);
		user.setU_username("王");
		user.setU_sex("1");
		List<User> listByUser = mapper.selectUserListByUser(user);
		for (User user2 : listByUser) {
			System.out.println(user2);
		}
		
	}

这个是后我们就会发现,如果在User表中数据为null或’'时,该代码还会继续进行下去,因此用if标签对sql语句中的代码进行筛选

<select id="selectUserListByUser" parameterType="User" resultType="User">
select *
from user where
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex}
</if>
<if test="u_username!=null and u_username!=''">
and u_username like "%"#{u_username}"%"
</if>
<if test="u_cid!=null">
and u_cid=#{u_cid}
</if>
</select>

2.where标签-解决if标签拼接字符串and符号的问题

在使用if标签时,当第一个if标签中的条件不满足时则不会对其中的sql语句进行拼接,因此后续的sql语句中开头则会多出一个and,用where标签可以解决这一问题(where标签只能去除sql语句前面多出的and)

<select id="selectUserListByUser" parameterType="User" resultType="User">
select *
from user
<where>
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex}
</if>
<if test="u_username!=null and u_username!=''">
and u_username like "%"#{u_username}"%"
</if>
<if test="u_cid!=null">
and u_cid=#{u_cid}
</if>
</where>
</select>

3.trim-定制where标签的规则

<select id="selectUserListByUser" parameterType="User" resultType="User">
select *
from user
<trim prefix="where" suffixOverrides="and"><!-- prefix中的where代替了这一行成为sql语句的where -->
<!-- <where> -->
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex} and 
</if>
<if test="u_username!=null and u_username!=''">
u_username like "%"#{u_username}"%" and 
</if>
<if test="u_cid!=null">
u_cid=#{u_cid}
</if>
<!-- </where> -->
</trim>
</select>

4.set标签-解决更新数据时表字符串拼接时逗号问题

<update id="upadteUser" parameterType="User">
update user
<set>
<if test="u_username!=null and u_username!=''">u_username=#{u_username},</if>
<if test="u_password!=null and u_password!=''">u_password=#{u_password},</if>
<if test="u_sex!=null and u_sex!=''">u_sex=#{u_sex}</if>
where
u_id=#{u_id}
</set>
</update>

5.foreach标签-如果需要使用IN查询多条相同数据,可以使用foreach遍历

<select id="selectUserByIds" parameterType="Integer" resultType="User">
SELECT * from user where
u_id in
<!-- collection:代表被循环的集合(这里本来是ids,但因为在直接传递数组需要遍历的时候会默认对其进行封装,因此如果传递的是一个数组的话应该填array)
注意:如果传递的是数组则用list,传递的是包装类则用包装类中的参数名
open:循环开始拼接的内容(不参与循环)
close:循环结束后拼接的内容(不参与循环)
item:代表每次循环向SQL中添加的元素 (参与循环)
separator:代表向SQL中添加的分割符号(参与循环)-->
<foreach collection="array" item="u_id" open="(" close=")" separator=",">
#{u_id}</foreach>
</select>

6.sql标签

<!--sql标签可以将SQL语句进行抽取,类似与在java中抽取共用的变量、方法-->
<sql id="mySql">
SELECT * from user
</sql>
<!--抽取的语句则可以用include标签对其进行调用-->
<include refid="mySql"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值