本文主要是介绍MyBatis ResultMap 的基本用法示例详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本...
MyBatis 中的 resultMap
在 MyBatis 中,resultMap
用于 定义数据库查询结果到 Java 对象属性的映射关系。适合处理 复杂的映射关系,如 嵌套对象、集合、一对多或多对一的关系,以及 字段和属性名不一致 的情况。
resultMap
明确指定数据库查询结果(字段)如何映射到 Java 对象的属性。通常,resultMap
被用于 SELECT
查询时的 结果映射,但也可以用于 插入 和 更新 等操作。
1. resultMap 的基本语法
resultMap
通常写在 MyBatis 的 Mapper.XML
文件中,通过 <resultMap>
标签定义。以下是 resultMap
的基本语法结构:
<resultMap id="resultMapId" type="Java类类型"> <result property="属性名" column="列名"/> </resultMap>
id
:resultMap
的唯一标识,可以在select
、insert
等语句中引用。type
:指定查询结果映射的 Java 类型(通常是一个 POJO 类)。property
:Java 对象的属性名。column
:数据库表的列名。
2. 简单的 resultMap 示例
假设数据库表 t_user
和实体类 User
如下:
t_user
表:
CREATE TABLE t_user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), user_email VARCHAR(100), user_age INT );
User
实体类:
public class User { private Integer id; private String username; private String email; private Integer age; // Getters and Setters }
resultMap
示例:
<mapper namespace="com.example.mapper.UserMapper"> <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="email" column="user_email"/> <result property="age" column="user_age"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT id, username, user_email, user_age FROM t_user WHERE id = #{id} </select> </mapper>
<resultwww.chinasem.cnMap>
标签用于定义数据库列和 Java 属性之间的映射。<result>
标签用于指定字段和属性的具体映射关系(**<resultMap>
**中用于具体描述映射关系的子标签)。column
是数据库列名,property
是 Java 实体类的属性。
在这个例子中,当执行查询时,MyBatis 会将 t_user
表中的字段映射到 User
类的属性上。
关于上面的例子,大家可能会有这样的问题,为什么属性名和字段名一样也要写出来,这种情况MyBatis不是会自动映射吗?这其实是出于一种 清晰和规范化的考虑,使用resultMap 的时候尽量把所有属性和字段的映射关系都写出来。详细可以参考:
Mybatis系列第8篇:自动映射,使用需谨慎!
【MyBatis自动映射】为什么即使字段名和属性名一致,使用resultMap时也应该显式列出所有映射?
3. 常用的 resultMap 功能
(1) 处理字段名和属性名不一致
有时数据库的列名与 Java 类的属性名不一致,resultMap
可以帮助处理这种映射。使用 column
来指定数据库中的列名,property
来指定 Java 类的属性名。
例如:
<resultMap id="userResultMap" type="User"> <result property="id" column="user_id"/> <result property="username" column="user_name"/> <result property="email" column="user_email"/> </resultMap&jsgt;
(2) 一对一映射(<association>
)
如果查询的结果需要映射到一个嵌套的对象(如一对一关系),可以使用 <association>
标签。
示例:一对一关系(用户和地址)
假设 t_user
表中有 address_id
字段,表示该用户的地址 ID,而地址信息存储在 t_address
表中。我们需要将查询结果映射为用户和地址的组合。
t_address
表:
CREATE TABLE t_address ( id INT PRIMARY KEY AUTO_INCREMENT, street VARCHAR(100), city VARCHAR(50) );
Address
实体类:
public class Address { private Integer id; private String street; private String city; // Getters and Setters }
User
实体类(修改版):
public class User { private Integer id; private String username; private Address address; // 关联一个 Address 对象 // Getters and Setters }
resultMap
示例:
<mapper namespace="com.example.mapper.UserMapper"> <resultMap id="userResultMap" type="User"> <result property="id" column="user_id"/> <result property="username" column="user_name"/> <association property="address" column="address_id" javaType="Address" select="selectAddressById"/> </resultMap> <selechttp://www.chinasem.cnt id="selectUserById" resultMap="userResultMap"> SELECT user_id, user_name, address_id FROM t_user WHERE user_id = #{id} </select> <select id="selectAddressById" resultType="Address"> SELECT id, street, city FROM t_address WHERE id = #{addressId} </select> </mapper>
- 使用
<association>
标签实现 一对一 映射,property
是 Java 对象中的属性(address
),column
是数据库中关联的列(address_id
)。 javaType
是关联对象的类型(Address
),select
属性指定了通过address_id
查询地址完整信息的 SQL 查询。
原本只能查出
address_id
,现在在userResultMap
中定义把查出来的address_id
映射到address
,但是一个Integer
类的id怎么赋值给Address
类?所以需要在association
的select
属性中指定根据这个id去对应表中查询出对应实体类的全部信息,然后再赋值,这就是为什么还要写一个selectAddressById
的select
语句。
(3) 一对多映射(<collection>
)
如果查询的结果是多个对象(如一对多关系),可以使用 <collection>
标签。
示例:一对多关系(用户和订单)
假设 t_user
表与 t_order
表之间有一对多关系,即一个用户有多个订单。我们可以使用 resultMap
和 <collection>
来映射多个订单。
t_order
表:
CREATE TABLE t_order ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, order_date DATE );
Order
实体类:
public class Order { private Integer id; private Date orderDate; // Getters and Setters }
User
实体类(修改版):
public class User { private Integehttp://www.chinasem.cnr id; private String username; private List<Order> orders; // 一对多的订单列表 // Getters and Setters }
resultMap
示例:
<mapper namespace="com.example.mapper.UserMapper"> <resultMap id="userResultMap" type="User"> <result property="id" column="user_id"/> <result property="username" column="user_name"/> <collection ofType="Order" property="orders" select="selectOrdersByUserId" column="user_id"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT user_id, user_name FROM t_user WHERE user_id = #{id} </select> <select id="selectOrdersByUserId" resultType="Order"> SELECT id, order_date FROM t_order WHERE user_id = #{userId} </select> </mapper>
<collection>
标签用于一对多的关系映射。property
对应 Java 类中的集合属性(如orders
),ofType
指定集合元素的类型(如Order
),select
指定查询订单的 SQL。collection ofType="Order"
:字面意思就是关于“Order”的集合property="orders"
,column="user_id"
:表示我们要从查询出的user_id
映射到orders
属性,但是一个是Integer
类,一个是List
类,怎么对应?select="selectOrdersByUserId"
:所以用指定一个查询语句,我们可以根据这个Integer
类的id(user_id
)查询出List
类(这个用户对应的所有订单的List)
4. 总结
resultMap
是 MyBatis 中强大的映射工具,能够帮助我们处理复杂的 字段到属性的映射关系,如一对一、一对多、嵌套对象等。- 对于 简单的字段映射,我们可以使用
@Results
注解;但对于 复杂的关联关系,resultMap
给予我们更大的灵活性。 <association>lYBqlLDhX
和<collection>
是resultMap
中常用的标签,用于处理 一对一 和 一对多 的关系映射。- 使用
resultMap
可以让查询结果映射更明确和可控,尤其适用于多表联查或关系复杂的场景。
到此这篇关于MyBatis ResultMap 的基本用法的文章就介绍到这了,更多相关MyBatis ResultMap用法内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于MyBatis ResultMap 的基本用法示例详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!