• MyBatis的高级映射


    MyBatis的高级映射

    完成属性映射,首先需要确定各个表之间的关系,下面我们建立了两张表,学生表与班级表
    学生表
    在这里插入图片描述
    班级表
    在这里插入图片描述

    多对一映射

    多对一映射有三种方法。在多对一映射关系中需要确定主表,确定了主表即确定了主类。
    在学生类里面添加班级类属性

    //学生类
    package com.powernode.mybatis.pojo;
    
    /**
     * 学生类
     * @author 老杜
     * @version 1.0
     * @since 1.0
     */
    public class Student {
        private Integer sid;
        private String sname;
        private Clazz clazz;
    
        public Clazz getClazz() {
            return clazz;
        }
    
        public void setClazz(Clazz clazz) {
            this.clazz = clazz;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "sid=" + sid +
                    ", sname='" + sname + '\'' +
                    ", clazz=" + clazz +
                    '}';
        }
    
        public Student() {
        }
    
        public Student(Integer sid, String sname) {
            this.sid = sid;
            this.sname = sname;
        }
    
        public Integer getSid() {
            return sid;
        }
    
        public void setSid(Integer sid) {
            this.sid = sid;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    //班级类
    public class Clazz {
        private Integer cid;
        private String cname;
    
    • 1
    • 2
    • 3
    • 4
    级联属性映射

    StudentMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.powernode.mybatis.mapper.StudentMapper">
    
        <resultMap id="studentResultMap" type="Student">
            <id property="sid" column="sid"/>
            <result property="sname" column="sname"/>
            <result property="clazz.cid" column="cid"/>
            <result property="clazz.cname" column="cname"/>
        resultMap>
    
        <select id="selectBySid" resultMap="studentResultMap">
            select s.*, c.* from t_student s join t_clazz c on s.cid = c.cid where sid = #{sid}
        select>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    StudentMapper接口

    package com.cxy.mybatis.mapper;
    
    import com.cxy.mybatis.pojo.Student;
    
    import java.util.List;
    
    public interface StudentMapper {
        /**
         * 检查是否正确与数据库进行连接
         * @param sid
         * @return
         */
        Student selectBySid(Integer sid);
    
        /**
         * 多对一的级联属性映射
         * @param sid 传入的学生id
         * @return
         */
        Student selectBySidWith(int sid);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    编写测试方法

    package com.powernode.mybatis.test;
    
    import com.powernode.mybatis.mapper.StudentMapper;
    import com.powernode.mybatis.pojo.Student;
    import com.powernode.mybatis.utils.SqlSessionUtil;
    import org.junit.Test;
    
    public class StudentMapperTest {
        @Test
        public void testSelectBySid(){
            StudentMapper mapper = SqlSessionUtil.openSession().getMapper(StudentMapper.class);
            Student student = mapper.selectBySid(1);
            System.out.println(student);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    association映射

    这个和上面的级联映射大部分相同,不过在结果映射集中需要添加以下配置
    StudentMapper.xml

    <resultMap id="studentResultMap" type="Student">
      <id property="sid" column="sid"/>
      <result property="sname" column="sname"/>
      <association property="clazz" javaType="Clazz">
        <id property="cid" column="cid"/>
        <result property="cname" column="cname"/>
      association>
    resultMap>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    分步映射

    分步映射是用的的最多的,这样有利于代码的复用以及进行sql语句的延迟加载
    分步映射其他地方不需要修改,只需要修改下面三个地方

    1. association中select位置填写sqlId,**这个sql id是指在ClazzMapper接口中的查询方法的权限定名称,sqlid=namespace+id,**其中column属性作为这条子sql语句的条件
    <resultMap id="studentResultMap" type="Student">
      <id property="sid" column="sid"/>
      <result property="sname" column="sname"/>
      <association property="clazz"
                   select="com.powernode.mybatis.mapper.ClazzMapper.selectByCid"
                   column="cid"/>
    resultMap>
    
    <select id="selectBySid" resultMap="studentResultMap">
      select s.* from t_student s where sid = #{sid}
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 在ClazzMapper接口中添加方法
     /**
         * 多对一分步查询2
         * @param cid
         * @return
         */
        Clazz selectByCidStep2(Integer cid);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 在ClazzMapper.xml文件中进行配置
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.powernode.mybatis.mapper.ClazzMapper">
        <select id="selectByCid" resultType="Clazz">
            select * from t_clazz where cid = #{cid}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    多对一延迟加载

    可以再association中添加fetchType=lazy;表示开启延迟加载;fetchType=eager表示关闭延迟加载

    一般情况下,在项目开发中我们会进行全局上的设置,在全局上设置开启延迟加载,具体代码如下

    <settings>
      <setting name="lazyLoadingEnabled" value="true"/>
    settings>
    
    • 1
    • 2
    • 3

    如果个别功能需求需要关闭延迟加载的话,可以设置fetch=eager;

    一对多映射

    一对多的实现,通常是在一的一方中有List集合属性。(一的一方是主表
    在Clazz类中添加List stus; 属性。

    public class Clazz {
        private Integer cid;
        private String cname;
        private List<Student> stus;
        // set get方法
        // 构造方法
        // toString方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    一对多的实现有两种方式:

    1. collection

    2. 分步映射查询

    第一种方式collection

    /**
         * 一对多collection查询
         * @param cid
         * @return 返回的是Clazz对象,里面有List stus 对象
         */
        Clazz selectByCid(Integer cid);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ClazzMapper.xml文件

      <resultMap id="selectByCidMap" type="Clazz">
            <id property="cid" column="cid">id>
            <result property="cname" column="cname">result>
            <collection property="stus" ofType="Student">
                
                <id property="sid" column="sid">id>
                <result property="sname" column="sname">result>
            collection>
        resultMap>
        <select id="selectByCid" resultMap="selectByCidMap">
            select * from t_class c join t_stu s on c.cid=s.cid where c.cid=#{cid}
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    第二种方式:分步查询

    <resultMap id="selectByCidStep1Map" type="Clazz">
            <id property="cid" column="cid">id>
            <result property="cname" column="cname">result>
            
            <collection property="stus"
                        select="com.cxy.mybatis.mapper.StudentMapper.selectByCidStep2"
                        column="cid"
                        fetchType="eager"
            >
            collection>
        resultMap>
        
        <select id="selectByCidStep1" resultMap="selectByCidStep1Map">
           select * from t_class c where c.cid=#{cid}
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    和多对一一样,在一对多种需要在StudentMapper接口中定义方法,

     /**
         * 一对多的分步查询,接收的是传过来的cid
         * @param cid
         * @return
         */
       List<Student> selectByCidStep2(Integer cid);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    <resultMap id="selectByCidStep2Map" type="Student">
            <id property="sid" column="sid">id>
            <result property="sname" column="sname"/>
        resultMap>
        <select id="selectByCidStep2" resultMap="selectByCidStep2Map">
            select * from t_stu where cid=#{cid}
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    多对多映射与一对一映射

    多对多映射需要进行多表联查,往往需要借助一个中间表来实现表与表之间的联系。
    多对多映射:可以分解成两个多对一映射
    一对一映射:可以分解成两个一对多映射

  • 相关阅读:
    python 就是随便玩玩,生成gif图,生成汉字图片,超级简单
    Containerd 如何配置 Proxy?
    智慧化工园区管理平台综合解决方案
    keil5显示内存和存储占用百分比进度条工具
    【gzoj1081】k上升段【DP】
    HTML静态网页成品作业(HTML+CSS)——非遗昆曲介绍设计制作(1个页面)
    Day46 MySQL查询关键字
    【深度学习实验】线性模型(一):使用NumPy实现简单线性模型:搭建、构造损失函数、计算损失值
    微信收款助手消息不弹窗的解决办法
    KingbaseES V8R3集群维护案例之---pcp_node_refresh应用
  • 原文地址:https://blog.csdn.net/CXYCMY/article/details/127642043