首页 技术 正文
技术 2022年11月19日
0 收藏 772 点赞 3,259 浏览 3318 个字

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

接口:

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

mapper.xml文件

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

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

     <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>
<select id="getUserListByRoleid_Array" resultMap="userListArray" >
select * from smbms_user where userRole in
<foreach collection="array" item="roleids" open="(" separator="," close=")">
#{roleids}
</foreach>
</select>

编写对应的测试方法:

     @Test
public void testGetUserByForeach_Array(){
SqlSession sqlSession = null;
List<User> userList = new ArrayList<User>();
Integer[] userArray={2,3};
try {
sqlSession = MyBatisUtil.createSqlSession();
userList = sqlSession.getMapper(UserMapper.class).getUserListByRoleid_Array(userArray); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user: userList){
logger.debug("testGetUserListAddressByUserId UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
} }

运行结果:

[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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849