版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/83586568
关联映射主要分为 一对一、一对多、多对多,下面学习一对一
在学习resultMap元素时,包含了一个association子元素,这个元素就是用来处理一对一关联关系的
下面通过一个案例来学习一下
1.建立数据表
create table tb_idcard(
id int primary key auto_increment,
code varchar(18)
);
insert into tb_idcard(code)
values('152221198711020624'),('152221198804058713');
create table tb_person(
id int primary key auto_increment,
name varchar(32),
age int,
sex varchar(8),
card_id int unique,
foreign key(card_id) references tb_idcard(id)
);
insert into tb_person(name,age,sex,card_id)
values('rose',29,'女',1);
insert into tb_person(name,age,sex,card_id)
values('tom',25,'男',2);
2.创建项目chapter09,并将所需jar包以及MybatisUtils工具类以及核心配置文件mybatis—config.xml赋值粘贴过来
package com.itheima.po;
public class IdCard {
private Integer id;
private String code;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "IdCard [id="+id+",code="+code+"]";
}
}
package com.itheima.po;
public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard card;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public IdCard getCard() {
return card;
}
public void setCard(IdCard card) {
this.card = card;
}
@Override
public String toString() {
return "Customer [id="+id+",name="+name+",age="+age+",sex="+sex+",Card="+card+"]";
}
}
3.在com.itheima.mapper包中,创建证件映射文件IdCardMapper.xml和PersonMapper.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.itheima.mapper.IdCardMapper">
<select id="findCodeById" parameterType="Integer" resultType="IdCard">
select * from tb_idcard where id=#{id};
</select>
</mapper>
<?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.itheima.mapper.PersonMapper">
<select id="findPersonById" parameterType="Integer" resultMap="IdCardWithPersonResult">
select * from tb_person where id=#{id}
</select>
<resultMap type="person" id="IdCardWithPersonResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<association property="card" column="card_id" javaType="IdCard"
select="com.itheima.mapper.IdCardMapper.findCodeById"></association>
</resultMap>
</mapper>
4.在核心配置文件mybatis-config.xml文件中,引入Mapper映射文件并定义别名,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<package name="com.itheima.po"/>
</typeAliases>
<!-- 1.配置环境,默认的环境id为mysql -->
<environments default="mysql">
<!-- 1.2.配置id为mysql的数据库环境 -->
<environment id="mysql">
<!-- 使用数据库事务管理 -->
<transactionManager type="JDBC"/>
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="itcst"/>
</dataSource>
</environment>
</environments>
<!-- 2.配置Mapper的位置 -->
<mappers>
<mapper resource="com/itheima/mapper/IdCardMapper.xml"/>
<mapper resource="com/itheima/mapper/PersonMapper.xml"/>
</mappers>
</configuration>
5.创建测试类MyBatisssociatedTest,并在类中编写测试方法findPersonByIdTest()
package com.itheima.test;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.itheima.po.Person;
import com.itheima.utils.MybatisUtils;
public class MybatisAssociatedTest {
@Test
public void findPersonByIdTest() {
SqlSession session = MybatisUtils.getSession();
Person person = session.selectOne("com.itheima.mapper.PersonMapper.findPersonById",1);
System.out.println(person);
//关闭SqlSession
session.close();
}
}
6.查看测试结果
显示完全成功了,并且成功的打印了两句查询语句
7.嵌套结果方式
以上的六步都是在演示使用嵌套查询的一对一关联方法,但是在复杂的查询条件下,嵌套查询可能会很复杂甚至很缓慢,这个时候就需要用到嵌套结果方式查询。
其步骤与上面大致相同,不同之处在于PersonMapper.xml中的有关代码,修改之后的代码如下:
<select id="findPersonById2" parameterType="Integer" resultMap="IdCardWithPersonResult2">
select p.*,idcard.code
from tb_person p,tb_idcard idcard
where p.card_id=idcard.id
and idcard.id=#{id}
</select>
<resultMap type="person" id="IdCardWithPersonResult2">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<association property="card" javaType="IdCard">
<id property="id" column="card_id"/>
<result property="code" column="code"/>
</association>
</resultMap>
并创建测试方法
@Test
public void findPersonByIdTest2() {
SqlSession session = MybatisUtils.getSession();
Person person = session.selectOne("com.itheima.mapper.PersonMapper.findPersonById2",1);
System.out.println(person);
//关闭SqlSession
session.close();
}
测试结果如下:
扫描二维码关注公众号,回复:
3846744 查看本文章
可以看到只打印了一条查询语句,但是查询结果和嵌套查询的查询结果是一致的