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%", "zhu@qq.com", 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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值