对象属性存在对象或者list对象mybatis如何配置映射

本文详细介绍了如何使用MyBatis的association和collection标签进行复杂对象的关联映射,通过具体的代码示例展示了如何配置一对多关系,以及如何通过嵌套查询实现数据的级联加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

YuanGong.java 对象有以下属性

	private int id;
	private String name;
	private String sal;
	private String sex;
	private Person person;
	private List<Person> pList;

Person.java对象

	private int id;
	private String name;
	private int age;
	private String sex;
	private Date date;

这里假设person的id,name跟YuanGong的id和name有关联,也就是说用yuangong表里面的id或者name能从perosn表里查到记录,且有一对多(所以会存在list属性)。

mybatis 配置 yuangong

<resultMap type="entiry.YuanGong" id="YGmapper">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sal" column="sal"/>
<result property="sex" column="sex"/>
<result property="sex" column="sex"/>
<association property="person" column="{tid=id,tname=name}" select="selectPerson"/>
 <collection property="pList" ofType="entiry.Person"
  select="selectPersonList" column="name" javaType="ArrayList"/>
</resultMap>

person 配置

<resultMap type="entiry.Person" id="personM">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="date" column="date" jdbcType="DATE"/>
</resultMap>

两个select

<select id="selectPerson" parameterType="java.util.Map" resultMap="personM">
select * from PERSON where id=#{tid} and name=#{tname}
</select>

<select id="selectPersonList" parameterType="java.lang.String" resultMap="personM">
select * from PERSON where name=#{name}
</select>

查询例子

<select id="selectEmp" parameterType="entiry.YuanGong" resultMap="YGmapper">
select * from yuangong
</select>

其实这里配置映射主要就是association 跟collection 标签里面这个select=“selectPerson"属性
column=”{tid=id,tname=name}"表示
map.put(“tid”,yuangong.id);
map.put(“tname”,yuangong.name);
然后执行

<select id="selectPerson" parameterType="java.util.Map" resultMap="personM">
select * from PERSON where id=#{tid} and name=#{tname}
</select>

传入的map里面就有对应的tid值跟tname值了。

执行selectEmp,
可以看到日志分别执行了三段sql
select * from yuangong;
select * from PERSON where name=#{name};
select * from PERSON where id=#{tid} and name=#{tname};
debugger模式下可以看到yuangong对象属性都有值了

### MyBatis 对象映射使用指南 #### 1. 映射基础概念 MyBatis 是一种持久层框架,它通过 XML 或注解的方式将 Java 方法与 SQL 查询关联起来。对象映射是指将数据库表的数据转换成 Java 实体类的对象实例的过程。 在 MyBatis 中,`<resultMap>` 和 `<sql>` 标签用于定义复杂的结果集映射和可重用的 SQL 片段[^1]。对于简单的场景可以直接使用 `#{} `参数占位符以及 `<select>`, `<insert>`, `<update>`, `<delete>` 等标签来完成基本操作。 #### 2. POJO 类设计 为了使 MyBatis 能够正确地进行对象映射,通常需要创建对应的 Plain Old Java Object (POJO) 来表示数据模型。这些实体类应该包含属性名与数据库字段相匹配,并提供 getter/setter 方法以便访问器能够读取或设置值。 ```java public class User { private Integer id; private String name; // Getters and Setters... } ``` #### 3. Mapper 接口编写 Mapper 接口用来声明 CRUD 操作的方法签名,不需要具体实现因为它们会在运行期间由 MyBatis 动态生成。接口中的每一个方法都对应着一条或多条SQL语句,在执行时会自动处理输入输出参数之间的映射关系。 ```java @Mapper public interface UserMapper { /** * 获取指定ID用户的列表. */ List<User> getUserById(@Param("id") Integer id); // Other methods... } ``` 上述代码片段展示了如何利用 `@Mapper` 注解标记一个接口作为 MyBatisMapper 接口,其中包含了获取特定 ID 用户信息的功能[^2]。 #### 4. 结果映射配置 当涉及到复杂的查询逻辑或者存在多表联结的情况下,则可能需要用到 resultMap 进行更精细控制。下面是一个例子说明怎样为上面提到的 `User` 表建立结果映射: ```xml <!-- resources/mapper/UserMapper.xml --> <mapper namespace="com.example.mapper.UserMapper"> <!-- 定义用户实体到列的关系 --> <resultMap type="User" id="BaseResultMap"> <id column="id" property="id"/> <result column="name" property="name"/> </resultMap> <!-- 使用 resultMap 属性引用之前定义好的 BaseResultMap --> <select id="getUserById" parameterType="int" resultMap="BaseResultMap"> SELECT * FROM users u WHERE u.id = #{id}; </select> </mapper> ``` 这段XML文档描述了一个名为 `UserMapper` 的命名空间下的 SQL 映射规则,其中包括了对 `users` 表的操作。特别是 `getUserById` 方法被设定为返回单个记录并将其映射至 `User` 实体上。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值