mybatis一對一關聯關系映射
在關聯關系中,有一對一,一對多,多對多三種關聯關系。
一對一關系:在操作上,任意一方引入對方的主鍵作為外鍵。
一對多關系:在“多”的一方添加“一”的一方的主鍵作為外鍵。
多對多關系:產生中間表引入兩張表的主鍵作為外鍵,將兩個主鍵作為聯合主鍵或者引入新的字段作為這個中間表的主鍵。
一對一關聯關系
例如person和IDcard,一個人只有一個身份證號,而一個身份證號只對應一個人。
以上是person表和IDcard表。
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"/><!--表示需要查詢出來的結果字段,為tb_idcard中的字段-->
</association>
</resultMap>
<sql id="Base_Column_List">
id, name, age, sex, card_id
</sql>
<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>
一對一運行結果截圖
一對一關系總結:
一對一關系是比較簡單的,通過在實體類中聲明另一個實體類的對象屬性,這樣就可以把他們關聯起來,在寫mapper文件時,關聯關系都應該采用嵌套結果查詢的方式進行關聯查詢,因為這樣比較方便而且快速,不用去建另一個的實體類的mapper接口和mapper映射文件。一對一關系映射使用<association>
元素。