Mybatis表的关联查询详情
作者:姓蔡小朋 发布时间:2023-11-23 12:15:03
导语
关于<resultMap>标签映射,<association>&<collection>的使用
什么时候用<resultMap>标签映射
1.当我们查询结果的返回值是由对象封装的且对象中封装了另一个对象时,用标签映射: 具体细节见:Mybatis表的关联查询
2.当我们的实体类名与数据库列名不一致时,可以使用标签映射:
什么时候用<association>&<collection>
当我们的实体类中存有另一个实体类对象时,用
<association>
来映射内部的实体类对象。一对一、多对一的关联关系一般用
<association>
。当我们的实体类中存有List或map集合是,用
<collection>
来映射。一对多的关联关系一般用
<collection>
。无论是什么关联关系,如果某方持有另一方的集合,则使用
<collection>
标签完成映射,如果某方持有另一方的对象,则使用<association>
标签完成映射。具体细节见:Mybatis表的关联查询
无论是什么关联关系,如果某方持有另一方的集合,则使用标签完成映射,如果某方持有另一方的对象,则使用标签完成映射。
Mybatis表的关联查询
一对多查询
根据顾客ID查询顾客信息(一),同时将顾客名下所有订单查出(多)。
实现思路:
创建顾客类,存储顾客信息:注意我们在顾客类中用List集合封装了顾客名下的订单信息。
package com.user.pojo;
import java.util.List;
public class Customer {
//customer表中的三个列
private Integer id;
private String name;
private Integer age;
//该客户名下所有订单集合
private List<Order> orderList;
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", orderList=" + orderList +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
public Customer(Integer id, String name, Integer age, List<Order> orderList) {
this.id = id;
this.name = name;
this.age = age;
this.orderList = orderList;
}
public Customer() {
}
}
创建订单类,存储订单信息:
package com.user.pojo;
public class Order {
private Integer id;
private String orderNumber;
private Double orderPrice;
@Override
public String toString() {
return "Order{" +
"id=" + id +
", orderNumber='" + orderNumber + '\'' +
", orderPrice=" + orderPrice +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public Double getOrderPrice() {
return orderPrice;
}
public void setOrderPrice(Double orderPrice) {
this.orderPrice = orderPrice;
}
public Order(Integer id, String orderNumber, Double orderPrice) {
this.id = id;
this.orderNumber = orderNumber;
this.orderPrice = orderPrice;
}
public Order() {
}
}
* 接口:
package com.user.mapper;
import com.user.pojo.Customer;
public interface CustomerMapper {
//根据客户id查询客户所有信息并同时查询该客户名下的所有订单
Customer getById(Integer id);
}
Mapper.xml文件:
因为我们的查询结果包括该顾客信息和顾客所有的订单信息,所以我们的返回值需要自定义一个map集合来存储。我们这里使用 <resultMap>
标签映射的方式创建我们的集合customermap
,并将该集合作为我们的返回值类型。
<?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.user.mapper.CustomerMapper">
<!--
type:map集合中存储的数据单位,是customer对象
id:该map结构的标识符
-->
<resultMap id="customermap" type="customer">
<!--
主键用id标签绑定,非主键用result标签绑定,集合用collection标签绑定
property是customer类中的属性,column是属性对应存储的数据库表中的列名,ofType是集合中存储的数据类型
-->
<!--主键绑定-->
<id property="id" column="id"></id>
<!--非主键绑定-->
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<!--订单数据,不在customer表内-->
<collection property="orderList" ofType="order">
<id property="id" column="id"></id>
<result property="orderNumber" column="orderNumber"></result>
<result property="orderPrice" column="orderPrice"></result>
</collection>
</resultMap>
<select id="getById" parameterType="Integer" resultMap="customermap">
select customer.id ,name,age ,orders.id ,orderNumber ,orderPrice ,customer_id
from customer left join orders on customer.id = orders.customer_id
where customer.id = #{id}
</select>
<!--#{id}为占位符,随便写的,表示所要查询的顾客ID-->
</mapper>
测试程序:
import java.io.InputStream;
import java.util.List;
public class MyTest {
SqlSession sqlSession;
CustomerMapper customerMapper;
@Before
public void openSqlSession() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
customerMapper = sqlSession.getMapper(CustomerMapper.class);
}
@After
public void closeSqlSession(){
sqlSession.close();
}
@Test
public void testGetCustomerById(){
Customer customer = customerMapper.getById(1);
System.out.println(customer);
}
}
其余基础配置文件SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.user.pojo"/>
<package name="com.user.mapper"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.user.mapper"></package>
</mappers>
</configuration>
多对一查询
根据订单ID查询订单信息及该订单所属的顾客的信息。
实现思路:
创建订单类,存储订单信息:注意我们在订单类中封装了该订单下的顾客的信息。
package com.user.pojo;
public class Order {
//订单信息
private Integer id;
private String orderNumber;
private Double orderPrice;
private Integer customer_id;
//订单所属的客户
private Customer customer;
@Override
public String toString() {
return "Order{" +
"id=" + id +
", orderNumber='" + orderNumber + '\'' +
", orderPrice=" + orderPrice +
", customer_id=" + customer_id +
", customer=" + customer +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public Double getOrderPrice() {
return orderPrice;
}
public void setOrderPrice(Double orderPrice) {
this.orderPrice = orderPrice;
}
public Integer getCustomer_id() {
return customer_id;
}
public void setCustomer_id(Integer customer_id) {
this.customer_id = customer_id;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Order(Integer id, String orderNumber, Double orderPrice, Integer customer_id, Customer customer) {
this.id = id;
this.orderNumber = orderNumber;
this.orderPrice = orderPrice;
this.customer_id = customer_id;
this.customer = customer;
}
public Order() {
}
}
创建顾客类,存储顾客信息:
package com.user.pojo;
import java.util.List;
public class Customer {
//顾客信息
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Customer(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Customer() {
}
}
* 接口:
package com.user.mapper;
import com.user.pojo.Customer;
import com.user.pojo.Order;
public interface OrderMapper {
//根据主键查订单并同时查询该订单的客户信息
Order getById(Integer id);
}
Mapper.xml文件:
因为我们的查询结果包括该订单信息和订单所属顾客的信息,所以我们的返回值需要自定义一个map集合来存储。我们这里使用 <resultMap>
标签映射的方式创建我们的集合customermap
,并将该集合作为我们的返回值类型。
<?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.user.mapper.OrderMapper">
<!--
type:map集合中存储的数据单位,是order对象
id:该map结构的标识符
-->
<resultMap id="ordermap" type="order">
<!--
主键用id标签绑定,非主键用result标签绑定,集合用collection标签绑定,
这里没有用collection标签,而是用了association,因为返回的只有一行数据(一个顾客的信息),不需要集合存储
property是order类中的属性,column是属性对应存储的数据库表中的列名,javaType是返回的数据用什么存储
-->
<!--主键绑定-->
<id property="id" column="id"></id>
<!--非主键绑定-->
<result property="orderNumber" column="orderNumber"></result>
<result property="orderPrice" column="orderPrice"></result>
<result property="customer_id" column="customer_id"></result>
<!--顾客数据,不在order表内-->
<association property="customer" javaType="customer">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
<result property="age" column="age"></result>
</association>
</resultMap>
<select id="getById" parameterType="Integer" resultMap="ordermap">
select orders.id ,orderNumber ,orderPrice ,customer_id ,customer.id ,name,age
from orders left join customer on orders.customer_id = customer.id
where orders.id = #{id}
</select>
</mapper>
测试类:
package com.user;
import com.user.mapper.OrderMapper;
import com.user.pojo.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class MyTest {
SqlSession sqlSession;
OrderMapper orderMapper;
@Before
public void openSqlSession() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
orderMapper = sqlSession.getMapper(OrderMapper.class);
}
@After
public void closeSqlSession(){
sqlSession.close();
}
@Test
public void testGetOrderById(){
Order order = orderMapper.getById(11);
System.out.println(order);
}
}
其余基础配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.user.pojo"/>
<package name="com.user.mapper"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.user.mapper"></package>
</mappers>
</configuration>
一对一查询
同多对一。
实体类:
* 接口:
mapper.xml文件:
多对多查询
多对多关联中,需要通过中间表化解关联关系。中间表描述两张主键表的关联。
来源:https://blog.csdn.net/m0_53881899/article/details/126906766


猜你喜欢
- C#连接本地.mdf文件:项目中右键点击,新增——数据——基于服务的数据库,项目下直接生成.mdf数据库文件,后台(数据库的写入用参数传递)
- 配置文件中使用${}注入值方式在springboot中使用System.setProperty设置参数user: user-na
- 一、SpringBoot可以识别4种配置文件1.application.yml2.application.properties3.boots
- 本文实例讲述了java版微信公众平台消息接口应用方法。分享给大家供大家参考,具体如下:微信公众平台现在推出自动回复消息接口,但是由于是接口内
- 在使用fastJson时,对于泛型的反序列化很多场景下都会使用到TypeReference,例如:void testTypeReferenc
- springboot static调用service为null@PostConstruct注解好多人以为是Spring提供的。其实是Java
- 在2020.1.1版本之前IDEA pom文件导包是这样的最近新装新版本IDEA之后,这个图标没有了,对于习惯旧操作没有图标了还真不习惯。就
- 前言众所周知,RxJava2 中当链式调用中抛出异常时,如果没有对应的 Consumer 去处理异常,则这个异常会被抛出到虚拟机中去,And
- SpringMVC RESTFul列表功能实现一、增加控制器方法在控制器类 EmployeeController 中,添加访问列表方法。@C
- 最近在玩3g体育门户客户端的时候,看到这样个效果: 轻触赛事图标,会有一个图标变大浮出的效果.,蛮有意思的.于是就把仿照它做了一
- 主要从以下十几个方面对Hibernate做总结,包括Hibernate的检索方式,Hibernate中对象的状态,Hibernate的3种检
- 像javascript中有eval()来执行动态代码,c#中是没有的,于是自己动手丰衣足食,先来代码using System;using S
- 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁。也就是如果有Synchr
- Java Memory Model简称JMM, 是一系列的Java虚拟机平台对开发者提供的多线程环境下的内存可见性、是否可以重排序等问题的无
- 废话不多说了,直奔主题了。需要两个jar包:commons-fileupload.jarCommons IO的jar包(本文使用common
- 注解是 JDK 5.0 引入的一种注释机制。注解可以作用在类型(类、接口、枚举等)、属性、方法、参数等不同位置,具体的 JDK
- 栈的变化规则:1、方法调用会导致栈的生长,具体包括两个步骤:一、插入方法返回地址(下图中的Fn:);二、将实际参数按值(可以使用ref或ou
- 1.以G71列车为例,首先对车次站台进行占位编码(从1开始到最后一站递加)对以上占位简单描述以下:G71总共18个站点那么我们的单个座位的座
- 一、什么是并查集对于一种数据结构,肯定是有自己的应用场景和特性,那么并查集是处理什么问题的呢?并查集是一种树型的数据结构,用于处理一些不相交
- 一、点睛邻接矩阵通常采用一个一维数组存储图中节点的信息,采用一个二维数组存储图中节点之间的邻接关系。邻接矩阵可以用来表示无向图、有向图和网。