mybatis框架,使用foreach實現復雜結果的查詢--循環List集合方式


需求,根據用戶角色列表  查詢用戶列表信息 

之前我們傳入的參數是Array,一個數組的形式,現在我們傳入的是一個List集合,其他條件沒有變化。

/**
* 需求:傳入指定的用戶角色,用戶角色有1-n,獲取這些用戶角色下的用戶列表信息
* @param roleids
* @return
*/
public List<User> getUserListByRoleid_List(List<Integer> roleList);

 

<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>

<!-- 使用第二中方式機型入參,傳入的參數是list集合 -->
<select id="getUserListByRoleid_List" resultMap="userListArray" >
  select * from smbms_user where userRole in
   <foreach collection="list" item="roleList" open="(" separator="," close=")">
   #{roleList}
  </foreach>
</select>

 

 1 //傳入List集合的方式
 2     @Test
 3     public void testGetUserByForeach_List(){
 4         SqlSession sqlSession = null;
 5         List<Integer> userList = new ArrayList<Integer>();
 6         userList.add(2);
 7         userList.add(3);
 8         List<User> userListShow=new ArrayList<User>();
 9         try {
10             sqlSession = MyBatisUtil.createSqlSession();
11             userListShow = sqlSession.getMapper(UserMapper.class).getUserListByRoleid_List(userList);
12             
13         } catch (Exception e) {
14             // TODO: handle exception
15             e.printStackTrace();
16         }finally{
17             MyBatisUtil.closeSqlSession(sqlSession);
18         }
19         for(User user: userListShow){
20             logger.debug("testGetUserListAddressByUserId UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
21         }
22     
23             
24     }

運行結果:

 1 [DEBUG] 2019-12-22 15:13:43,496 cn.smbms.dao.user.UserMapper.getUserListByRoleid_List - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@28bb494b]
 2 [DEBUG] 2019-12-22 15:13:43,496 cn.smbms.dao.user.UserMapper.getUserListByRoleid_List - ==>  Preparing: select * from smbms_user where userRole in ( ? , ? ) 
 3 [DEBUG] 2019-12-22 15:13:43,514 cn.smbms.dao.user.UserMapper.getUserListByRoleid_List - ==> Parameters: 2(Integer), 3(Integer)
 4 [DEBUG] 2019-12-22 15:13:43,550 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@28bb494b]
 5 [DEBUG] 2019-12-22 15:13:43,551 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@28bb494b]
 6 [DEBUG] 2019-12-22 15:13:43,551 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 683362635 to pool.
 7 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: liming and UserName: 李明and userRole:2
 8 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: hanlubiao and UserName: 韓路彪and userRole:2
 9 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhanghua and UserName: 張華and userRole:3
10 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: wangyang and UserName: 王洋and userRole:3
11 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhaoyan and UserName: 趙燕and userRole:3
12 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: sunlei and UserName: 孫磊and userRole:3
13 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: sunxing and UserName: 孫興and userRole:3
14 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhangchen and UserName: 張晨and userRole:3
15 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: dengchao and UserName: 鄧超and userRole:3
16 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: yangguo and UserName: 楊過and userRole:3
17 [DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhaomin and UserName: 趙敏and userRole:2


免責聲明!

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



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