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>
//传入List集合的方式
@Test
public void testGetUserByForeach_List(){
SqlSession sqlSession = null;
List<Integer> userList = new ArrayList<Integer>();
userList.add(2);
userList.add(3);
List<User> userListShow=new ArrayList<User>();
try {
sqlSession = MyBatisUtil.createSqlSession();
userListShow = sqlSession.getMapper(UserMapper.class).getUserListByRoleid_List(userList); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user: userListShow){
logger.debug("testGetUserListAddressByUserId UserCode: " + user.getUserCode() + " and UserName: " + user.getUserName()+"and userRole:"+user.getUserRole());
} }
运行结果:
[DEBUG] 2019-12-22 15:13:43,496 cn.smbms.dao.user.UserMapper.getUserListByRoleid_List - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@28bb494b]
[DEBUG] 2019-12-22 15:13:43,496 cn.smbms.dao.user.UserMapper.getUserListByRoleid_List - ==> Preparing: select * from smbms_user where userRole in ( ? , ? )
[DEBUG] 2019-12-22 15:13:43,514 cn.smbms.dao.user.UserMapper.getUserListByRoleid_List - ==> Parameters: 2(Integer), 3(Integer)
[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]
[DEBUG] 2019-12-22 15:13:43,551 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@28bb494b]
[DEBUG] 2019-12-22 15:13:43,551 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 683362635 to pool.
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: liming and UserName: 李明and userRole:2
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: hanlubiao and UserName: 韩路彪and userRole:2
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhanghua and UserName: 张华and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: wangyang and UserName: 王洋and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhaoyan and UserName: 赵燕and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: sunlei and UserName: 孙磊and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: sunxing and UserName: 孙兴and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhangchen and UserName: 张晨and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: dengchao and UserName: 邓超and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: yangguo and UserName: 杨过and userRole:3
[DEBUG] 2019-12-22 15:13:43,551 cn.smbms.dao.user.UserMapperTest - testGetUserListAddressByUserId UserCode: zhaomin and UserName: 赵敏and userRole:2
mybatis框架,使用foreach实现复杂结果的查询--循环List集合方式的更多相关文章
- mybatis框架之foreach标签
foreach一共有三种类型,分别为List,[](array),Map三种,下面表格是我总结的各个属性的用途和注意点. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径 ...
- mybatis框架,使用foreach实现复杂结果的查询--循环集合数组
需求:假定现在查询出用户角色是2和3指定的用户列表信息,并进行展示 接口: /** * 需求:传入指定的用户角色,用户角色有1-n,获取这些用户角色下的用户列表信息 * @param roleids ...
- 在mybatis框架中,延迟加载与连表查询的差异
1.引子 mybatis的延迟加载,主要应用于一个实体类中有复杂数据类型的属性,包括一对一和一对多的关系(在xml中用collection.association标签标识).这个种属性往往还对应着另一 ...
- myBatis框架_关于怎么获得多表查询的总记录数
<!-- 查找总记录数 --> <select id="billCount" resultType="int"> select coun ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...
- MyBatis 框架笔记
Mybatis 框架笔记 ------技术源于热爱! 获取更多内容请关注小编的个人微信公众平台 1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jd ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- Mybatis框架入门
Mybaits框架 一.什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了googl ...
随机推荐
- SpringCloud Ribbon 负载均衡 通过服务器名无法连接的神坑一个
一,问题 采取eureka集群.客户端通过Ribbon调用服务,Ribbon端报下列异常 java.net.UnknownHostException: SERVICE-HI java.lang.Ill ...
- Python连载27-log日志
一.log 1.推荐网站:https://www.cnblogs.com/yyds/p/6901864.html 该网站为日志处理logging模块简介 2.logging模块提供模块级别的函数记录日 ...
- 父组件调用子组件 viewChild
父组件调用子组件 1.在子组件的ts中声明一个变量 public lineout:any="你好,我是被父组件调用的子组件"; 2.在父组件的html中写入 (引入子组件) & ...
- UVA 10852 Less Prime 题解
Less Prime Let n be an integer, 100 n 10000, nd the prime number x, x n, so that n
- Linux 分区管理器
下面的列表没有特定的排名顺序.大多数分区工具应该存在于 Linux 发行版的仓库中. GParted 这可能是 Linux 发行版中最流行的基于 GUI 的分区管理器.你可能已在某些发行版中预装它.如 ...
- 《 .NET并发编程实战》阅读指南 - 第9章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- [转] JavaScript数组去重(12种方法)
数组去重,一般都是在面试的时候才会碰到,一般是要求手写数组去重方法的代码.如果是被提问到,数组去重的方法有哪些?你能答出其中的10种,面试官很有可能对你刮目相看.在真实的项目中碰到的数组去重,一般都是 ...
- 排序算法Java代码实现(四)—— 归并排序
本篇内容: 归并排序 归并排序 算法思想: 将两个或两个以上的有序表合并成一个新的有序表, 即把待排序序列分成若干个子序列,每个子序列是有序的,然后在把有序子序列合并为整体有序序列. 此算法分为两步: ...
- edrawmax使用技巧备忘
由于自己经常需要画图示意,在对比研究了市面上的画图软件后,最终选择了亿图图示,一来是该软件貌似简单强大,有好多现成可用的功能,二来支持国产,并且亿图团队感觉还是不错的一直在用心打磨产品,当然要支持的! ...
- 【selenium】基于python语言,如何用select选择下拉框
在项目测试中遇到了下拉框选择的控件,来总结下如何使用select选择下拉框: 下图是Select类的初始化描述,意思是,给定元素是得是select类型,不是就抛异常.接下来给了例子:要操作这个sele ...