10.2 MyBatis动态SQL 10.2.2 choose(when, otherwise)标签 MyBatis
提供了choose
标签,它有点像Java
中的switch
语句。
测试choose标签 select标签 在EmployeeMapper.xml
中添加入下select
标签:
1 2 3 4 5 6 7 8 9 10 11 12 13 <select id ="selectEmployeeChoose" parameterType ="hashmap" resultType ="org.fkit.domain.Employee" > SELECT * FROM tb_employee WHERE state = 'ACTIVE' <choose > <when test ="id != null" > and id = #{id}</when > <when test ="loginname != null and password != null" > and loginname = #{loginname} and password = #{password} </when > <otherwise > and sex = '男'</otherwise > </choose > </select >
这个select
标签表示,调用该方法时
如果提供了id
参数就按id
查找,
如果提供了loginname
和password
参数就按loginname
和password
查找,
如果两者都没有提供,就返回所有sex
等于男
的Employee
.
mapper接口方法 在接口中对应的方法:
1 List<Employee> selectEmployeeUserChoose (HashMap<String, Object> params) ;
测试 提供多个条件时的运行效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class ChooseTest { public static void main (String[] args) { SqlSession sqlSession = null ; sqlSession = SqlSessionFratoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); HashMap<String, Object> params = new HashMap <String, Object>(); params.put("id" , 1 ); params.put("loginname" , "xiaowang" ); params.put("password" , "xiaowang" ); List<Employee> employees = employeeMapper.selectEmployeeUserChoose(params); employees.forEach(employee -> System.out.println(" " + employee)); } }
运行程序,控制台输出如下:
1 2 3 4 DEBUG [main] ==> Preparing: select * from tb_employee where state='active' and id=? DEBUG [main] ==> Parameters: 1 (Integer) DEBUG [main] <== Total: 1 Employee [id=1 , loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19 , phone=123456789123 , sal=9800 .0 , state=active]
可以看到虽然两个条件都提供,但只会执行第一个条件.也就是说,在choose
标签中,写在前面的when
标签优先匹配.并且匹配成功之后不会再匹配 .所以当满足多个条件时,只会执行第一个when
标签中的SQL
语句 .这和只提供第一个条件的效果是一样的.
只提供第一个条件时的运行效果 现在注释掉loginname
和password
参数,保留id
参数,如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static void main (String[] args) { SqlSession sqlSession = null ; sqlSession = SqlSessionFratoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); HashMap<String, Object> params = new HashMap <String, Object>(); params.put("id" , 1 ); List<Employee> employees = employeeMapper.selectEmployeeUserChoose(params); employees.forEach(employee -> System.out.println(" " + employee)); }
再次执行,控制台输出如下:
1 2 3 4 DEBUG [main] ==> Preparing: select * from tb_employee where state='active' and id=? DEBUG [main] ==> Parameters: 1 (Integer) DEBUG [main] <== Total: 1 Employee [id=1 , loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19 , phone=123456789123 , sal=9800 .0 , state=active]
可以看到这和上面提供多个条件的效果一致。
只提供第二个条件时的运行效果 注释掉id
参数,保留loginname
和password
参数,如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static void main (String[] args) { SqlSession sqlSession = null ; sqlSession = SqlSessionFratoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); HashMap<String, Object> params = new HashMap <String, Object>(); params.put("loginname" , "xiaowang" ); params.put("password" , "xiaowang" ); List<Employee> employees = employeeMapper.selectEmployeeUserChoose(params); employees.forEach(employee -> System.out.println(" " + employee)); }
运行效果:
1 2 3 4 DEBUG [main] ==> Preparing: select * from tb_employee where state='active' and loginname = ? and password = ? DEBUG [main] ==> Parameters: xiaowang(String), xiaowang(String) DEBUG [main] <== Total: 1 Employee [id=2 , loginname=xiaowang, password=xiaowang, name=小王, sex=男, age=21 , phone=123456789123 , sal=6800 .0 , state=active]
可以看到,HashMap
里面传递的参数只包括loginname
和password
,所以SQL
语句是按照loginname
和password
查找,查询返回的Emplyee
对象就是loginname
是xiaowang
,并且password
是xiaowang
的对象.
不提供参数时的运行效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public static void main (String[] args) { SqlSession sqlSession = null ; sqlSession = SqlSessionFratoryTools.getSqlSession(); EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); HashMap<String, Object> params = new HashMap <String, Object>(); List<Employee> employees = employeeMapper.selectEmployeeUserChoose(params); employees.forEach(employee -> System.out.println(" " + employee)); }
运行效果:
1 2 3 4 5 6 DEBUG [main] ==> Preparing: select * from tb_employee where state='active' and sex='男' DEBUG [main] ==> Parameters: DEBUG [main] <== Total: 2 Employee [id=1 , loginname=xiaoming, password=xiaoming, name=小明, sex=男, age=19 , phone=123456789123 , sal=9800 .0 , state=active] Employee [id=2 , loginname=xiaowang, password=xiaowang, name=小王, sex=男, age=21 , phone=123456789123 , sal=6800 .0 , state=active]
可以看到,由于没有传递任何参数,故SQL
语句执行的是otherwise
标签里面的查询条件,即”and sex='男'
“,查询返回的Emplyee
对象就是”sex='男'
“的对象.