mybatis关联集合List&分布查询传递多列值
更多精彩文章欢迎关注公众号“Java之康庄大道”

场景:查询部门的同时,要求查询此部门下的所有用户。
部门(Department)
private Integer id;
private String departmentName;
private List<Blogger> bloggers;//关联List集合。部门下所有bloggers
用户(Blogger)
private Integer id;
private String username;
private String password;
private String profile;
private String nickname;
private String sign;
private String imagename;
private Department dep;
方法一:结果集下collection关联
接口DepartMapper.java
package com.yunqing.mybatis.dao;
import com.yunqing.mybatis.bean.Department;
public interface DepartmentMapper {
Department getDepByIdStep2(Integer id);
Department getDepAndBloggers(Integer id);
Department getDepStep(Integer id);
}
DepartmentMapper.xml
<resultMap id="map" type="com.yunqing.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="department" property="departmentName"/>
<collection property="bloggers" ofType="com.yunqing.mybatis.bean.Blogger">
<id column="bid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</collection>
</resultMap>
<select id="getDepAndBloggers" resultMap="map">
SELECT d.id did,d.department,b.id bid,b.username,b.`password` FROM t_dep d LEFT JOIN t_blogger b ON d.id=b.depId WHERE d.id=#{id}
</select>
测试类
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "conf/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
@Test
public void getDepAndBlogger() throws IOException {
SqlSession sqlSession = getSqlSessionFactory().openSession();
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
Department depAndBloggers = mapper.getDepAndBloggers(1);
System.out.println(depAndBloggers);
System.out.println(depAndBloggers.getBloggers());
}
方法二:分布查询
BloggerMapper.java
//根据部门id查询此部门下的人
package com.yunqing.mybatis.dao; import com.yunqing.mybatis.bean.Blogger;
import com.yunqing.mybatis.bean.User;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Select; import java.util.List;
import java.util.Map; public interface BloggerMapper {
@Select("select * from t_blogger")
List<Blogger> getAllBlogger(); @MapKey("username")
Map<String,Blogger> getAllBloggerReturnMap(); Blogger getBloggerAndDepById(Integer id); Blogger getBloggerAndDepByIdAss(Integer id); Blogger getBloggerByIdStep1(Integer id); List<Blogger> getBloggersByDepId(Integer depId); }
BloggerMapper.xml
<resultMap id="mapp" type="com.yunqing.mybatis.bean.Blogger">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<select id="getBloggersByDepId" resultMap="mapp">
SELECT id,username,password FROM t_blogger WHERE depId=#{depId}
</select>
再根据部门id查询部门
DepartmentMapper.java
package com.yunqing.mybatis.dao;
import com.yunqing.mybatis.bean.Department;
public interface DepartmentMapper {
Department getDepByIdStep2(Integer id);
Department getDepAndBloggers(Integer id);
Department getDepStep(Integer id);
}
DepartmentMapper.xml
<resultMap id="maop" type="com.yunqing.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="department" property="departmentName"/>
<collection property="bloggers" select="com.yunqing.mybatis.dao.BloggerMapper.getBloggersByDepId"
column="did"> </collection>
</resultMap>
<select id="getDepStep" resultMap="maop">
SELECT id did,department FROM t_dep WHERE id=#{id}
</select>
测试类
@Test
public void getDepStep() throws IOException {
SqlSession sqlSession = getSqlSessionFactory().openSession();
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
Department depStep = mapper.getDepStep(1);
System.out.println(depStep);
System.out.println(depStep.getBloggers()); }
结果打印:

分布查询时如果需要传递多列值,可以
<resultMap id="maop" type="com.yunqing.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="department" property="departmentName"/>
<collection property="bloggers" select="com.yunqing.mybatis.dao.BloggerMapper.getBloggersByDepId"
column="{depId=did}" fetchType="eager"> </collection>
</resultMap>
<select id="getDepStep" resultMap="maop">
SELECT id did,department FROM t_dep WHERE id=#{id}
</select>
传递多列值,column="{column1=key1,column2=key2}"
cloumn1的来源是,select的里的方法的传递的参数。
List<Blogger> getBloggersByDepId(Integer depId);
虽然在设置中已经开启了延迟加载,但是在此处的fetchType也可以控制是否延迟加载,lazy延迟加载,eager立即加载。
mybatis关联集合List&分布查询传递多列值的更多相关文章
- Mybatis3.1-[tp_36-37]-_映射文件_select_resultMap关联查询__分步查询传递多列值&fetchType_discriminator鉴别器
_分步查询传递多列值&fetchType_discriminator鉴别器 笔记要点出错分析与总结 Department.java bean public class Department { ...
- 将表中null值替换成想要的值、查询某一列值为null
用到ISNULL()函数 例如:SELECT 其他列名,ISNULL(列名,替换值)as 重命名 from 表名 (简单参考:http://www.cnblogs.com/netsa/archive ...
- mybatis 传入集合参数遍历 查询总结
出自:http://blog.csdn.net/u013628152/article/details/51184641 1. findByIds(List ids) 如果参数的类型是List, 则在使 ...
- mybatis映射文件select_resultMap_关联查询_collection定义关联集合
知识点:查询一个实体类,并查出这个类下面的集合 Employee.java实体类 package com.hand.mybatis.bean;public class Employee { pr ...
- 三、mybatis多表关联查询和分布查询
前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...
- MyBatis关联查询,一对多关联查询
实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...
- mybatis关联查询之一对多查询
一对多,是最常见的一种设计.就是 A 表的一条记录,对应 B 表的多条记录,且 A 的主键作为 B 表的外键.这主要看以哪张表为中心,下面的测试数据中,从employee 表来看,一个员工对应一个部门 ...
- MyBatis 关联查询的实现:一对多
有2个实体:用户.订单,一个用户可以拥有多个订单,同时这多个订单属于一个用户,即一对多. user_tb: order_tb: 在“多”的一方(order)添加“一”的一方(user)的主键(user ...
- MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射
先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...
随机推荐
- struts2随笔
1.struts.properties配置常量等同于struts.xml中配置(置于类加载路径下面)struts.multipart.maxSize文件上传最大大小struts.action.exte ...
- JAVA学习之路(多线程)---模拟售票(细解)
首先看题目描述: 假设有火车票100张,创建4个线程模拟4个售票点,每100ms售出一张,打印出售票过程,格式如下: 窗口3:卖出第100张票 窗口4:卖出第99张票 ............ ... ...
- 由ArrayList来深入理解Java中的fail-fast机制
1. fail-fast简介“快速失败”也就是fail-fast,它是Java集合的一种错误检测机制.某个线程在对collection进行迭代时,不允许其他线程对该collection进行结构上的修改 ...
- 三大图表库:ECharts 、 BizCharts 和 G2,该如何选择?
最近阿里正式开源的BizCharts图表库基于React技术栈,各个图表项皆采用了组件的形式,贴近React的使用特点.同时BizCharts基于G2进行封装,Bizcharts也继承了G2相关特性. ...
- Js的核心:找到DOM
掌握 JavaScript 的核心之一:DOM,能够熟悉DOM相关操作,了解JavaScript事件机制 一.使用getElementById().getElementsByTagName().chi ...
- Strapi 安装易错位置
Strapi官网(https://strapi.io)介绍:最先进的开源内容管理框架,可以毫不费力地构建功能强大的API,建立在Node.js平台之上,为您的API提供高速惊人的表现. 简单点说,(对 ...
- JavaScript练习笔记整理·4 - 6.26
基础练习(1): 我的解答为: function getMiddle(s) { if(s.length%2 == 0) { return s.charAt(s.length/2-1)+s.charAt ...
- 【阿里云产品公测】OpenSearch初体验
OpenSearch是一个非常有意义的功能,对于很多数据量较大的站点, SF2< 如果搜索功能自己做的话,或者用数据库里的查询语句,首先效率低下,而且占用资源. ); <Le6 另 ...
- Scrum团队开发
Scrum学习心得 什么是scrum Scrum是迭代式增量软件开发过程,通常用于敏捷软件开发.Scrum包括了一系列实践和预定义角色的过程骨架.Scrum中的主要角色包括同项目经理类似的Scrum主 ...
- dubbo学习总结一 API
API 一般用来暴露接口 项目分层一般是 api + entity + enums + model 就是接口加上一些实体之类的东西