zoukankan      html  css  js  c++  java
  • MyBatis学习笔记(四)一多一关系

    Student中包含Address

    1. package com.skymr.mybatis.model;  
    2.   
    3. public class Student {  
    4.   
    5.     private int id;  
    6.       
    7.     private String name;  
    8.       
    9.     private int age;  
    10.       
    11.     private Address address;  
    12.       
    13.     /**  
    14.      * 必须要有无参构造器,有参构造器可有可无(至少我测试时是这样)  
    15.      * 如果没有无参构造器,只有有参构造器,会报错  
    16.      */  
    17.     public Student() {  
    18.     }  
    19.     public Student(String name, int age) {  
    20.         this.name = name;  
    21.         this.age = age;  
    22.     }  
    23.   
    24.     public int getId() {  
    25.         return id;  
    26.     }  
    27.   
    28.     public void setId(int id) {  
    29.         this.id = id;  
    30.     }  
    31.   
    32.     public String getName() {  
    33.         return name;  
    34.     }  
    35.   
    36.     public void setName(String name) {  
    37.         this.name = name;  
    38.     }  
    39.   
    40.     public int getAge() {  
    41.         return age;  
    42.     }  
    43.   
    44.     public void setAge(int age) {  
    45.         this.age = age;  
    46.     }  
    47.       
    48.     public Address getAddress() {  
    49.         return address;  
    50.     }  
    51.     public void setAddress(Address address) {  
    52.         this.address = address;  
    53.     }  
    54.     public String toString(){  
    55.         return "["+name+","+age+","+address+"]";  
    56.     }  
    57.       
    58. }  
    1. package com.skymr.mybatis.model;  
    2.   
    3. public class Address {  
    4.   
    5.     //省  
    6.     private String province;  
    7.     //市  
    8.     private String city;  
    9.     //区  
    10.     private String region;  
    11.       
    12.     private int id;  
    13.   
    14.     public String getProvince() {  
    15.         return province;  
    16.     }  
    17.   
    18.     public void setProvince(String province) {  
    19.         this.province = province;  
    20.     }  
    21.   
    22.     public String getCity() {  
    23.         return city;  
    24.     }  
    25.   
    26.     public void setCity(String city) {  
    27.         this.city = city;  
    28.     }  
    29.   
    30.     public String getRegion() {  
    31.         return region;  
    32.     }  
    33.   
    34.     public void setRegion(String region) {  
    35.         this.region = region;  
    36.     }  
    37.   
    38.     public int getId() {  
    39.         return id;  
    40.     }  
    41.   
    42.     public void setId(int id) {  
    43.         this.id = id;  
    44.     }  
    45.       
    46.     public String toString(){  
    47.         return "[" + id+","+this.province+","+this.city+","+this.region+"]";  
    48.     }  
    49. }  
    1. package com.skymr.mybatis.mappers;  
    2.   
    3. import java.util.List;  
    4.   
    5. import com.skymr.mybatis.model.Student;  
    6.   
    7. public interface StudentMapper {  
    8.   
    9.     public Student getStudent(int id);  
    10.       
    11.     public List<Student> getAllStudents();  
    12.     //取得所有学生,带地址  
    13.     public List<Student> getAllStudentsWithAddr();  
    14.     //取得学生,带地址  
    15.     public Student getStudentWithAddr(int id);  
    16. }  

    方式一:对象集联

    1. <!-- 一对一关系 -->  
    2. <select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">  
    3.     select * from mybatis_Student t1, mybatis_address t2 where t1.address_id=t2.id  
    4. </select>  
    5. <resultMap type="Student" id="stuMapWidhAddr">  
    6.     <id property="id" column="id"/>  
    7.     <result property="name" column="name"/>  
    8.     <result property="age" column="age"/>  
    9.     <result property="address.id" column="address_id"/>  
    10.     <result property="address.province" column="province"/>  
    11.     <result property="address.city" column="city"/>  
    12.     <result property="address.region" column="region"/>  
    13. </resultMap>  
    14.   
    15. <select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">  
    16.     select * from mybatis_Student t1 left join mybatis_address t2 on t1.address_id=t2.id and t1.id=#{id}  
    17. </select>  


    这种方式不太好,重用性差.

    方式二:

    1. <!-- 一对一关系 -->  
    2. <select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">  
    3.     select * from mybatis_Student t1, mybatis_address t2 where t1.address_id=t2.id  
    4. </select>  
    5. <resultMap type="Student" id="stuMapWidhAddr">  
    6.     <id property="id" column="id"/>  
    7.     <result property="name" column="name"/>  
    8.     <result property="age" column="age"/>  
    9.     <association property="address" resultMap="addressMap"></association>  
    10. </resultMap>  
    11. <!-- 将Address独立出来 -->  
    12. <resultMap type="Address" id="addressMap">  
    13.     <id property="id" column="id"/>  
    14.     <result property="province" column="province"/>  
    15.     <result property="city" column="city"/>  
    16.     <result property="region" column="region"/>  
    17. </resultMap>  
    18. <select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">  
    19.     select * from mybatis_Student t1 left join mybatis_address t2 on t1.address_id=t2.id and t1.id=#{id}  
    20. </select>  


    将Address独立出来,然后使用association关联到Address的resultMap

    方式三:

    1. <resultMap type="Student" id="stuMapWidhAddr">  
    2.     <id property="id" column="id"/>  
    3.     <result property="name" column="name"/>  
    4.     <result property="age" column="age"/>  
    5.     <association property="address" javaType="Address">  
    6.         <result property="id" column="id"/>  
    7.         <result property="province" column="province"/>  
    8.         <result property="city" column="city"/>  
    9.         <result property="region" column="region"/>  
    10.     </association>  
    11. </resultMap>  


    方式四:推荐的方式

    这种方式还是主要是应用association标签

    首先,为Address类加入AddressMapper类与AddressMapper.xml

    1. package com.skymr.mybatis.mappers;  
    2.   
    3. import com.skymr.mybatis.model.Address;  
    4.   
    5. public interface AddressMapper {  
    6.   
    7.     public Address getAddress(int id);  
    8. }  
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
    3. <mapper namespace="com.skymr.mybatis.mappers.AddressMapper">  
    4.     <select id="getAddress" resultType="Address" parameterType="int">  
    5.         select * from mybatis_address where id=#{id}  
    6.     </select>  
    7. </mapper>   


    其次,修改StudentMapper.xml

    1. <!-- 一对一关系 -->  
    2. <select id="getAllStudentsWithAddr" resultMap="stuMapWidhAddr">  
    3.     select * from mybatis_Student  
    4. </select>  
    5. <resultMap type="Student" id="stuMapWidhAddr">  
    6.     <id property="id" column="id"/>  
    7.     <result property="name" column="name"/>  
    8.     <result property="age" column="age"/>  
    9.     <association property="address" column="address_id" select="com.skymr.mybatis.mappers.AddressMapper.getAddress">  
    10.     </association>  
    11. </resultMap>  
    12. <select id="getStudentWithAddr" resultMap="stuMapWidhAddr" parameterType="int">  
    13.     select * from mybatis_Student where id=#{id}  
    14. </select>  


    使用了association标签,定义column与select属性

    column: 传入mybatis_Student表的外键address_id.

    select: 调用AddressMapper的getAddress方法

    推演过程:查询一条student数据,再根据address_id查询address表的一条数据.

    思考:不知道可不可以这样想,如果查询1000条student,再分别查address表的1000条数据,这样查询次数就是1001次了,不影响性能吗?


  • 相关阅读:
    程序员转型架构师,推荐你读这几本书
    Dubbo服务发现源码解析
    高可用架构之限流降级
    为什么Kafka速度那么快
    从分布式一致性到共识机制(三)拜占庭问题
    从分布式一致性到共识机制(二)Raft算法
    三分钟看完京东区块链白皮书
    轻松理解零知识证明
    三大去中心化交易协议对比
    从分布式一致性到共识机制(一)Paxos算法
  • 原文地址:https://www.cnblogs.com/bkyliufeng/p/6291764.html
Copyright © 2011-2022 走看看