mysql mybatis list循环_mybatis框架,使用foreach实现复杂结果的查询--循环List集合方式...

需求,根据用户角色列表  查询用户列表信息

之前我们传入的参数是Array,一个数组的形式,现在我们传入的是一个List集合,其他条件没有变化。

/**

* 需求:传入指定的用户角色,用户角色有1-n,获取这些用户角色下的用户列表信息

* @param roleids

* @return

*/

public List getUserListByRoleid_List(List roleList);

select * from smbms_user where userRole in

#{roleList}

1 //传入List集合的方式

2 @Test3 public voidtestGetUserByForeach_List(){4 SqlSession sqlSession = null;5 List userList = new ArrayList();6 userList.add(2);7 userList.add(3);8 List userListShow=new ArrayList();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 trueon 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 683362635to 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值