Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现
mapper 层
提供 ClazzMapper 和 StudentMapper, ClazzMapper 查询所有班级信息, StudentMapper 根据班级编号查询学生信息.
在 ClazzMapper 中使用<collection>设置装配.
<collection>用于关联一个集合
property: 指定要关联的属性名
select: 设定要继续引用的查询, namespace+id
column: 查询时需要传递的列
直接上代码(准备俩张表,学生表和班级表,学生表的外键是班级表的主键 jar包和基本配置文件还是和MyBatis动态查询一样):

俩个实体类:
1.Clazz
package com.bjsxt.pojo;
import java.io.Serializable;
import java.util.List;
public class Clazz implements Serializable{
/**
*
*/
private static final long serialVersionUID = -5195063390556612327L;
private int id;
private String name;
private String room;
private List<Student> stus;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public List<Student> getStus() {
return stus;
}
public void setStus(List<Student> stus) {
this.stus = stus;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((room == null) ? 0 : room.hashCode());
result = prime * result + ((stus == null) ? 0 : stus.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Clazz other = (Clazz) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (room == null) {
if (other.room != null)
return false;
} else if (!room.equals(other.room))
return false;
if (stus == null) {
if (other.stus != null)
return false;
} else if (!stus.equals(other.stus))
return false;
return true;
}
@Override
public String toString() {
return "Clazz [id=" + id + ", name=" + name + ", room=" + room + ", stus=" + stus + "]";
}
public Clazz() {
super();
}
}
2.Student类:
package com.bjsxt.pojo;
import java.io.Serializable;
public class Student implements Serializable{
private int stid;
private String stname;
private int stage;
private String stsex;
private int stcid;
@Override
public String toString() {
return "Student [stid=" + stid + ", stname=" + stname + ", stage=" + stage + ", stsex=" + stsex + ", stcid="
+ stcid + "]";
}
public Student() {
super();
}
public int getStid() {
return stid;
}
public void setStid(int stid) {
this.stid = stid;
}
public String getStname() {
return stname;
}
public void setStname(String stname) {
this.stname = stname;
}
public int getStage() {
return stage;
}
public void setStage(int stage) {
this.stage = stage;
}
public String getStsex() {
return stsex;
}
public void setStsex(String stsex) {
this.stsex = stsex;
}
public int getStcid() {
return stcid;
}
public void setStcid(int stcid) {
this.stcid = stcid;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + stage;
result = prime * result + stcid;
result = prime * result + stid;
result = prime * result + ((stname == null) ? 0 : stname.hashCode());
result = prime * result + ((stsex == null) ? 0 : stsex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (stage != other.stage)
return false;
if (stcid != other.stcid)
return false;
if (stid != other.stid)
return false;
if (stname == null) {
if (other.stname != null)
return false;
} else if (!stname.equals(other.stname))
return false;
if (stsex == null) {
if (other.stsex != null)
return false;
} else if (!stsex.equals(other.stsex))
return false;
return true;
}
}
ClazzMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.ClazzMapper">
<resultMap type="clazz" id="cmap">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
<result property="room" column="croom"/>
<collection property="stus" select="com.bjsxt.mapper.StudentMapper.selBystcid" column="cid"></collection>
</resultMap>
<select id="selAll" resultMap="cmap">
select * from t_class
</select>
</mapper>
ClazzMapper接口:
package com.bjsxt.mapper;
import java.util.List;
import com.bjsxt.pojo.Clazz;
public interface ClazzMapper {
List<Clazz> selAll();
}
StudentMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<resultMap type="student" id="smap">
<id property="stid" column="sid"/>
<result property="stname" column="name"/>
<result property="stage" column="age"/>
<result property="stsex" column="sex"/>
<result property="stcid" column="scid"/>
</resultMap>
<select id="selBystcid" resultMap="smap" parameterType="int">
select * from t_stu where scid=#{0}
</select>
</mapper>
StudentMapper接口:
package com.bjsxt.mapper;
import java.util.List;
import com.bjsxt.pojo.Student;
public interface StudentMapper {
List<Student> selBystcid(int stcid);
}
Service接口(ClazzService 业务装配):
package com.bjsxt.service;
import java.util.List;
import com.bjsxt.pojo.Clazz;
public interface ClazzService {
List<Clazz> selAll();
}
ServiceImpl(ClazzServiceIml):继承ClazzService 实现它的方法:
package com.bjsxt.service.Impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.bjsxt.mapper.ClazzMapper;
import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.util.MyBatisUtil;
public class ClazzServiceImpl implements ClazzService{
@Override
public List<Clazz> selAll() {
SqlSession session = MyBatisUtil.getSession();
ClazzMapper mapper = session.getMapper(ClazzMapper.class);
List<Clazz> list = mapper.selAll();
session.close();
return list;
}
}
测试类(TestSel):
package com.bjsxt.test;
import java.util.List;
import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.service.Impl.ClazzServiceImpl;
public class TestSel {
public static void main(String[] args) {
ClazzService cs=new ClazzServiceImpl();
List<Clazz> list = cs.selAll();
for (Clazz clazz : list) {
System.out.println(clazz);
}
}
}
运行结果截图:

数据库查询截图:

Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现的更多相关文章
- MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现
mapper 层 提供 StudentMapper 和 ClazzMapper, StudentMapper 查询所有学生信息, ClazzMapper 根据编号查询班级信息. 再 StudentMa ...
- 多表关联查询_resultMap_集合对象
多表关联查询_resultMap_集合对象_N+1方式实现 package com.bjsxt.mapper; import java.util.List; import com.bjsxt.pojo ...
- mybatis多表关联查询之resultMap单个对象
resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...
- 序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
查询窗口中可以设置很多查询条件 表单中输入的内容转为datagrid的load方法所需的查询条件向原请求地址再次提出新的查询,将结果显示在datagrid中 转换方法看代码注释 <td cols ...
- mongodb操作之使用javaScript实现多表关联查询
一.数据控制 mongodb操作数据量控制,千万控制好,不要因为操作的数据量过多而导致失败. 演示一下发生此类错误的错误提示:
- MyBatis 多表关联查询
多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...
- MyBatis学习总结(三)——多表关联查询与动态SQL
在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...
- 三、mybatis多表关联查询和分布查询
前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...
- ofbiz学习笔记01--多表关联查询
不管做什么项目,肯定会用到多表关联查询数据,从网络查询得知ofbiz有三种多表关联查询方法 实现一:Screem.xml 中的 section 里,加 <action>, 加 get-re ...
随机推荐
- 项目——基于httpd镜像演示Dockerfile所有的指令
基于httpd镜像演示Dockerfile所有的指令: 第一步:创建Dockerfile工作目录 [root@localhost harbor]# mkdir /test [root@localhos ...
- Hbase简介以及简单的入门操作
Hbase是一个分布式的.面向列的开源数据库,可实时的读写.随机访问超大规模的数据集. Hbase主要分为两种模型: 逻辑模型和物理模型 1. 逻辑模型 Hbase的名字的来源是Hadoop data ...
- tornado的使用-数据库篇
tornado的使用-数据库篇
- 设置Ubuntu下vim缩进为4个空格
输入命令编辑vim配置文件: sudo vim /etc/vim/vimrc 文件中添加如下: 保存退出即可
- nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- PHP的global和$GLOBALS的区别
global是关键字,通常添加在变量前,可以使变量的作用域为全局. $GLOBALS预定义的超全局变量,把变量扔到里面一样可以变成全局变量. $GLOBALS 是一个关联数组,每一个变量为一个元素,键 ...
- 学习记录:《C++设计模式——李建忠主讲》5.“对象性能”模式
对象性能模式:面向对象很好地解决了抽象地问题,但是必不可免地要付出一定地代价.对于通常情况来讲,面向对象地成本大都可以忽略不计,但某些情况,面向对象所带来地成本必须谨慎处理. 典型模式:单件模式(Si ...
- SpringBoot系列之i18n集成教程
目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...
- 如何打造一款m3u8视频爬虫
0.前言 m3u8是一种很常见的网页视频播放器的视频源,比如说中国大学MOOC中课程就是使用了该种视频格式. 随便打开一门课程,就可以发现在网络请求中存在一个m3u8的文件,在preview中预览,它 ...
- 阅读《Windows 黑客编程技术详解》(甘迪文著)【正在进行】
内容提要: 本书介绍的是黑客编程的基础技术,涉及用户层下的Windows编程和内核层下的Rootkit编程. 全书大纲: 第一篇 用户篇 平常计算机上使用的应用程序,都运行在用户层上,属于用户程序.在 ...