resultMap与resultType
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中
数据库新建一个t_order表


新建一个Order类
package com.mu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private int oid;
private int userid;
private String number;
private Date createtime;
private String note;
}
新增一个接口OrderDao
**package com.mu.dao;
import com.mu.pojo.Order;
import java.util.List;
public interface OrderDao {
//查询所有订单
List<Order> selectAll();
//根据userid查询订单
List<Order> selectByuserid(int userid);
}**
编写OrderMapper文件
因为user_id字段与Order类的userid属性不同,所以需要用到resultMap来手动配置映射
<?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.mu.dao.OrderDao">
<!-- resultMap最终还是要将结果映射到pojo类上,type就是指定映射到哪一个pojo类 -->
<!-- id:设置ResultMap的id -->
<resultMap type="com.mu.pojo.Order" id="orderResultMap">
<!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
<!-- property:主键在pojo中的属性名 -->
<!-- column:主键在数据库中的列名 -->
<id property="oid" column="oid" />
<!-- 定义普通属性 -->
<result property="userid" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</resultMap>
<!-- 查询所有的订单数据 -->
<!-- resultMap:填入配置的resultMap标签的id值 -->
<select id="selectAll" resultMap="orderResultMap">
SELECT * FROM t_order
</select>
<select id="selectByuserid" resultMap="orderResultMap" parameterType="int">
SELECT * FROM t_order WHERE user_id = #{userid}
</select>
</mapper>
全局配置文件中加载

添加一个测试类MyTest4
package com.mu;
import com.mu.dao.OrderDao;
import com.mu.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.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest4 {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
}
@Test
public void Test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrderDao mapper = sqlSession.getMapper(OrderDao.class);
System.out.println("查询所有订单----->>>");
List<Order> orderList = mapper.selectAll();
orderList.forEach(System.out::println);
System.out.println("根据用户id查询----->>>");
orderList.clear();
orderList = mapper.selectByuserid(21);
orderList.forEach(System.out::println);
sqlSession.close();
}
}
测试结果


关联查询
使用resultMap标签以及association和collection子标签,进行关联查询
一对一查询
一个订单信息只会是一个人下的订单,从查询订单信息出发关联查询用户信息为一对一查询。
在Order类中添加User属性,User属性是一个引用类型,用于存储关联查询的用户信息,因为关联关系是一对一,所以只需要添加单个属性即可
OrderDao接口中添加方法
//根据订单号id查询订单,以及下单用户的属性
Order selectByid(int id);
OrderMapper中添加
<!-- 一对一查询 根据订单查询关联的用户信息-->
<resultMap type="com.mu.pojo.Order" id="orderUserResultMap">
<id property="oid" column="oid" />
<result property="userid" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
<!-- association :配置一对一属性 -->
<!-- property:order里面的User属性名 -->
<!-- javaType:属性类型 -->
<association property="user" javaType="com.mu.pojo.User">
<!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
<id property="id" column="user_id"/>
<result property="name" column="name"/>
<result property="pwd" column="pwd"/>
<result property="age" column="age"/>
<result property="phone" column="phone"/>
</association>
</resultMap>
<select id="selectByid" resultMap="orderUserResultMap" parameterType="int">
SELECT o.* ,u.*
FROM t_order o
join user u
on o.user_id = u.id
WHERE o.oid =#{id}
</select>
添加测试
@Test
public void Test2() {
SqlSession sqlSession2 = sqlSessionFactory.openSession();
OrderDao mapper2 = sqlSession2.getMapper(OrderDao.class);
System.out.println(mapper2.selectByid(3));
sqlSession2.close();
}
测试

一对多查询
一个用户可以下多个订单。从用户信息出发查询用户下的订单信息则为一对多查询。
在User类中添加Order集合

OrderDao接口中添加方法
//根据用户名字查询该用户的所有订单信息
User selectByname(String name);
OrderMapper中添加
<!-- 一对多关联,根据用户名字查询该用户下的订单 -->
<select id="selectByname" resultMap="userOrderResultMap">
SELECT
u.*,o.*
FROM
user u
LEFT JOIN
t_order o
ON
u.id = o.user_id
WHERE
u.name = #{name}
</select>
<resultMap type="com.mu.pojo.User" id="userOrderResultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="pwd" column="pwd"/>
<result property="age" column="age"/>
<result property="phone" column="phone"/>
<!-- 配置一对多的关系
property:填写pojo类中集合类类属性的名称
javaType:填写集合类型的名称
-->
<collection property="orders" javaType="List" ofType="com.mu.pojo.Order">
<!-- 配置主键,是关联Order的唯一标识 -->
<id property="oid" column="oid" />
<result property="userid" column="user_id" />
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
添加测试
@Test
public void Test3() {
SqlSession sqlSession3 = sqlSessionFactory.openSession();
OrderDao mapper3 = sqlSession3.getMapper(OrderDao.class);
User user = mapper3.selectByname("孙九");
//用get属性获取该用户的订单集合
List<Order> orderList = user.getOrders();
orderList.forEach(System.out::println);
sqlSession3.close();
}
测试结果

总结
resultType:
作用: 将查询结果按照sql列名pojo属性名一致性映射到pojo中。
场合: 常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。
resultMap:
使用association和collection完成一对一和一对多高级映射(对结果有特殊的映射要求)。
association:
作用: 将关联查询信息映射到一个pojo对象中。
场合: 为了方便查询关联信息可以使用association将关联订单信息映射为用户对象的pojo属性中,比如:查询订单及关联用户信息。使用resultType无法将查询结果映射到pojo对象的pojo属性中,根据对结果集查询遍历的需要选择使用resultType还是resultMap。
collection:
作用 : 将关联查询信息映射到一个list集合中。
场合: 为了方便查询遍历关联信息可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块及模块下的菜单,可使用collection将模块映射到模块list中,将菜单列表映射到模块对象的菜单list属性中,这样的作的目的也是方便对查询结果集进行遍历查询。
使用resultMap是针对那些对查询结果映射有特殊要求的功能,,比如特殊要求映射成list中包括多个list。
本文详细介绍了Mybatis中的ResultMap及其在一对一、一对多查询中的应用。通过resultMap,可以解决字段名与Pojo属性名不一致的问题,并实现复杂类型的对象映射。举例说明了如何配置resultMap,以及association和collection在关联查询中的作用和应用场景。
&spm=1001.2101.3001.5002&articleId=113405657&d=1&t=3&u=45799dbbd0414e43a719282ea395aae1)
894

被折叠的 条评论
为什么被折叠?



