需求:假定现在查询出用户角色是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. 守护进程--Supervisor

    01,介绍 02,安装部署 yum install python-setuptoolseasy_install supervisor 03,使用说明 supervisor安装成功之后,没有提供默认的配 ...

  2. java web开发入门七(mybatis)基于intellig idea

    mybatis 一. 入门开发步骤 1.导入相关jar包 mybatis3.4.2核心开发包 asm-5.1.jarcglib-3.2.4.jarcommons-logging-1.1.3.jarlo ...

  3. CentOS7 GitLab 安装

    1.安装依赖 $ yum -y install policycoreutils openssh-server openssh-clients postfix $ yum install policyc ...

  4. sql server 2019 & spark

    https://cloudblogs.microsoft.com/sqlserver/2019/04/01/how-to-develop-and-submit-spark-jobs-to-sql-se ...

  5. Qt Quick 常用元素:Textinput 与 TextEdit 文本编辑框

    一.Textinput Textinput 用于编辑一行文本,类似于 QLineEdit. font 分组属性允许你设置 Textlnput 元素所用字体的各种属性,包括字体族(family).大 小 ...

  6. 微信小程序常用样式汇总

    本文系转载: 原文作者:chenzheng8975 原文地址:https://www.cnblogs.com/chenzheng8975/p/9605186.html 微信小程序常用样式汇总 小程序特 ...

  7. torch_03_二分类

    logistic回归 博客链接:https://www.cnblogs.com/home123/p/7356523.html 分类器:监督学习从数据中学习一个分类模型或者分类决策函数,被称为分类器(c ...

  8. phpredis可视化工具RedisDesktopManager

    phpredis可视化工具RedisDesktopManager这个管理phpredis还不错下载地址https://redisdesktop.com/download刷新数据可以右键db0 relo ...

  9. C# 与 Java 的一些差异

    如果你是 Java 开发人员,则可以在 Xamarin 平台上充分利用你的技能和现有代码,同时获得 C# 的代码重用优势.你会发现 C# 语法与 Java 语法非常相似,这两种语言提供非常类似的功能. ...

  10. 【翻译】asp.net core 3.0基本概念

    这篇文章描述了开发asp.net core所需要掌握的基本概念. 原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/?vie ...