• MyBatis级联操作


    先解决一下idea无法识别lombok构造方法的问题,解决方案是在idea的插件中下载并安装lombok插件。

    MyBatis级联操作,列举最简单的student-classes(学生与班级)的关系表:

    create table if not exists student (
        id int primary key auto_increment,
        name varchar(20) not null comment '学生姓名',
        cid int not null comment '班级id'
    );
    create table if not exists classes (
        id int primary key auto_increment,
        name varchar(20) not null comment '班级名'
    );

    接下来,创建关系表对应的实体类:

    @Data
    public class Student {
        private long id;
        private String name;
        private Classes classes;
    }
    @Data
    public class Classes {
        private long id;
        private String name;
        private List<Student> students;
    }

    在repository包下新建StudentRepository接口:

    public interface StudentRepository {
        public Student findById(long id);
    }

    然后创建对应的mapper文件StudentRepository.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="com.wts.repository.StudentRepository">
        
        <resultMap id="studentMap" type="com.wts.entity.Student">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
            <association property="classes" javaType="com.wts.entity.Classes">
                <id column="cid" property="id"></id>
                <result column="cname" property="name"></result>
            </association>
        </resultMap>
        
        <select id="findById" parameterType="long" resultMap="studentMap">
            select s.id,s.name,c.id as cid,c.name as cname from student s,classes c where s.id = #{id} and s.cid = c.id
        </select>
    </mapper>

    注意这里有几个限制:

    1.命名空间,xml文件的namespace必须是对应接口的全类名

    2.Statement标签的id必须与接口方法相同,其中parameterType为参数,resultType为返回类型,复杂类型用resultMap

    3.复杂类型resultMap中多对一用association,一堆多用集合collection

    MyBatis执行sql返回的结果集会和关系对象映射起来,注意列与字段的对应关系。

    然后将mapper引入:

    <mappers>
        <mapper resource="com/wts/repository/StudentRepository.xml"></mapper>
    </mappers>

    编写测试方法:

    @Test
    public void test03() {
        InputStream inputStream = AppTest.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = (new SqlSessionFactoryBuilder()).build(inputStream);
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            // 级联查询
            StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
            System.out.println(studentRepository.findById(1L));
        }
    }
  • 相关阅读:
    Design a stack that supports getMin() in O(1) time and O(1) extra space
    Python的sys.argv用法
    数据库系统概论学习4-SQL 语句和关系代数(二)单表查询
    数据库系统概论学习3-SQL 语句和关系代数(一)SQL 入门
    数据库系统概论学习2-《关系数据库与 E/R 模型》
    MySQL实验1: 新建一个名为 library 的数据库,包含 book、reader 两张表,根据自己的理解安排表的内容并插入数据。
    如何在Eclipse环境下安装PyDev并成功运行Python3.6代码
    模型融合之blending和stacking
    Pandas基础用法-数据处理【全】-转
    各种排序算法-用Python实现
  • 原文地址:https://www.cnblogs.com/viewts/p/13213352.html
Copyright © 2020-2023  润新知