需求:假定现在查询出用户角色是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实现复杂结果的查询--循环集合数组的更多相关文章

  1. mybatis框架之foreach标签

    foreach一共有三种类型,分别为List,[](array),Map三种,下面表格是我总结的各个属性的用途和注意点. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径 ...

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

    需求,根据用户角色列表  查询用户列表信息 之前我们传入的参数是Array,一个数组的形式,现在我们传入的是一个List集合,其他条件没有变化. /** * 需求:传入指定的用户角色,用户角色有1-n ...

  3. 在mybatis框架中,延迟加载与连表查询的差异

    1.引子 mybatis的延迟加载,主要应用于一个实体类中有复杂数据类型的属性,包括一对一和一对多的关系(在xml中用collection.association标签标识).这个种属性往往还对应着另一 ...

  4. myBatis框架_关于怎么获得多表查询的总记录数

    <!-- 查找总记录数 --> <select id="billCount" resultType="int"> select coun ...

  5. SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  6. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  7. MyBatis 框架笔记

    Mybatis 框架笔记   ------技术源于热爱! 获取更多内容请关注小编的个人微信公众平台 1       Mybatis入门 1.1     单独使用jdbc编程问题总结 1.1.1  jd ...

  8. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  9. Mybatis框架入门

    Mybaits框架 一.什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了googl ...

随机推荐

  1. luogu P5606 小 K 与毕业旅行 - 构造 - 多项式

    题目传送门 传送门 先考虑 $a_i > 0$ 的情况.考虑构造这样一个顺序:$a_i$ 要么和后面的数的乘积都大于 $w$ 要么都小于等于 $w$. 这个构造可以这样做: vector< ...

  2. torch_06_卷积神经网络

    1.概述 卷积神经网络的参数,由一些可学习的滤波器集合构成的,每个滤波器在空间上都计较小,但是深度和输入数据的深度保持一致.在前向传播中,让每个滤波器都在输入数据的宽度和高度上滑动(卷积),然后计算整 ...

  3. Laravel本地环境搭建:Homestead开发环境的部署

    Laravel框架在php开发过程是不断进行优化的,当然也包括了本地环境的开发,下面我们就来具体看看laravel框架中的Homestead 开发环境的部署内容. 首先白以下几个概念 VirtualB ...

  4. 在Mu-kittenbot中使用Robotbit固件

    首先,先下载安装支持robotbit扩展板的Mu: http://cdn.kittenbot.cn/mu/mu-kittenbot.exe 标准的3针插口,信号,正电,负电,可接市面的arduino模 ...

  5. vins_fusion学习笔记

    Vins-Fusion源码:https://github.com/HKUST-Aerial-Robotics/VINS-Fusion 摘要 应项目需要,侧重学习stereo+gps融合 转载几篇写的比 ...

  6. SQL Inserted 触发器游标结合实例

    SqlServer的Inserted可能是一个集合而不是一条数据,如果有如果需要对插入数据进行处理,有时需要用游标逐条处理 FROM inserted) --插入和更新 declare cur cur ...

  7. vue-v-xxx基于 Vue拓展的 v-xxx 库

    君问归期未有期,巴山夜雨涨秋池. 何当共剪西窗烛,却话巴山夜雨时. 作为vue轻车熟路的老司机,经常会用到一些指令,vue官方提供的指令又太少,无法满足旺盛的欲望,而每次要写一遍,终日郁郁寡欢,从小就 ...

  8. jsplumb 初识

    Jsplumb官网:https://jsplumbtoolkit.com GitHub:https://github.com/sporritt/jsplumb/    有很多官网示例 基础教程:htt ...

  9. 你知道Object中有哪些方法及其作用吗?

    一.引言二.Object方法详解1.1.registerNatives()1.2.getClass()1.2.1.反射三种方式:1.3.hashCode()1.4.equals()1.4.clone( ...

  10. Eureka和ZooKeeper的区别

    首先我们先说下: RDBMS==>(MySql,Oracle,SqlServer等关系型数据库)遵循的原则是:ACID原则(A:原子性.C:一致性.I:独立性.D:持久性.). NoSql==& ...