MyBatis学习笔记一多一关系
Posted 劳资的睡姿决定发型
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis学习笔记一多一关系相关的知识,希望对你有一定的参考价值。
Student中包含Address
- package com.skymr.mybatis.model;
- public class Student {
- private int id;
- private String name;
- private int age;
- private Address address;
- /**
- * 必须要有无参构造器,有参构造器可有可无(至少我测试时是这样)
- * 如果没有无参构造器,只有有参构造器,会报错
- */
- public Student() {
- }
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Address getAddress() {
- return address;
- }
- public void setAddress(Address address) {
- this.address = address;
- }
- public String toString(){
- return "["+name+","+age+","+address+"]";
- }
- }
- package com.skymr.mybatis.model;
- public class Address {
- //省
- private String province;
- //市
- private String city;
- //区
- private String region;
- private int id;
- public String getProvince() {
- return province;
- }
- public void setProvince(String province) {
- this.province = province;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getRegion() {
- return region;
- }
- public void setRegion(String region) {
- this.region = region;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String toString(){
- return "[" + id+","+this.province+","+this.city+","+this.region+"]";
- }
- }
- package com.skymr.mybatis.mappers;
- import java.util.List;
- import com.skymr.mybatis.model.Student;
- public interface StudentMapper {
- public Student getStudent(int id);
- public List<Student> getAllStudents();
- //取得所有学生,带地址
- public List<Student> getAllStudentsWithAddr();
- //取得学生,带地址
- public Student getStudentWithAddr(int id);
- }
方式一:对象集联
- <!-- 一对一关系 -->
- <select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">
- select * from mybatis_Student t1, mybatis_address t2 where t1.address_id=t2.id
- </select>
- <resultMap type="Student" id="stuMapWidhAddr">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="age" column="age"/>
- <result property="address.id" column="address_id"/>
- <result property="address.province" column="province"/>
- <result property="address.city" column="city"/>
- <result property="address.region" column="region"/>
- </resultMap>
- <select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">
- select * from mybatis_Student t1 left join mybatis_address t2 on t1.address_id=t2.id and t1.id=#{id}
- </select>
这种方式不太好,重用性差.
方式二:
- <!-- 一对一关系 -->
- <select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">
- select * from mybatis_Student t1, mybatis_address t2 where t1.address_id=t2.id
- </select>
- <resultMap type="Student" id="stuMapWidhAddr">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="age" column="age"/>
- <association property="address" resultMap="addressMap"></association>
- </resultMap>
- <!-- 将Address独立出来 -->
- <resultMap type="Address" id="addressMap">
- <id property="id" column="id"/>
- <result property="province" column="province"/>
- <result property="city" column="city"/>
- <result property="region" column="region"/>
- </resultMap>
- <select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">
- select * from mybatis_Student t1 left join mybatis_address t2 on t1.address_id=t2.id and t1.id=#{id}
- </select>
将Address独立出来,然后使用association关联到Address的resultMap
方式三:
- <resultMap type="Student" id="stuMapWidhAddr">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="age" column="age"/>
- <association property="address" javaType="Address">
- <result property="id" column="id"/>
- <result property="province" column="province"/>
- <result property="city" column="city"/>
- <result property="region" column="region"/>
- </association>
- </resultMap>
方式四:推荐的方式
这种方式还是主要是应用association标签
首先,为Address类加入AddressMapper类与AddressMapper.xml
- package com.skymr.mybatis.mappers;
- import com.skymr.mybatis.model.Address;
- public interface AddressMapper {
- public Address getAddress(int id);
- }
- <?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.skymr.mybatis.mappers.AddressMapper">
- <select id="getAddress" resultType="Address" parameterType="int">
- select * from mybatis_address where id=#{id}
- </select>
- </mapper>
其次,修改StudentMapper.xml
- <!-- 一对一关系 -->
- <select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">
- select * from mybatis_Student
- </select>
- <resultMap type="Student" id="stuMapWidhAddr">
- <id property="id" column="id"/>
- <result property="name" column="name"/>
- <result property="age" column="age"/>
- <association property="address" column="address_id" select="com.skymr.mybatis.mappers.AddressMapper.getAddress">
- </association>
- </resultMap>
- <select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">
- select * from mybatis_Student where id=#{id}
- </select>
使用了association标签,定义column与select属性
column: 传入mybatis_Student表的外键address_id.
select: 调用AddressMapper的getAddress方法
推演过程:查询一条student数据,再根据address_id查询address表的一条数据.
思考:不知道可不可以这样想,如果查询1000条student,再分别查address表的1000条数据,这样查询次数就是1001次了,不影响性能吗?
以上是关于MyBatis学习笔记一多一关系的主要内容,如果未能解决你的问题,请参考以下文章
mybatis学习笔记(14)-spring和mybatis整合