• mybatis框架,使用foreach实现复杂结果的查询循环集合数组


    需求:假定现在查询出用户角色是2和3指定的用户列表信息,并进行展示

    接口:

    /**
    * 需求:传入指定的用户角色,用户角色有1-n,获取这些用户角色下的用户列表信息
    * @param roleids
    * @return
    */
    public List<User> getUserListByRoleid_Array(Integer[] roleids);

    mapper.xml文件

     

     1     <resultMap type="User" id="userListArray">
     2          <id property="id" column="id"/>
     3         <result property="userCode" column="userCode" />
     4         <result property="userName" column="userName" />
     5         <result property="userRole" column="userRole" />
     6     </resultMap>
     7     <select id="getUserListByRoleid_Array" resultMap="userListArray" >
     8        select * from smbms_user where userRole in 
     9        <foreach collection="array" item="roleids" open="("  separator="," close=")">
    10            #{roleids}
    11        </foreach>
    12     </select>

     编写对应的测试方法:

     1     @Test
     2     public void testGetUserByForeach_Array(){
     3         SqlSession sqlSession = null;
     4         List<User> userList = new ArrayList<User>();
     5         Integer[] userArray={2,3};
     6         try {
     7             sqlSession = MyBatisUtil.createSqlSession();
     8             userList = sqlSession.getMapper(UserMapper.class).getUserListByRoleid_Array(userArray);
     9             
    10         } catch (Exception e) {
    11             // TODO: handle exception
    12             e.printStackTrace();
    13         }finally{
    14             MyBatisUtil.closeSqlSession(sqlSession);
    15         }
    16         for(User user: userList){
    17             logger.debug("testGetUserListAddressByUserId UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
    18         }
    19     
    20             
    21     }

    运行结果:

    [DEBUG] 2019-12-17 11:32:24,987 cn.smbms.dao.user.UserMapper.getUserListByRoleid_Array - ==> Preparing: select * from smbms_user where userRole in ( ? , ? )
    [DEBUG] 2019-12-17 11:32:25,003 cn.smbms.dao.user.UserMapper.getUserListByRoleid_Array - ==> Parameters: 2(Integer), 3(Integer)
    [DEBUG] 2019-12-17 11:32:25,020 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4a6397eb]
    [DEBUG] 2019-12-17 11:32:25,021 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@4a6397eb]
    [DEBUG] 2019-12-17 11:32:25,021 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1248040939 to pool.
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: liming and UserName: 李明and userRole:2
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: hanlubiao and UserName: 韩路彪and userRole:2
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhanghua and UserName: 张华and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: wangyang and UserName: 王洋and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhaoyan and UserName: 赵燕and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: sunlei and UserName: 孙磊and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: sunxing and UserName: 孙兴and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhangchen and UserName: 张晨and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: dengchao and UserName: 邓超and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: yangguo and UserName: 杨过and userRole:3
    [DEBUG] 2019-12-17 11:32:25,021 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhaomin and UserName: 赵敏and userRole:2

  • 相关阅读:
    互联网改变的产业 汽车 x 互联网 = 汽车革命
    我有一个实现HMI触摸屏的资源【4418开发平台】降d成本
    iTOP-4412开发板裸机开发环境文档分享
    2019年最受欢迎iTOP-4418开发板_新产品研发必备利器
    安卓触控一体机为什么得到大家认可?远比Windows系统一体机大受欢迎
    新手入门嵌入式学习单片机?stm32?树莓派?4412开发板资料大汇报-基础了解
    iTOP-4418开发板-Qt系统下运行摄像头测试程序
    iTOP-4418/6818开发板-QtE4.7WIFI_MT6620热点
    iTOP-4412开发板-使用PartitionManager分区之后tf卡无法识别
    迅为iTOP-4418/6818开发板-MiniLinux-GPS使用文档
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12053588.html
Copyright © 2020-2023  润新知