目录
MyBatis
八、自定义映射resultMap
1. 处理字段和属性的映射关系
a> 为字段起别名,保持和属性名的一致
<!-- public List<User> getAllUser(); -->
<select id="getAllUser" resultType="user">
select d_id did from m_user;
</select>
b> 设置全局配置,将_自动映射为驼峰
<settings>
<!-- 是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
c> 通过自定义映射resultMap
resultMap:设置自定义映射
- id:唯一标识
- type:设置映射关系中的实体类类型
- 子标签:
- id:设置主键的映射关系
- result:设置普通字段的映射关系
- 属性:
- property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性名
- column:设置映射关系中的字段名,必须是sql语句查询出的字段名
<resultMap type="user" id="usermap">
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="did" column="d_id"/>
</resultMap>
<!-- public List<User> getAllUser(); -->
<select id="getAllUser" resultMap="usermap">
select * from m_user;
</select>
2. 处理多对一的映射关系
a> 级联属性赋值
<resultMap type="user" id="dusermap">
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="did" column="d_id"/>
<result property="duser.did" column="d_id"/>
<result property="duser.dname" column="d_name"/>
</resultMap>
<!--
@MapKey("id")
public Map<String,Object> getUserAndDuser(@Param("did") int did);
-->
<select id="getUserAndDuser" resultMap="dusermap">
select * from m_user left join d_user on m_user.d_id = d_user.d_id where m_user.d_id = ${did};
</select>
b> association
association:处理多对一的映射关系
- property:需要处理多对的映射关系的属性名
- javaType:该属性的类型
<resultMap type="user" id="dusermap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="did" column="d_id"/>
<association javaType="DUser" property="duser">
<id property="did" column="d_id"/>
<result property="dname" column="d_name"/>
</association>
</resultMap>
c> 分步查询
select:设置分步查询的sql的唯一标识(mapper接口的全类名.方法名)
column:设置分步查询的条件
<!-- DUser getUserAllByStepTwo(@Param("did") int did); -->
<select id="getUserAllByStepTwo" resultType="duser">
select * from d_user where d_id = ${did};
</select>
<resultMap type="user" id="stepmap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="did" column="d_id"/>
<!-- -->
<association property="duser"
select="dao.DUserMapper.getUserAllByStepTwo"
column="d_id">
</association>
</resultMap>
<!-- public List<User> getUserAllByStepOne(@Param("did") int did); -->
<select id="getUserAllByStepOne" resultMap="stepmap">
select * from m_user where d_id = ${did};
</select>
分步查询的好处:实现延迟加载
需要在settings下设置:
lazyLoadingEnabled表示全局的延迟加载开关,true表示所有关联对象都会延迟加载,false表示关闭
aggressiveLazyLoading表示是否加载该对象的所有属性,如果开启则任何方法的调用会加载这个对象的所有属性,如果关闭则是按需加载
<settings> <!-- 是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 开启延迟加载的全局开关 --> <setting name="lazyLoadingEnabled" value="true"/> </settings>
由于这个配置是在核心配置文件中设定的,所以所有的分步查询都会实现延迟加载,而如果某个查询不需要延迟加载,可以在collection标签或者association标签中的fetchType属性设置是否使用延迟加载,属性值lazy表示延迟加载,属性值eager表示立即加载
<!-- select:设置分步查询的sql的唯一标识(mapper接口的全类名.方法名) column:设置分步查询的条件 fetchType:当开启了全局的延迟加载后,可以通过此属性手动控制延迟加载的效果 fetchType="lazy|eager" : lazy表示延迟加载 eager表示立即加载 --> <association property="duser" select="dao.DUserMapper.getUserAllByStepTwo" column="d_id" fetchType="lazy" > </association>
延迟加载的好处:减少数据库压力
MyBatis – 延迟加载MyBatis中的延迟加载 ,也称为懒加载 ,是指在进行关联查询时,按照设置 延迟规则推迟对关联对象的select查询,延迟加载可以有效的减少数据库压力。
注意: MyBatis的延迟加载只是对关联对象的查询有延迟设置,对于主加载对象都是直接执行查询语句的 。
3. 处理一对多的映射关系
在pojo中CUser下添加属性:
private List<User> users;
a> collection
collection:处理一对多的映射关系 property:需要处理多对的映射关系的属性名 ofType:该属性对应的集合中存储数据的类型
<resultMap type="duser" id="dusermap">
<id property="did" column="d_id"/>
<result property="dname" column="d_name"/>
<!--
collection:处理一对多的映射关系
property:需要处理多对的映射关系的属性名
ofType:该属性对应的集合中存储数据的类型
-->
<collection property="users" ofType="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</collection>
</resultMap>
<!-- Map<String,Object> getDUserAll(@Param("did") int did); -->
<select id="getDUserAll" resultMap="dusermap">
select * from d_user left join m_user on m_user.d_id = d_user.d_id where d_user.d_id = ${did};
</select>
b> 分步查询
<resultMap type="duser" id="dstepmap">
<id property="did" column="d_id"/>
<result property="dname" column="d_name"/>
<collection property="users"
select="dao.UserMapper.getDUserAllByStepTwo"
column="d_id"
fetchType="lazy">
</collection>
</resultMap>
<!-- DUser getDUserAllByStepOne(@Param("did") int did); -->
<select id="getDUserAllByStepOne" resultMap="dstepmap">
select * from d_user where d_id = ${did};
</select>