• 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
  • 相关阅读:
    jquery_ajax 地址三级联动
    delphi窗体按钮灰化禁用
    sqlserver查找断号,回收单据号
    query.locate过个过滤参数
    StringGrid换行功能
    你不知道的JavaScript--Item13 理解 prototype, getPrototypeOf 和__proto__
    你不知道的JavaScript--Item12 undefined 与 null
    你不知道的JavaScript--Item11 arguments对象
    你不知道的JavaScript--Item10 闭包(closure)
    你不知道的JavaScript--Item9 call(),apply(),bind()与回调
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12080327.html
Copyright © 2020-2023  润新知