Mybatis 动态SQL【if标签、 where标签、sql标签和include标签、foreache标签】

本文介绍了在Mybatis中如何使用动态SQL进行条件查询,包括if标签处理空值问题,where标签避免语法错误,以及sql和include标签减少SQL重复。同时,展示了如何使用foreach标签解析数组或List查询多个id的用户信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求:根据性别和名字查询用户

SQL语句:

SELECT id, username, birthday, sex, address FROM `user` WHERE sex = 1 AND username LIKE '%三%'

存在的问题:

A.sex和username属性有可能只有一个,而另一个为null或者空【使用if标签

B.但是当sex/username 属性为null或者空的时候,sql语句就变成了:

              SELECT id, username, birthday, sex, address FROM `user` WHERE AND username LIKE '%三%'   ==>>存在语法错误

解决方案:

  •  添加1=1:SELECT id, username, birthday, sex, address FROM `user` WHERE 1=1 AND username LIKE '%三%'

 

<select id="findBynamesex" parameterType="User" resultType="User">
		select * from user 
		where 1=1
			<if test="sex != null and sex!=''">
				sex = #{sex}
			</if>
			<if test="username != null and username != ''">
				and username like "%"#{username}"%"
			</if>
		</where>
	</select>
  •  where标签
<select id="findBynamesex" parameterType="User" resultType="User">
		select * from user
		<!-- where标签可以自动添加where,同时处理sql语句中         前and   关键字 -->
		<where>
			<if test="sex != null and sex!=''">
				and sex = #{sex}
			</if>
			<if test="username != null and username != ''">
				and username like "%"#{username}"%"
			</if>
		</where>
	</select>

C.应对sql重复部分解决【SQL片段----sql标签和include标签

 

要求:根据多个id查询用户信息【向sql传递数组或List,mybatis使用foreach解析】

SQL语句:SELECT * FROM user WHERE id IN (1,10,24)

实现1:创建包装类,在包装类里面添加一个List类型的属性

 

实现2:使用Integer[] ids这样的数组

实现3:使用List<Integer> ids这样的list

mapper.java

package com.mapper;

import java.util.List;

import com.entity.QueryVo;
import com.entity.User;

/**
 *
 * mapper动态代理开发 只写接口不需要实现类(由程序生成) 用来取代Dao
 */
public interface UserMapper {
	// 遵循4个原则
	// 1 接口方法名 == User.xml中的id
	// 2 返回值类型 与 User.xml中的返回值一致
	// 3 方法的入参 与 User.xml中一致
	// 4 命名空间绑定此接口(User.xml中namespace写此类)
	

	// 根据id列表查询用户【封装类里面添加一个list】
	public List<User> findByIds1(QueryVo vo);

	// 根据id列表查询用户【使用Integer[] ids这样的数组】
	public List<User> findByIds2(Integer[] ids);

	// 根据id列表查询用户【使用List<Integer> ids这样的list】
	public List<User> findByIds3(List<Integer> ids);

}

mapper.xml

<?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">
<mapper namespace="com.mapper.UserMapper">
<!-- 根据id列表查询用户【封装类里面添加一个list】 这里的collection可以直接取封装类的ids属性
		SELECT * FROM user WHERE id IN (1,10,24)
	-->
	<select id="findByIds1" parameterType="QueryVo" resultType="User">
	<include refid="selector"/>
	<where>
		<foreach collection="ids" item="id" open="id IN (" close=")" separator=",">
		#{id}
		</foreach>
	
	</where>
	</select>
	
	
	<!-- 根据id列表查询用户【使用Integer[] ids这样的数组】 这里的collection不能直接写ids,必须写array
		SELECT * FROM user WHERE id IN (1,10,24)
	-->
	<select id="findByIds2" parameterType="Integer" resultType="User">
	<include refid="selector"/>
	<where>
		<foreach collection="array" item="id" open="id IN (" close=")" separator=",">
		#{id}
		</foreach>
	
	</where>
	</select>
	
	
	<!-- 根据id列表查询用户【使用List<Integer> ids这样的list】 这里的collection也不能直接写ids,必须写list
		SELECT * FROM user WHERE id IN (1,10,24)
	-->
	<select id="findByIds3" parameterType="List" resultType="User">
	<include refid="selector"/>
	<where>
		<foreach collection="list" item="id" open="id IN (" close=")" separator=",">
		#{id}
		</foreach>
	
	</where>
	</select>
	
	<!-- 定义sql片段 -->
	<sql id="selector">
	select * from user
	</sql>
</mapper>

测试类:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值