mybatis框架,使用foreach实现复杂结果的查询--循环集合数组
需求:假定现在查询出用户角色是2和3指定的用户列表信息,并进行展示
接口:
/**
* 需求:传入指定的用户角色,用户角色有1-n,获取这些用户角色下的用户列表信息
* @param roleids
* @return
*/
public List<User> getUserListByRoleid_Array(Integer[] roleids);
mapper.xml文件
<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
mybatis框架,使用foreach实现复杂结果的查询--循环集合数组的更多相关文章
- mybatis框架之foreach标签
foreach一共有三种类型,分别为List,[](array),Map三种,下面表格是我总结的各个属性的用途和注意点. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径 ...
- mybatis框架,使用foreach实现复杂结果的查询--循环List集合方式
需求,根据用户角色列表 查询用户列表信息 之前我们传入的参数是Array,一个数组的形式,现在我们传入的是一个List集合,其他条件没有变化. /** * 需求:传入指定的用户角色,用户角色有1-n ...
- 在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 ...
随机推荐
- @PostConstruct、@Autowired以及构造函数的执行顺序
结论先行:构造函数 -> PostConstruct -> @Autowired 依次执行 由于项目需要启动时加载一个配置信息,所以想到了用@PostConstruct,如下所示: @Co ...
- jquery easyui datagrid 加载保存好的自定义设置的列属性
直接附上源代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- vertica内存不足的解决方案
执行以下语句: alter resource pool general MAXMEMORYSIZE '80%'; 当内存达到80%时,新的任务进去等待状态,直到前面任务执行完毕.
- laravel框架的中间件middleware的详解
本篇文章给大家带来的内容是关于laravel框架的中间件middleware的详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. laravel中间件是个非常方便的东西,能将一些逻辑 ...
- git bash 使用自带 curl 命令出现乱码解决方法
前言 使用过 git 的小伙伴应该都不会陌生,git 自带一个终端 git bash 类似于 window 自带的 dos git 官网下载:https://git-scm.com/dow ...
- IDEA rider 管道模式 经典模式(2)
1.这里设置为Classic,并打开applicationhost.config将对应应用的 Clr4IntegratedAppPool全部替换为 Clr4ClassicAppPool 2.Confi ...
- ElasticSearch : High Rest Api 使用
pom文件: <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId> ...
- 【linux】CentOS 查看系统时间,修改时区
===============CentOS 7.6================ 1.查看系统时间 date 查看当前系统时间以及时区结果是: Mon Jul 8 09:23:31 UTC 2019 ...
- 《STL源码剖析》——Array
array array本身内容较少,日常使用也不是很多,里面也没有很高深的技巧 1 array的基本架构 了解array的架构需要一个额外的语法知识: int a[100]; int [100]b; ...
- java获取调用当前方法的方法名和行数
java获取调用当前方法的方法名和行数String className = Thread.currentThread().getStackTrace()[2].getClassName();//调用的 ...