MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现
mapper 层
提供 StudentMapper 和 ClazzMapper, StudentMapper 查询所有学生信息, ClazzMapper 根据编号查询班级信息. 再
StudentMapper 中使用<association>设置装配:(对比resultMap_集合对象_N+1方式实现)
<association>用于关联一个对象
property: 指定要关联的属性名
select: 设定要继续引用的查询, namespace+id
column: 查询时需要传递的列

Mapper包下:
ClazzMapper接口:
package com.bjsxt.mapper;
import com.bjsxt.pojo.Clazz;
public interface ClazzMapper {
Clazz selById(int id);
}
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"/>
</resultMap>
<select id="selById" resultMap="cmap" parameterType="int">
select * from t_class where cid=#{0}
</select>
</mapper>
StudentMapper接口:
package com.bjsxt.mapper;
import java.util.List;
import com.bjsxt.pojo.Student;
public interface StudentMapper {
List<Student> 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"/>
<!-- 用于关联一个对象 -->
<association property="clazz" select="com.bjsxt.mapper.ClazzMapper.selById" column="scid"/>
</resultMap>
<select id="selAll" resultMap="smap">
select * from t_stu
</select>
</mapper>
POJO包下:
Clazz:
package com.bjsxt.pojo;
import java.io.Serializable;
public class Clazz implements Serializable{
private int id;
private String name;
private String room;
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;
}
@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());
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;
return true;
}
@Override
public String toString() {
return "Clazz [id=" + id + ", name=" + name + ", room=" + room + "]";
}
public Clazz() {
super();
}
}
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;
private Clazz clazz;
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;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((clazz == null) ? 0 : clazz.hashCode());
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 (clazz == null) {
if (other.clazz != null)
return false;
} else if (!clazz.equals(other.clazz))
return false;
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;
}
@Override
public String toString() {
return "Student [stid=" + stid + ", stname=" + stname + ", stage=" + stage + ", stsex=" + stsex + ", stcid="
+ stcid + ", clazz=" + clazz + "]";
}
public Student() {
super();
}
}
Service包下:
StudentService接口:
package com.bjsxt.service;
import java.util.List;
import com.bjsxt.pojo.Student;
public interface StudentService {
List<Student> selAll();
}
StudentServiceImp:
package com.bjsxt.service.Impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.bjsxt.mapper.StudentMapper;
import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.util.MyBatisUtil;
public class StudentServiceImpl implements StudentService{
@Override
public List<Student> selAll() {
SqlSession session = MyBatisUtil.getSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.selAll();
session.close();
return list;
}
}
TestSel测试类:
package com.bjsxt.test;
import java.util.List;
import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.service.Impl.StudentServiceImpl;
public class TestSel {
public static void main(String[] args) {
StudentService ss=new StudentServiceImpl();
List<Student> list = ss.selAll();
for (Student student : list) {
System.out.println(student);
}
}
}
运行截图:

数据库截图:

MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现的更多相关文章
- Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现
mapper 层 提供 ClazzMapper 和 StudentMapper, ClazzMapper 查询所有班级信息, StudentMapper 根据班级编号查询学生信息. 在 ClazzMa ...
- 多表关联查询_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 ...
- 面试官:为什么mysql不建议执行超过3表以上的多表关联查询?
概述 前段时间在跟其他公司DBA交流时谈到了mysql跟PG之间在多表关联查询上的一些区别,相比之下mysql只有一种表连接类型:嵌套循环连接(nested-loop),不支持排序-合并连接(sort ...
- mongodb操作之使用javaScript实现多表关联查询
一.数据控制 mongodb操作数据量控制,千万控制好,不要因为操作的数据量过多而导致失败. 演示一下发生此类错误的错误提示:
- ofbiz学习笔记01--多表关联查询
不管做什么项目,肯定会用到多表关联查询数据,从网络查询得知ofbiz有三种多表关联查询方法 实现一:Screem.xml 中的 section 里,加 <action>, 加 get-re ...
- MyBatis 多表关联查询
多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...
- MyBatis学习总结(三)——多表关联查询与动态SQL
在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...
随机推荐
- php修改网站默认编码
php修改网站默认编码网站如果header 不指定utf8默认 不是utf8 所以输入中文显示会乱码 一般都是apache不是不是utf8 打开 apache 配置文件 httpd.conf 加个 A ...
- nsq (三) 消息传输的可靠性和持久化[二]diskqueue
上一篇主要说了一下nsq是如何保证消息被消费端成功消费,大概提了一下消息的持久化,--mem-queue-size 设置为 0,所有的消息将会存储到磁盘. 总有人说nsq的持久化问题,消除疑虑的方法就 ...
- Spring中常用的注解及作用
@Component(value) 配置类,当使用该注解时,SpringIOC会将这个类自动扫描成一个bean实例 不写的时候,默认是类名,且首字母小写 @ComponentScan 默认是代表进行扫 ...
- logback日志回顾整理--2018年8月8日
几年前使用过logback作为项目的日志框架. 当时觉得这个框架比log4j更加好用. 所以系统的学习了一遍. 后来换了公司, 不再使用logback. 如今, 又有机会使用logback了, 所以, ...
- nyoj 79-拦截导弹 (动态规划)
79-拦截导弹 内存限制:64MB 时间限制:3000ms 特判: No 通过数:9 提交数:11 难度:3 题目描述: 某国为了防御敌国的导弹袭击,发展中一种导弹拦截系统.但是这种导弹拦截系统有一个 ...
- asp.net Mvc 使用NPOI导出Excel文件
1.新建MVC项目,新建控制器.视图 添加控制器: 添加视图(将使用布局页前面的复选框里的勾勾去掉) 2.在Models里新建一个类 public class Shop { /// <summa ...
- Centos下的MySQL安装及配置
里使用的是VMware虚拟机和Centos7系统 虚拟机安装这里不多讲,网上教程很多了,这里就介绍下虚拟机的网络配置. 虚拟机网络配置 Centos网络连接模式这里设置为桥接模式,不用勾选复制物理网络 ...
- 你的文章里为什么不放源码Github链接了
"你的文章里为什么不放源码Github链接了?",一个读者这么问我 我把这张图发给了他,这是我之前放文章中Demo源码的Github仓库 他一脸疑惑,问我怎么了 经常使用Githu ...
- react create-react-app使用less 及关闭eslint
使用less和关闭eslint都需要先运行命令 npm run eject 来暴露配置文件,(不可逆的) 一.less使用 运行命令安装less npm install less less-load ...
- 2019-9-19:渗透测试,基础学习,html常用标签,笔记
HTML常用标签<br>:换行<meta charset="utf-8">:编码标签<script type="路径">:插 ...