zoukankan      html  css  js  c++  java
  • mybatis一对一和一对多实例

    一对一:<association></association>

    一对多:<collection></collection>

    ClassMapper.xml:

    <resultMap type="com.taobaoo.school.domain.edu.XjClass" id="classMap">
    <id property="id" column="class_id"/>
    <result property="className" column="class_name"/>
    <association property="school" javaType="com.taobaoo.school.domain.edu.School">
    <id property="id" column="school_id"/>
    <result property="schoolName" column="school_name"/>
    </association>
    <association property="grade" javaType="com.taobaoo.school.domain.edu.Grade">
    <id property="id" column="grade_id"/>
    <result property="gradeName" column="grade_name"/>
    </association>
    <association property="classTea" javaType="com.taobaoo.school.domain.edu.Teacher">
    <id property="id" column="class_teacher_id"/>
    <result property="name" column="class_teacher_name"/>
    </association>
    <collection property="teaList" ofType="com.taobaoo.school.domain.edu.Teacher">
    <id property="id" column="sub_teacher_id"/>
    <result property="name" column="sub_teacher_name"/>
    <result property="subjectName" column="sub_name"/>
    </collection>
    </resultMap>

    <select id="queryClass" resultMap="classMap">
    select
    cls.id AS class_id,
    cls.class_name AS class_name,
    school.id AS school_id,
    school.school_name AS school_name,
    grade.id AS grade_id,
    grade.grade_name AS grade_name,
    teacher.id AS class_teacher_id,
    teacher.name AS class_teacher_name
    from class cls
    join grade on cls.grade_id=grade.id
    join school on cls.school_id=school.id
    join teacher on cls.teacher_id=teacher.id
    </select>

    Class.java:

    private Long id;

    private Grade grade; //1对1

    private Teacher teacher;  //1对1

    Private List<Teacher> teaList; //1对多

    //get/set省略...

  • 相关阅读:
    一点一点学习GIt
    17-ajax向后端提交POST请求
    16-djongo中间件学习
    ES6快速入门
    15 Django组件——中间件
    导入Scrapy 项目报错:no module named scrapy
    slf4j + logback 输出日志:mybatis sql语句
    java实现链表
    Log4j2.xml的日志设置
    java多线程之生产者消费者经典问题
  • 原文地址:https://www.cnblogs.com/chenweichu/p/5508901.html
Copyright © 2011-2022 走看看