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

posted on 2019-12-22 15:52  ~码铃薯~  阅读(3156)  评论(0编辑  收藏  举报

导航