首页 > 代码库 > MyBatis学习笔记(四)一多一关系
MyBatis学习笔记(四)一多一关系
Student中包含Address
[html] view plain copy
- 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+"]";
- }
- }
[html] view plain copy
- 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+"]";
- }
- }
[html] view plain copy
- 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);
- }
方式一:对象集联
[html] view plain copy
- <!-- 一对一关系 -->
- <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>
这种方式不太好,重用性差.
方式二:
[html] view plain copy
- <!-- 一对一关系 -->
- <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
方式三:
[html] view plain copy
- <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
[html] view plain copy
- package com.skymr.mybatis.mappers;
- import com.skymr.mybatis.model.Address;
- public interface AddressMapper {
- public Address getAddress(int id);
- }
[html] view plain copy
- <?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
[html] view plain copy
- <!-- 一对一关系 -->
- <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学习笔记(四)一多一关系
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。