作用:在Mybaits中association标签是用来实现连表查询的。
使用的背景:association的使用场景为1:1和n:1两种情况。
添加的内容:使用association的时候需要在类中添加关联对象。
下面分情况来具体看一下吧。
一、第一种情况:
一对一: 一个人只能有一张身份证,一张身份证只能对应一个人
查询信息:查询人信息的时候返回身份证信息
1、People类中需要额外添加IdentityCard对象作为两个表的关联条件
@Data
public class People {
private Long id;
private String peopleName;
private String peopleIdentityCardId;
private IdentityCard identityCard;//关联对象
}
IdentityCard类中不需要添加额外字段
@Data
public class IdentityCard {
private Long id;
private String identityCardId;
private String identityCardName;
@JsonFormat(pattern="yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
private LocalDateTime identityCardBirthday;
private String identityCardAddress;
}
2、PeopleMapper.xml中的代码如下
<resultMap id="BaseResultMapWithIdentityCard" type="com.example.demo.entity.People">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="people_name" jdbcType="VARCHAR" property="peopleName" />
<result column="people_identity_card_id" jdbcType="VARCHAR" property="peopleIdentityCardId" />
<association property="identityCard" column="people_identity_card_id" select="com.example.demo.mapper.IdentityCardMapper.selectIdentityCardById"/>
</resultMap>
<select id="peopleSelectAll" resultMap="BaseResultMapWithIdentityCard">
select * from people
</select>
2.1 在PeopleMapper.xml中调用peopleSelectAll方法执行sql查询语句,将从people表中查询出的信息映射到BaseResultMapWithIdentityCard中。
2.2 在id为BaseResultMapWithIdentityCard的映射中需要额外添加association标签。
2.3 来看一下association标签中各信息的含义:
1)column=“people_identity_card_id”:代表将people表中people_identity_card_id字段作为出参传递到下一条要执行的sql语句中。
2)select=“com.example.demo.mapper.IdentityCardMapper.selectIdentityCardById”:代表下一条要执行的sql语句所在的位置。
3)property=“identityCard”:代表将下一条执行的sql语句中查询出的信息映射到类People中的identityCard对象中。
3、IdentityCardMapper.xml中的代码如下
<select id="selectIdentityCardById" resultType="com.example.demo.entity.IdentityCard">
select * from identity_card where identity_card_id = #{id}
</select>
此查询语句为association标签中要执行的下一条sql语句,people表中的people_identity_card_id字段作为入参,将查询的结果映射到IdentityCard类中。
4、people的controller层如何调用来看一下
@RequestMapping("peopleSelectAll")
@ResponseBody
public List<People> peopleSelectAll(){
return peopleService.peopleSelectAll();
}
5、查询结果如下图:
二、第二种情况:
多对一:一个班级可以有多个学生,一个学生只能有一个班级
查询信息:查询学生信息的时候返回班级信息
1、Student类中需要额外添加classInfo对象作为两个表的关联条件
@Data
public class Student {
private Long id;
private Long studentId;
private String studentName;
private Long studentClassInfoId;
private Integer studentSex;
private ClassInfo classInfo;//关联属性:对象
}
classinfo类中不需要添加额外字段
@Data
public class IdentityCard {
private Long id;
private String identityCardId;
private String identityCardName;
@JsonFormat(pattern="yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
private LocalDateTime identityCardBirthday;
private String identityCardAddress;
}
2、StudentMapper.xml中的代码如下,多对一和一对一的association的使用方法是一样滴,就不描述啦。
<resultMap id="BaseResultMap" type="com.example.demo.entity.Student">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="student_id" jdbcType="VARCHAR" property="studentId" />
<result column="student_name" jdbcType="INTEGER" property="studentName" />
<result column="student_class_info_id" jdbcType="BIGINT" property="studentClassInfoId" />
<association property="classInfo" column="student_class_info_id" select="com.example.demo.mapper.ClassInfoMapper.selectClassInfoById"/>
</resultMap>
<select id="studentSelectAll" resultMap="BaseResultMap">
select * from student
</select>
3、ClassInfoMapper.xml中的代码如下
<select id="selectClassInfoById" resultType="com.example.demo.entity.ClassInfo">
select * from class_info where class_info_id = #{id}
</select>
4、student的controller层如何调用来看一下
@RequestMapping("studentSelectAll")
@ResponseBody
public List<Student> studentSelectAll(){
return studentService.studentSelectAll();
}
5、查询结果如下图: