周振林 周振林
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • Spring
  • SpringMVC
  • Mybatis
  • 安装教程
  • 其他教程
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏

周振林

IT界的小学生
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • Spring
  • SpringMVC
  • Mybatis
  • 安装教程
  • 其他教程
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • Spring

  • SpringMVC

  • Mybatis

    • Mybatis CRUD
    • Mybatis参数
    • Mybatis返回类型
      • Mybatis返回类型
        • 返回普通数据
        • 返回List、Map
        • 自定义结果集 - ResultMap
        • association标签
        • association标签 分布查询
        • collection标签
        • collection标签 分布查询
        • 延迟加载
    • Mybatis动态SQL
    • Mybatis分页插件
    • Mybatis 其他
  • 安装教程

  • 其他教程

  • 后端
  • Mybatis
周振林
2025-12-01
目录

Mybatis返回类型

# Mybatis返回类型

# 返回普通数据

返回基本类型、普通对象 都只需要在 resultType 中声明返回值类型全类名即可

<select id="getUser" resultType="com.joe.entity.Employee">
    select * from `user` where id = #{id}
</select>

<select id="countUser" resultType="java.lang.Long">
    select count(*) from `user`
</select>
1
2
3
4
5
6
7

# 返回List、Map

List:resultType为集合中的元素类型

Map:resultType为map,配合@MapKey 指定哪一列的值作为Map的key,Map的Value为这一行数据的完整信息 Map<Key,Map>

  1. 返回单条记录的 Map:resultType="java.util.Map",MyBatis 自动将字段名作为key,字段值作为value
<select id="selectUserMapById" resultType="java.util.Map">
    SELECT id,  user_name, age,create_time 
    FROM user  WHERE id = #{id}
</select>
1
2
3
4
  1. 返回多条记录的 Map:需配合 定义 key 字段,或用 @MapKey 注解
@MapKey("id") // 必须指定:用返回结果中的 "id" 字段作为 Map 的 key
Map<Long, User> selectAllUserMap();

<select id="selectAllUserMap" resultMap="UserMapResult">
    SELECT id, user_name, age, create_time FROM user
</select>
1
2
3
4
5
6

# 自定义结果集 - ResultMap

数据库的字段,如果和Bean的属性不能一一对应,有两种办法

  1. 如果符合驼峰命名,则开启驼峰命名规则
  2. 编写自定义结果集(ResultMap)进行封装
  • id 标签:必须指定主键列映射规则
  • result 标签:指定普通列映射规则
<resultMap id="userResultMap" type="com.joe.entity.Employee">
    <id column="user_id" property="userId"/>
    <result column="user_name" property="userName"/>
    <result column="user_salary" property="userSalary"/>
</resultMap>

<select id="getUser" resultMap="userResultMap">
    select * from  `user` where id = #{id}
</select>
1
2
3
4
5
6
7
8
9

# association标签

association标签:指定自定义对象封装规则

<!--   自定义结果集 -->
<resultMap id="OrderRM" type="com.joe.bean.Order">
    <id column="id" property="id"></id>
    <result column="address" property="address"></result>
    <result column="amount" property="amount"></result>
    <result column="customer_id" property="customerId"></result>
    <!--一对一关联封装  -->
    <association property="customer" javaType="com.joe.bean.Customer">
        <id column="c_id" property="id"></id>
        <result column="customer_name" property="customerName"></result>
        <result column="phone" property="phone"></result>
    </association>
</resultMap>


<!--查询订单以及订单的客户信息-->
<select id="getOrderByIdWithCustomer" resultMap="OrderRM">
select o.*,
    c.id c_id,
    c.customer_name,
    c.phone
from t_order o
    left join t_customer c on o.customer_id = c.id
where o.id = #{id}
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# association标签 分布查询

<!--   分步查询:自定义结果集;封装订单的分步查询  -->
<resultMap id="OrderCustomerStepRM" type="com.joe.bean.Order">
    <id column="id" property="id"></id>
    <result column="address" property="address"></result>
    <result column="amount" property="amount"></result>
    <result column="customer_id" property="customerId"></result>
    <!--
    customer属性关联一个对象,启动下一次查询,查询这个客户 
    property: JaveBean 属性列
    column:订单结果集中哪一列作为查询条件查询客户
    select:查询客户的sql
    -->
    <association property="customer"
                select="com.joe.mapper.OrderCustomerStepMapper.getCustomerById"
                column="customer_id">
    </association>
</resultMap>

<!--   按照id查询客户 -->
<select id="getCustomerById" resultMap="com.joe.entity.Customer">
    select * from t_customer where id = #{id}
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# collection标签

collection标签:指定自定义集合封装规则

<resultMap id="CutomerRM" type="com.joe.bean.Customer">
    <id column="c_id" property="id"></id>
    <result column="customer_name" property="customerName"></result>
    <result column="phone" property="phone"></result>
    <!--
    collection:说明 一对N 的封装规则
    ofType: 集合中元素的类型
    -->
    <collection property="orders" ofType="com.joe.bean.Order">
        <id column="id" property="id"></id>
        <result column="address" property="address"></result>
        <result column="amount" property="amount"></result>
        <result column="c_id" property="customerId"></result>
    </collection>
</resultMap>

<!--查询客户以及客户的订单信息-->
<select id="getCustomerByIdWithOrders" resultMap="CutomerRM">
select c.id c_id,
    c.customer_name,
    c.phone,
    o.*
from t_customer c
    left join t_order o on c.id = o.customer_id
where c.id = #{id}
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# collection标签 分布查询

一对多:查询客户以及客户所有的订单信息

<!--   分步查询的自定义结果集: -->
<resultMap id="CustomerOrdersStepRM" type="com.joe.entity.Customer">
    <id column="id" property="id"></id>
    <result column="customer_name" property="customerName"></result>
    <result column="phone" property="phone"></result>
    <collection property="orders"
                select="com.joe.mapper.OrderCustomerStepMapper.getOrdersByCustomerId"
                column="id">
    </collection>
    <!--    告诉MyBatis,封装 orders 属性的时候,是一个集合,
            但是这个集合需要调用另一个 方法 进行查询;select:来指定我们要调用的另一个方法
            column:来指定我们要调用方法时,把哪一列的值作为传递的参数,交给这个方法
                1)、column="id": 单传参:id传递给方法
                2)、column="{cid=id,name=customer_name}":多传参(属性名=列名);
                    cid=id:cid是属性名,它是id列的值
                    name=customer_name:name是属性名,它是customer_name列的值
    -->

<select id="getCustomerByIdAndOrdersStep" resultMap="CustomerOrdersStepRM">
    select * from t_customer where id = #{id}
</select>

<!--  按照客户id查询他的所有订单-->
<select id="getOrdersByCustomerId" resultType="com.joe.entity.Order">
    select * from t_order where customer_id = #{id}
</select>
</resultMap>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 延迟加载

分步查询 有时候并不需要立即运行,我们希望在用到的时候再去查询,可以开启延迟加载的功能 全局配置:

mybatis.configuration.lazy-loading-enabled=true
mybatis.configuration.aggressive-lazy-loading=false
1
2
Last Updated: 2025/12/02, 11:22:00
Mybatis参数
Mybatis动态SQL

← Mybatis参数 Mybatis动态SQL→

最近更新
01
查询优化N+1
12-02
02
项目代码组织方式
12-02
03
Mybatis分页插件
12-02
更多文章>
Copyright © 2019-2025 鲁ICP备19032096号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×