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 14
| <select id="selectEmployeeUserChoose" resultType="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
| 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='男'
“的对象.
原文链接: 10.2 MyBatis动态SQL 10.2.2 choose(when, otherwise)标签