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,在执行查询时就出 ...
随机推荐
- 从ExtensionLoader理解Dubbo扩展机制
Dubbo的扩展机制是怎么实现的?最简单的回答就是@SPI. Dubbo的插件化思路来源于Java SPI. JAVA SPI 机制 SPI的全名为Service Provider Int ...
- js权威指南学习笔记(四)对象
1.创建对象 (1).通过对象直接量的方式创建 说明:对象直接量是由若干名/值对组成的映射表,名/值对中间用冒号分隔,名/值对之间用逗号分隔,整个映射表用花括号括起来. 如: 5 5 ...
- webservice使用windows身份验证,ajax请求报错401未授权的解决办法
$.ajax({ type: "GET", url: service_url, dataType: "xml", data: "ParamId=&qu ...
- iview中事件获取自定义参数
前提:页面渲染多个cascaer组件,根据不同cascader组件选中的值,加载不同内容 解决:此时需要解决的问题是,在on-change事件中返回index索引使用如下格式: <Cascade ...
- 表格 滚动条 (tbody部分滚动)
本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 html <table> <thead> < ...
- cnpm 安装
国内npm 安装比较慢,可选择cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org
- IntelliJ IDEA开发工具println报错的解决方法
IntelliJ IDEA 编译 JSP,出现 out.println 报错,下图所示: 报错原因:println报红,这是因为没有关联好服务器! 解决方案:点击File->Project st ...
- postgres备份数据库
1. psql --help psql is the PostgreSQL interactive terminal. Usage: psql [OPTION]... [DBNAME [USERNAM ...
- .NET开源工作流RoadFlow-Bug修改-1.8.2子流程接收者始终为发送者
1.8.2及以前版本中子流程待办任务的处理者始终为上一步骤发送者BUG的处理: 修改类:RoadFlow.Platform.WorkFlowTask中如下图红框中的内容即可:
- Http重要知识点