xml中resultmap标签的作用
时间: 2023-04-15 12:03:01 浏览: 103
resultmap标签的作用是将查询结果集中的列与Java对象的属性进行映射,从而方便程序对查询结果的处理和使用。在resultmap标签中可以定义多个result子标签,每个result子标签对应一个Java对象的属性,通过column属性指定查询结果集中的列名,通过property属性指定Java对象的属性名。resultmap标签还可以定义id、type和extends等属性,用于指定resultmap的唯一标识、Java对象的类型和继承自其他resultmap的属性映射。
相关问题
mybatis中resultMap 标签的使用
在MyBatis中,resultMap标签用于配置实体类与表字段的映射规则。该标签有以下作用:
- 自定义映射规则,通过id属性给映射规则指定一个唯一标识,用于在其他地方引用。
- 使用id标签配置主键映射,指定实体类中的属性和数据库表中的字段的对应关系。
- 使用result标签配置普通字段映射,指定实体类中的属性和数据库表中的字段的对应关系。
举例来说,可以使用resultMap标签来配置一个名为studentmap的映射规则,该规则用于将数据库表中的字段与Student实体类中的属性进行映射。其中,id标签用于配置主键映射,result标签用于配置普通字段映射。例如:
```xml
<resultMap id="studentmap" type="student">
<id property="sid" column="s_id"></id>
<result property="sname" column="s_name"></result>
<result property="sage" column="s_age"></result>
<result property="sphone" column="s_phone"></result>
</resultMap>
```
此外,在核心配置文件中,还可以通过设置lazyLoadingEnabled属性来开启延迟加载。如果使用的是MyBatis 3.41之前的版本,还需要配置aggressiveLazyLoading为true。同时,可以使用resultMap标签来配置一对多的映射关系,将一个教师对象对应多个学生对象。例如:
```xml
<resultMap id="teacher" type="Teacher">
<id property="tid" column="t_id"></id>
<result property="tname" column="t_name"></result>
<result property="tage" column="t_age"></result>
<collection property="students" ofType="student">
<id property="sid" column="s_id"></id>
<result property="sage" column="s_age"></result>
<result property="sname" column="s_name"></result>
<result property="sphone" column="s_phone"></result>
</collection>
</resultMap>
```
此外,可以使用association标签来配置多对一的映射关系,将多个学生对象对应一个教师对象。例如:
```xml
<association property="teacher" javaType="Teacher">
<result property="tid" column="t_id"></result>
<result property="tname" column="t_name"></result>
<result property="tage" column="t_age"></result>
</association>
```
总结起来,resultMap标签在MyBatis中用于配置实体类与表字段的映射规则,可以通过id、result、collection、association等子标签来实现不同类型的映射关系的配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [mybatis中Resultmap标签的的使用](https://blog.csdn.net/weixin_43891398/article/details/125608434)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
mybatis中的resultmap标签
MyBatis中的resultMap标签是用来映射查询结果集中的列名和Java对象属性名之间的关系的。通过resultMap标签,我们可以将查询结果集中的列名映射到Java对象的属性名上,从而方便地将查询结果集转换为Java对象。resultMap标签可以定义在mapper.xml文件中,也可以定义在Java接口中的@Results注解中。在resultMap标签中,我们可以使用id、result、association、collection等子标签来定义映射关系。其中,id子标签用来指定主键列的映射关系,result子标签用来指定普通列的映射关系,association子标签用来指定一对一关系的映射关系,collection子标签用来指定一对多关系的映射关系。通过resultMap标签,我们可以灵活地定义查询结果集的映射关系,从而实现更加高效、灵活的数据访问。