<?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.xxx.dynamicSQL.mapper.EmpMapper"> <!-- choose、when、otherwise相当于if, else if ,else when至少设置一个,匹配上条件后,其他的条件不执行
otherwise最多设置一个,when中的条件都不符合时,执行该条件 --> <select id="getEmp" resultType="emp"> select * from t_emp <where> <choose> <when test="empName != null and empName != ''"> emp_name = #{empName} </when> <when test="age != null and age != ''"> age = #{age} </when> <when test="gender != null and gender != ''"> gender = #{gender} </when> <otherwise> emp_id = 1 </otherwise> </choose> </where> </select> </mapper>
测试代码1:
@Test public void test1(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,"",11,"男"); List<Emp> list = mapper.getEmp(emp); list.forEach(System.out::println); }
测试代码2:
@Test public void test1(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = new Emp(null,"",null,""); List<Emp> list = mapper.getEmp(emp); list.forEach(System.out::println); }
标签:mapper,emp,list,when,EmpMapper,sqlSession,choose,mybatis,Emp From: https://www.cnblogs.com/ixtao/p/17177147.html