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方式实现的更多相关文章

  1. Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现

    mapper 层 提供 ClazzMapper 和 StudentMapper, ClazzMapper 查询所有班级信息, StudentMapper 根据班级编号查询学生信息. 在 ClazzMa ...

  2. 多表关联查询_resultMap_集合对象

    多表关联查询_resultMap_集合对象_N+1方式实现 package com.bjsxt.mapper; import java.util.List; import com.bjsxt.pojo ...

  3. mybatis多表关联查询之resultMap单个对象

    resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...

  4. 序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询

    查询窗口中可以设置很多查询条件 表单中输入的内容转为datagrid的load方法所需的查询条件向原请求地址再次提出新的查询,将结果显示在datagrid中 转换方法看代码注释 <td cols ...

  5. 面试官:为什么mysql不建议执行超过3表以上的多表关联查询?

    概述 前段时间在跟其他公司DBA交流时谈到了mysql跟PG之间在多表关联查询上的一些区别,相比之下mysql只有一种表连接类型:嵌套循环连接(nested-loop),不支持排序-合并连接(sort ...

  6. mongodb操作之使用javaScript实现多表关联查询

    一.数据控制 mongodb操作数据量控制,千万控制好,不要因为操作的数据量过多而导致失败. 演示一下发生此类错误的错误提示:

  7. ofbiz学习笔记01--多表关联查询

    不管做什么项目,肯定会用到多表关联查询数据,从网络查询得知ofbiz有三种多表关联查询方法 实现一:Screem.xml 中的 section 里,加 <action>, 加 get-re ...

  8. MyBatis 多表关联查询

    多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...

  9. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

随机推荐

  1. VMware虚拟机添加5个RAID10磁盘并挂载

    1.打开虚拟机之前先添加硬盘设备 具体添加方法详见我https://www.cnblogs.com/Ghost-m/p/11707996.html这个博客 制作RAID需要用到madam命令 mdad ...

  2. Linux中文件的SUID、SGID、Sticky权限说明

    1.SUID 首先我们要了解,在Linux中启动一个程序或者启动一个进程是需要有用户的,一个文件的存在是要有用户和组的,一个进程启动后,它的属主取决于进程的发起者,比如 我用root用户启动了一个 c ...

  3. 忘记Linux登录密码的破解方法

    注意:1.破解方式只限于7.0以后的Linux系统. 2.要注意自己linux系统中有没有开启selinux,如果开启则在后面要建一个名为:autorelabel的隐藏文件.     1.启动Linu ...

  4. java实现两个json的深度对比

    两个json的深度对比 在网上找了好多资料都没有找到想要的,还是自己写个吧! 上代码!!! 1.pom.xml中加入 <dependency> <groupId>com.ali ...

  5. lqb 基础练习 数列特征

    基础练习 数列特征 时间限制:1.0s   内存限制:256.0MB     问题描述 给出n个数,找出这n个数的最大值,最小值,和. 输入格式 第一行为整数n,表示数的个数. 第二行有n个数,为给定 ...

  6. setState何时同步,何时异步,为什么?

    setState何时同步,何时异步,为什么 答案:在React库控制时,异步:否则同步. 示例代码如下: constructor(props){ super(porps); this.state = ...

  7. ZeroC ICE源代码中的那些事 - 嵌套类和局部类

    使用嵌套类(类中定义的类,c++没有静态类)或局部类(在函数或成员方法中定义的类),进行行为模式的委托(委托请求)或异步 . java中嵌套类和局部类隐式完成了你对外部对象(实例)访问的私有堆栈的初始 ...

  8. 剑指Offer-25.复杂链表的复制(C++/Java)

    题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否则 ...

  9. Ubuntu 16.04上源码编译Poco并编写cmake文件 | guide to compile and install poco cpp library on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/281dd8cd/,欢迎阅读! guide to compile and install poco cpp library on u ...

  10. ProxySQL读写分离代理

    实现ProxySQL反向代理Mysql读写分离 简介 ProxySQL相当于小型的数据库,在磁盘上有存放数据库的目录:ProxySQL用法和mysql相似 启动ProxySQL后会有两个监听端口: 6 ...