mybatis框架-choose when otherwise 的使用

Posted dongyaotou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis框架-choose when otherwise 的使用相关的知识,希望对你有一定的参考价值。

需求:模拟实际业务情况,传入多条件进行查询

/**
* 需求:模拟实际业务,用户传入多个条件,进行用户列表信息的查询
* @param roleids
* @return
*/
public List<User> getUserListByMulConditions(@Param("usercode")String usercode,@Param("userName")String userName,@Param("userRole")Integer userRole,@Param("creationDate")java.util.Date date);

 

<!--模拟多个条件,进行用户列表信息的查询 -->
<select id="getUserListByMulConditions" resultMap="userListArray" >
  select * from smbms_user where 1=1
  <choose>
    <when test="userName!=null and userName!=‘‘ "> and userName like concat(‘%‘,#{userName},‘%‘)</when>
    <when test="usercode!=null and usercode!=‘‘ "> and usercode like concat(‘%‘,#{usercode},‘%‘)</when>
    <when test="userRole!=null and userRole!=‘‘ "> and userrole= #{userRole} </when>
    <otherwise> and year(creationDate)=year(#{creationDate}) </otherwise>
  </choose>
</select>

 

<resultMap type="User" id="userListArray">
   <id property="id" column="id"/>
  <result property="userCode" column="userCode" />
  <result property="userName" column="userName" />
  <result property="userRole" column="userRole" />
</resultMap>

 

 1 //模拟实际业务情况,传入多条件进行查询
 2         @Test
 3         public void testGetUserListByMulConditions(){
 4             SqlSession sqlSession = null;
 5             String usercode="";
 6             String userName="";
 7             Integer userRole=1;
 8     
 9             
10             List<User> userListShow=new ArrayList<User>();
11             try {
12                 Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2019-12-22");
13                 sqlSession = MyBatisUtil.createSqlSession();
14                 userListShow = sqlSession.getMapper(UserMapper.class).getUserListByMulConditions(usercode,userName,userRole,date);
15                 
16             } catch (Exception e) {
17                 // TODO: handle exception
18                 e.printStackTrace();
19             }finally{
20                 MyBatisUtil.closeSqlSession(sqlSession);
21             }
22             for(User user: userListShow){
23                 logger.debug("testGetUserByForeach_Gender_Roleids UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
24             }
25         
26                 
27         }
28     
29     

运行结果:

1 [DEBUG] 2019-12-22 17:09:46,696 cn.smbms.dao.user.UserMapper.getUserListByMulConditions - ==>  Preparing: select * from smbms_user where 1=1 and userrole= ? 
2 [DEBUG] 2019-12-22 17:09:46,711 cn.smbms.dao.user.UserMapper.getUserListByMulConditions - ==> Parameters: 1(Integer)
3 [DEBUG] 2019-12-22 17:09:46,723 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@65d0d124]
4 [DEBUG] 2019-12-22 17:09:46,724 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@65d0d124]
5 [DEBUG] 2019-12-22 17:09:46,724 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1708183844 to pool.
6 [DEBUG] 2019-12-22 17:09:46,724 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: admin and UserName: 系统管理员and userRole:1

以上是关于mybatis框架-choose when otherwise 的使用的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis中choose when正确写法

求解mybatis choose 标签使用为啥只能执行第一个<when>语句

MyBatis框架动态SQL,分页,二进制存入数据库图片

MyBatis框架动态SQL

java框架之mybatis(动态SQL)

choose标签可以单独使用吗