一对一关系:在操作上,任意一方引入对方的主键作为外键。
例如 person 和 IDcard,一个人只有一个身份证号,而一个身份证号只对应一个人。
Person 表
IDcard 表
//Person 持久化类
public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard card; //一对一关系映射
//setter/getter方法
}
//身份证持久化类
public class IdCard {
private Integer id;
private String code;
}
PersonMapper接口
public interface PersonMapper {
Person selectPersonById(int id);
}
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="cn.jason.bootmybatis.mapper.PersonMapper">
<resultMap id="BaseResultMap" type="cn.jason.bootmybatis.model.Person">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="sex" property="sex" jdbcType="VARCHAR"/>
<!--这是嵌套查询,这样不方便,开发效率比较低,需要写两处sql和mapper接口和mapper文件,比较多余,
现在我想只写一处sql就把需求搞定,也就是写多表查询。把多表查询语句写在一个mapper文件中就可以了。-->
<!-- <association property="card" column="card_id" javaType="IdCard"
select="cn.jason.bootmybatis.mapper.IdCardMapper.selectById">
</association>-->
<!--嵌套结果查询 一对一关系映射-->
<association property="card" javaType="IdCard">
<!--这里面的属性都是关联的那个实体的属性,都是可以在前台获取到的-->
<id property="id" column="card_id"/><!-- id 为主键列,也就是在 tb_idcard 表中的 id 对应 tb_person 表中的 card_id -->
<result property="code" column="code"/><!-- result表示需要查询出来的结果字段, column="code" 为tb_idcard中的字段-->
</association>
</resultMap>
<select id="selectPersonById" parameterType="Integer" resultMap="BaseResultMap">
select p.*,idcard.code
from tb_person p ,tb_idcard idcard
where p.card_id=idcard.id and p.id=#{id}
</select>
</mapper>
Person业务层接口
//service业务层接口
public interface FindPersonWithIdCard {
Person findByIdWithIdcard(int id);
}
业务层实现类
@Service
public class FindPersonWithIdCardImpl implements FindPersonWithIdCard {
@Autowired
private PersonMapper personMapper;
@Override
public Person findByIdWithIdcard(int id) {
return personMapper.selectPersonById(id);
}
}
控制器类
@Controller
public class OneToOne {
@Autowired
private FindPersonWithIdCard findPersonWithIdCard;
@RequestMapping("/findpersonid/{id}")//restful架构风格
public String find(@PathVariable int id, Model model) {
model.addAttribute("personinfo", findPersonWithIdCard.findByIdWithIdcard(id));
return "personinfo";
}
}
页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>person信息页面</title>
</head>
<body>
<table>
<thead>
<tr>
<th>personID</th>
<th>姓名</th>
<th>年龄</th>
<th>身份证号</th>
<th>idcardID</th>
</tr>
</thead>
<tr>
<td th:text="${personinfo.id}">personid</td>
<td th:text="${personinfo.name}">姓名</td>
<td th:text="${personinfo.age}">年龄</td>
<td th:text="${personinfo.card.code}">身份证号</td>
<td th:text="${personinfo.card.id}">idcardID</td>
</tr>
</table>
</body>
</html>