zoukankan      html  css  js  c++  java
  • mybatis框架之多参数入参传入Map集合

    需求:查询出指定性别和用户角色列表下的用户列表信息

    实际上:mybatis在入参的时候,都是将参数封装成为map集合进行入参的,不管你是单参数入参,还是多参数入参,都是可以封装成map集合的,这是无可非议的。

    /**
    * 需求:查询出指定性别和用户角色列表下的用户列表信息
    * @param roleids
    * @return
    */
    public List<User> getUserListByGender_UserRoleids(Map<String,Object> conditionMap);

    <select id="getUserListByGender_UserRoleids" resultMap="userListArray" >
      select * from smbms_user where 1=1 and gender=#{gender} and userRole in
      <foreach collection="roleIDS" item="aaa" open="(" separator="," close=")">
        #{aaa}
      </foreach>
    </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 //多参数的时候,传入map集合
     2         @Test
     3         public void testGetUserByForeach_Gender_Roleids(){
     4             SqlSession sqlSession = null;
     5             Map conditionMap=new HashMap<String, Object>();
     6             List<Integer> userList = new ArrayList<Integer>();
     7             userList.add(2);
     8             userList.add(3);
     9             conditionMap.put("roleIDS", userList);
    10             conditionMap.put("gender", 1);
    11             List<User> userListShow=new ArrayList<User>();
    12             try {
    13                 sqlSession = MyBatisUtil.createSqlSession();
    14                 userListShow = sqlSession.getMapper(UserMapper.class).getUserListByGender_UserRoleids(conditionMap);
    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         }

    运行结果:

    1 [DEBUG] 2019-12-22 15:49:46,403 cn.smbms.dao.user.UserMapper.getUserListByGender_UserRoleids - ==>  Preparing: select * from smbms_user where 1=1 and gender=? and userRole in ( ? , ? ) 
    2 [DEBUG] 2019-12-22 15:49:46,442 cn.smbms.dao.user.UserMapper.getUserListByGender_UserRoleids - ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
    3 [DEBUG] 2019-12-22 15:49:46,462 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@40f892a4]
    4 [DEBUG] 2019-12-22 15:49:46,463 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@40f892a4]
    5 [DEBUG] 2019-12-22 15:49:46,463 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1090032292 to pool.
    6 [DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhanghua and UserName: 张华and userRole:3
    7 [DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhaoyan and UserName: 赵燕and userRole:3
    8 [DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhangchen and UserName: 张晨and userRole:3
    9 [DEBUG] 2019-12-22 15:49:46,464 cn.smbms.dao.user.UserMapperTest - testGetUserByForeach_Gender_Roleids UserCode: zhaomin and UserName: 赵敏and userRole:2
  • 相关阅读:
    Flink 电商实时数仓(二十三):ClickHouse基础(二)使用基础(2)ClickHouse 的安装(centos)
    Flink 电商实时数仓(二十二):ClickHouse基础(一)使用基础(1)ClickHouse 入门
    Flink 源码(二十六):Flink 内存管理(二)内存数据结构 、管理器
    Flink 源码(二十五):Flink 内存管理(一)内存模型与内存数据结构
    Flink 源码(二十四):Flink 任务调度机制(五)调度
    460. LFU Cache (solution 1)
    785. Is Graph Bipartite? (是否二分图)
    1318. Minimum Flips to Make a OR b Equal to c
    211. Add and Search Word
    188. Best Time to Buy and Sell Stock IV
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12080003.html
Copyright © 2011-2022 走看看