mybatis-210721-01---动态sql-if判断&where查询条件

mybatis-210721-01—动态sql-if判断&where查询条件.md


if_判断

EmployeeMapper.java

package com.bgy.mybatis.dao;
import java.util.List;
import com.bgy.mybatis.bean.Employee;

public interface EmployeeMapper {
	
	public List<Employee> getEmpsByConditionIf(Employee employee);
}

EmployeeMapper.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.bgy.mybatis.dao.EmployeeMapper">
	
	<!--
		if
		choose(when,otherwise)
		trim(where,set)
		foreach
	-->
	<!--
		查询员工
			要求:
			携带了那个字段,查询条件就带上这个字段的值
	-->
	<select id="getEmpsByConditionIf" resultType="com.bgy.mybatis.bean.Employee">
		select 
			* 
		from
			tbl_employee
		where
			<!--
				test:判断表达式(OGNL)
				从参数中取值
				
				遇见特殊符号需要转义
					比如: " " ==(转义)==> &quot;&quot;
			-->
			<if test="id!=null and id!=''">
				id=#{id}
			</if>
			<if test="lastName!=null and lastName!=&quot;&quot;">
				and last_name like #{lastName}
			</if>
			<if test="email!=null and email.trim()!=&quot;&quot;">
				and email like #{email}
			</if>
			<!-- ognl会自动进行字符串和数字的转换判断	"0"==0 -->
			<if test="gender==0 or gender==1">
				and gender=#{gender}
			</if>
	</select>
</mapper>

MybatisTest.java

package com.bgy.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import com.bgy.mybatis.bean.Department;
import com.bgy.mybatis.bean.Employee;
import com.bgy.mybatis.dao.DepartmentMapper;
import com.bgy.mybatis.dao.EmployeeMapper;

class MybatisTest {
	
	public SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		return new SqlSessionFactoryBuilder().build(inputStream);
	}

	
	/**
	 * 测试sql——if判断
	 * @throws IOException
	 */
	@Test
	public void test() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		try {
			EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
			
			Employee employee = new Employee(1,"","","1");
			List<Employee> emps = mapper.getEmpsByConditionIf(employee);
			
			System.out.println(emps);
		} finally {
			sqlSession.close();
		}
	}
}


where查询条件

EmployeeMapper.xml(笔记在这儿)(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.bgy.mybatis.dao.EmployeeMapper">
	
	<!--
		if
		choose(when,otherwise)
		trim(where,set)
		foreach
	-->
	<!--
		查询员工
			要求:
			携带了那个字段,查询条件就带上这个字段的值
	-->
	<select id="getEmpsByConditionIf" resultType="com.bgy.mybatis.bean.Employee">
		select 
			* 
		from
			tbl_employee
		where 
        	1=1
			<!--
				查询的时候如果某些条件没带sql,拼装可能有问题
				解决:
					1、给where后面加上 1=1 ,以后的条件都是    and xxx
					2、mybatis使用where标签来将所有的查询条件包括在内。
			-->
			<if test="id!=null and id!=''">
				and id=#{id}
			</if>
			<if test="lastName!=null and lastName!=&quot;&quot;">
				and last_name like #{lastName}
			</if>
			<if test="email!=null and email.trim()!=&quot;&quot;">
				and email like #{email}
			</if>
			<!-- ognl会自动进行字符串和数字的转换判断	"0"==0 -->
			<if test="gender==0 or gender==1">
				and gender=#{gender}
			</if>
	</select>
</mapper>

EmployeeMapper.xml(笔记在这儿)(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.bgy.mybatis.dao.EmployeeMapper">
	
	<!--
		if
		choose(when,otherwise)
		trim(where,set)
		foreach
	-->
	<!--
		查询员工
			要求:
			携带了那个字段,查询条件就带上这个字段的值
	-->
	<select id="getEmpsByConditionIf" resultType="com.bgy.mybatis.bean.Employee">
		select 
			* 
		from
			tbl_employee
			<!--
				查询的时候如果某些条件没带sql,拼装可能有问题
				解决:
					1、给where后面加上 1=1 ,以后的条件都是    and xxx
					2、mybatis使用where标签来将所有的查询条件包括在内。
					   mybatis就会将where标签中拼装的sql,将多出来的and或or去掉。
					   where只能去掉第一个多出来的and或or。
			-->
			<where>
				<if test="id!=null and id!=''">
				id=#{id}
				</if>
				<if test="lastName!=null and lastName!=&quot;&quot;">
					and last_name like #{lastName}
				</if>
				<if test="email!=null and email.trim()!=&quot;&quot;">
					and email like #{email}
				</if>
				<!-- ognl会自动进行字符串和数字的转换判断	"0"==0 -->
				<if test="gender==0 or gender==1">
					and gender=#{gender}
				</if>
			</where>
	</select>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值