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>
元素。