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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM