高级结果映射
需求:
查询宠物信息,关联查询类别信息。
数据库建立:
Pets表
Types表
思路:
若想在不改变实体类字段的基础上,同时查询两张表的信息,我们就需要创建一个拓展类来将类增强(本文将以Pets类为主类,创建一个增强类PetsExt类,实现Pets类,添加上Types类中字段)
案例
1.Pets类中字段
private int id;
private String name;
private Date birthDate;
private int typeId;
private Types types;
2.Types类中字段
private int id;
private String name;
3.PetsExt类(因为Pets类中存在id和name字段,则需要将Types类中的字段重命名)
public class PetsExt extends Pets{
/*
* 额外的字段
* */
private int tid;
private String tname;
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "PetsExt [tid=" + tid + ", tname=" + tname + ","+super.toString()+"]";
}
}
4.PetsMapper接口
public interface PetsMapper {
public List<PetsExt> findAllPetAndTypes();
public List<Pets> findAllPetAndTypes1();
}
5.PetsMapper.xml配置文件(映射文件名要和接口方法名一致)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qf.mapper.PetsMapper">
<!-- resultMap(1)查询 -->
<resultMap type="com.qf.bean.PetsExt" id="MyResultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="birthDate" column="birth_date"/>
<result property="typeId" column="type_id"/>
<result property="tid" column="tid"/>
<result property="tname" column="tname"/>
</resultMap>
<!-- resultMap(1)查询 -->
<select id="findAllPetAndTypes" resultMap="MyResultMap">
SELECT p.*,t.id tid,t.name tname
FROM pets p,TYPES t
WHERE p.type_id=t.id
</select>
<!--resultType查询 -->
<select id="findAllPetAndTypes" resultType="com.qf.bean.PetsExt">
SELECT p.id,p.name,p.birth_date birthDate,p.type_id typeId,t.id tid,t.name tname
FROM pets p,TYPES t
WHERE p.type_id=t.id
</select>
<!-- resultMap(2)查询 -->
<resultMap type="com.qf.bean.Pets" id="MyPetsResultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="birthDate" column="birth_date"/>
<result property="typeId" column="type_id"/>
<!-- 类型对象 -->
<association property="types" javaType="com.qf.bean.Types">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
<!-- resultMap(2)查询 -->
<select id="findAllPetAndTypes1" resultMap="MyPetsResultMap">
SELECT p.id,p.name,p.birth_date,p.type_id,t.id tid,t.name tname
FROM pets p,TYPES t
WHERE p.type_id=t.id
</select>
</mapper>
注意:映射文件中,方法的返回值可以有两种方式。
resultType:当返回值为resultType时,实体类中的字段名需要和数据库中的一致,若不一致,则需在查询时设置别名来保持一致
resultMap:
①在配置resultMap时,可以设置类型为PetsExt,然后一个字段一个字段映射
②可以设置类型为Pets,association通过添加Types对象(一个宠物对应一个类别)
③同理也可以建立一个TypesExt类,来实现一个类型有多个宠物
<resultMap type="com.tf.domain.Types" id="typesMap">
<id property="id" column="id"/>
<result property="name" column="name"></result>
<!-- 配置集合 -->
<collection property="pets" ofType="com.tf.domain.Pets">
<id property="id" column="pid"/>
<result property="name" column="pname"></result>
<result property="birthDate" column="birthDate"></result>
</collection>
</resultMap>
<select id="findOneTypes" resultMap="typesMap">
select t.*,p.id pid,p.name pname,p.birth_date birthDate
from types t
left join pets p
on t.id=p.type_id
</select>
延迟加载
延迟加载又叫懒加载,也叫按需加载。也就是说先加载主信息,在需要的时候,再去加载从信息。
在mybatis中,resultMap标签 的
association标签(配置单个对象)
collection标签(配置集合)具有延迟加载的功能。
设置延迟加载
mybatisConfig.xml中配置settings标签
<settings>
<!--打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 配置懒加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 积极懒加载 (按需加载)-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>