目录
[2.1. 订单实体类](#2.1. 订单实体类)
[2.2. 用户实体类](#2.2. 用户实体类)
[2.3. UserMapper](#2.3. UserMapper)
[2.4. UserMapper.xml](#2.4. UserMapper.xml)
[2.5. sqlMybatisConfig.xml](#2.5. sqlMybatisConfig.xml)
[2.6. properties](#2.6. properties)
[2.7. UserService](#2.7. UserService)
一、引言
在上一章节中我们讲到了日常业务场景开发中碰到的一对一查询,其中就提到了关于用户和订单表的例子,一笔订单关联一个用户,那么反过来一个用户其实会对应多笔订单,这就是我们本章要讲解的一对多查询。在MyBatis中,一对多查询也是通过配置映射文件中的<resultMap>来实现的。
一对多查询模型
用户表和订单表的反向关系为:
一个订单只从属于一个用户,但是一个用户有多个订单,用户表id关联多个订单表uid。
一对多查询的需求:查询一个用户,与此同时查询出该用户关联的所有订单。
二、完整代码实现
2.1. 订单实体类
java
package com.example.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private int id;
private Date orderTime;
private double total;
// 代表当前订单从属于哪一个用户
private User user;
}
2.2. 用户实体类
java
package com.example.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.Date;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private int id;
private String username;
private String password;
private Date birthday;
/**
* 描述的是当前用户存在哪些订单
*/
private List<Order> orderList;
}
2.3. UserMapper
java
package com.example.mapper;
import com.example.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
List<User> findAll() throws IOException;
}
2.4. UserMapper.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="userMap" type="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!-- 配置集合信息
property: 集合名称
ofType: 当前集合的数据类型
-->
<collection property="orderList" ofType="order">
<id column="oid" property="id"></id>
<result column="ordertime" property="orderTime"></result>
<result column="total" property="total"></result>
<result column="oid" property="id"></result>
</collection>
</resultMap>
<!-- 查询操作 -->
<select id="findAll" resultMap="userMap">
select * from user u, `order` o where u.id = o.uid
</select>
</mapper>
2.5. sqlMybatisConfig.xml
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 加载外部properties -->
<properties resource="jdbc.properties"></properties>
<settings>
<!-- 其他配置 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<typeAlias type="com.example.domain.User" alias="user"></typeAlias>
<typeAlias type="com.example.domain.Order" alias="order"></typeAlias>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定方言 MySQl -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
<!-- 数据源环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
<mapper resource="com/example/mapper/OrderMapper.xml"/>
</mappers>
</configuration>
2.6. properties
XML
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=Wangzhexiao
2.7. UserService
java
package com.example.service;
import com.example.domain.User;
import com.example.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserService {
public static void main(String[] args) throws IOException {
// 获得核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 获得Session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得Session会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取Dao代理对象,Mybatis内部已经帮我们完成了实现(与我们传统方式代码中的实现类相似)
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> list = mapper.findAll();
for (User user : list) {
System.out.println(user);
}
}
}