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 ...
随机推荐
- RocketMQ ACL使用指南
目录 1.什么是ACL? 2.ACL基本流程图 3.如何配置ACL 3.1 acl配置文件 3.2 RocketMQ ACL权限可选值 3.3.权限验证流程 4.使用示例 4.1 Broker端安装 ...
- jquery jssdk分享报错解决方法
jssdk分享报错解决方法 一般都是参数传错了
- python基础-匿名函数和内置函数
匿名函数和内置函数 匿名函数:没有名字,使用一次即被收回,加括号就可以运行的函数. 语法:lambda 参数:返回值 使用方式: 将匿名函数赋值给变量,给匿名函数一个名字,使用这个变量来调用(还不如用 ...
- Servlet相关学习
Servlet入门解析 概念 运行在服务器端的小程序 servlet就是一个接口,定义了Java类被浏览器访问到(tomcat识别)的规则 实现servlet接口.复写方法 快速入门 创建web项目 ...
- maven的项目结构
1.标准目录结构: src -main –bin 脚本库 –java java源代码文件 –resources 资源库,会自动复制到classes目录里 ...
- redis集群节点重启后恢复
服务器重启后,集群报错: [root@SHH-HQ-NHS11S nhsuser]# redis-cli -c -h ip -p 7000ip:7000> set cc dd(error) CL ...
- 那些年用过的UI开发平台
屈指算来,在我不长也不能算短的职业生涯中,接触了数代 的UI技术: MFC (Microsoft Foundation Class)- Win32上最强大的Class Library,没有之一.VS唯 ...
- 【科创人·独家】MegaEase左耳朵耗子陈皓复盘创业:第一年盈利被当骗子,线下广阔天地大有可为
[科创人·独家]MegaEase左耳朵耗子陈皓复盘创业:第一年盈利被当骗子,线下广阔天地大有可为 原创: babayage CTO科创圈 与上百位科技创业者共同关注科创人的成长心路. 文末有彩蛋:& ...
- [增强for循环] 格式
比如:
- 配置SElinux环境,将SELinux设置为enforcing
SELinux是 美国国家安全局 (NSA) 对于 强制访问控制的实现 =>可以使root受限的权限 关闭SELinux=>修改配置文件,永久生效; sed -i 's/SELINUX=e ...