MyBatis动态sql_where查询条件

MyBatis动态sql_where查询条件

EmployeeMapperDynamicSQL.java

package com.cn.mybatis.dao;

import java.util.List;

import com.cn.zhu.bean.Employee;

public interface  EmployeeMapperDynamicSQL {
	//<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
	public List<Employee> getEmpsByConditionIf(Employee  employee);
	
}
<?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.cn.mybatis.dao.EmployeeMapperDynamicSQL">
	<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
	<!--public List<Employee> getEmpsByConditionIf(Employee  employee);  -->
	<select id="getEmpsByConditionIf" resultType="com.cn.zhu.bean.Employee">
		select * from tbl_employee  where 
			<!--
				test 判断表达式(OGNL) C:if test OGNL参照ppt或者官方文档 c:if test 从参数中取值进行判断
				遇见特殊符号应该去写转义字符
			-->
			<if test="id!=null">
				 id=#{id}
        </if>
			<if test="lastName!=null && lastName!=""">
				and last_name like #{lastName} 
        </if>
			<if test="email !=null  and email.trim()!=""">
				and email=#{email} 
        </if>
			<!-- ognl 会进行字符串和数字的转换   "0"==0 -->
			<if test="gender==0  or  gender==1">
				and gender=#{gender}
        </if>
	</select>
</mapper>

测试程序

@Test
	public void  testDynamicSql() throws  IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		try {
			EmployeeMapperDynamicSQL   mapper=	openSession.getMapper(EmployeeMapperDynamicSQL.class);	
			Employee employee=new Employee(3, "%h%", "[email protected]", null);
			List<Employee>  emps=  mapper.getEmpsByConditionIf(employee);
			for(Employee emp: emps)
				System.out.println(emp);
			
		} catch (Exception e) {
			// TODO: handle exceptio
			e.printStackTrace();
		}

	}

测试结果

这样就能正常查询数据,如果当主键为空的时候,就会报错,那么怎么解决呢?

会多出一个  and

第一种方法: 在where 后面加上  where  1=1

<?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.cn.mybatis.dao.EmployeeMapperDynamicSQL">
	<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
	<!--public List<Employee> getEmpsByConditionIf(Employee  employee);  -->
	<select id="getEmpsByConditionIf" resultType="com.cn.zhu.bean.Employee">
		select * from tbl_employee	
		where 1=1
			<!--
				test 判断表达式(OGNL) C:if test OGNL参照ppt或者官方文档 c:if test 从参数中取值进行判断
				遇见特殊符号应该去写转义字符
			-->
			<if test="id!=null">
				 id=#{id}
        </if>
			<if test="lastName!=null && lastName!=""">
				and last_name like #{lastName} 
        </if>
			<if test="email !=null  and email.trim()!=""">
				and email=#{email} 
        </if>
			<!-- ognl 会进行字符串和数字的转换   "0"==0 -->
			<if test="gender==0  or  gender==1">
				and gender=#{gender}
        </if>
	</select>
</mapper>


第二种解决方式: 用where标签  <where >  </where>

<?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.cn.mybatis.dao.EmployeeMapperDynamicSQL">
	<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
	<!--public List<Employee> getEmpsByConditionIf(Employee  employee);  -->
	<select id="getEmpsByConditionIf" resultType="com.cn.zhu.bean.Employee">
		select * from tbl_employee
		<where>
			<!--
				test 判断表达式(OGNL) C:if test OGNL参照ppt或者官方文档 c:if test 从参数中取值进行判断
				遇见特殊符号应该去写转义字符
			-->
			<if test="id!=null">
				 id=#{id}
        </if>
			<if test="lastName!=null && lastName!=""">
				and last_name like #{lastName} 
        </if>
			<if test="email !=null  and email.trim()!=""">
				and email=#{email} 
        </if>
			<!-- ognl 会进行字符串和数字的转换   "0"==0 -->
			<if test="gender==0  or  gender==1">
				and gender=#{gender}
        </if>
		</where>
	</select>
</mapper>


测试程序

@Test
	public void  testDynamicSql() throws  IOException{
		SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
		SqlSession openSession=sqlSessionFactory.openSession();
		try {
			EmployeeMapperDynamicSQL   mapper=	openSession.getMapper(EmployeeMapperDynamicSQL.class);	
			Employee employee=new Employee(null, "%h%", null, null);
			List<Employee>  emps=  mapper.getEmpsByConditionIf(employee);
			for(Employee emp: emps)
				System.out.println(emp);
			// 查询的时候如果某些条件没带可能sql拼装会有问题
			// 1.给where 后面加上1=1,以后的条件都and  
			//2.  mybatis 使用where 标签将所有的查询条件包括在内。
			// mybatis 就会将where标签后面第一个and 去掉
			// where只会去掉第一个多出来的and 或者  or
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

测试结果

                    // 查询的时候如果某些条件没带可能sql拼装会有问题
// 1.给where 后面加上1=1,以后的条件都and  
//2.  mybatis 使用where 标签将所有的查询条件包括在内。
// mybatis 就会将where标签后面第一个and 去掉
// where只会去掉第一个多出来的and 或者  or

发布了89 篇原创文章 · 获赞 1 · 访问量 766

猜你喜欢

转载自blog.csdn.net/zhupengqq1/article/details/103990838