springboot mybaties 一对一 一对多

一、注解版

1、 一对一

  • 实体类
@Data
public class Student {
    private Long studentId;
    private String studentName;
    private Long age;
    private Long deptId;
    private Dept dept;
    private List<Car> cars;
    private List<Course> courses;
}
@Data
public class Dept {
    private Long deptId;
    private String deptName;
}
  • Mapper
@Mapper
public interface DeptMapper {

    @Select("Select * from dept where dept_id= #{id}")
    @Results({
            @Result(property = "deptId" ,column = "dept_id"),
            @Result(property = "deptName" ,column = "dept_name")
    }

    )
    public Dept getDeptByID(int id);

}

@Select("select * from student where student_id=#{id}")
    @Results({
            @Result(
                    property = "studentName", column = "student_name"
            ),
            @Result(
                    property = "deptId", column = "dept_id"
            ),
            @Result(
                    property = "dept", column = "dept_id", one = @One(select = "com.yanglk.mybaties.test_mb.mapper.DeptMapper.getDeptByID")
            )
    })
    Student getStudent(int id);

2、一对多

  • 实体类
@Data
public class Car {
    private Long carId;
    private String carName;
}
  • mapper
public interface CarMapper {

    @Select("Select * from car where student_id=#{id}")
    @Results({
            @Result(property = "carId",column = "car_id"),
            @Result(property = "carName",column = "car_name")
    })
    public List<Car> findByStuId();

}
@Select("select * from student where student_id=#{id}")
    @Results({
            @Result(
                    property = "studentId", column = "student_id"
            ),
            @Result(
                    property = "studentName", column = "student_name"
            ),
            @Result(
                    property = "deptId", column = "dept_id"
            ),
            @Result(
                    property = "cars", column = "student_id", many = @Many(select = "com.yanglk.mybaties.test_mb.mapper.CarMapper.findByStuId")
            ),
            @Result(
                    property = "dept", column = "dept_id", one = @One(select = "com.yanglk.mybaties.test_mb.mapper.DeptMapper.getDeptByID")
            )
    })
    Student getStudentCar(int id);

3、 注解说明

  • results 结果集 可以包含多个result
  • result 查询结果列
    -property 实体类变量名
    -column 数据表列名 在有关联时 表示 参照表中的关联项 可以理解为子查询的 查询条件
  • one 一对一 select 表示子查询
  • many 一对多

二 、XML 版

1 填坑

  • 使用idea, xml文件要放在resources 文件夹下,同时xxxMapper.xml文件要与对用的xxxMapper保持相同路径,但是Java文件下创建的是包(package)可以用(".")来分隔,在recourse下创建的是目录(dircetory),这时如果用(".")来分隔其实只是创建的一个目录,目录名中有("."),并不是一个层级目录。因从要使用("\")创建。
  • 在yaml中添加
mybatis:
 mapper-locations:
   - classpath:mapper/*.xml
   - classpath:com/**/mapper/*.xml

2 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.yanglk.mybaties.test_mb.mapper.StudentMapper">

    <resultMap id="studentMap" type="com.yanglk.mybaties.test_mb.model.Student">
        <id  property="studentId" column="student_id"></id>
        <result property="studentName" column="student_name"></result>
        <result property="deptId" column="dept_id"></result>
        <result property="age" column="age"></result>
        <association property="dept" javaType="com.yanglk.mybaties.test_mb.model.Dept" column="dept_id" >
            <id property="deptId" column="dept_id"></id>
            <result property="deptName" column="dname" />
        </association>
        <collection property="cars" ofType="com.yanglk.mybaties.test_mb.model.Car" column="student_id">
            <id property="carId" column="cid"></id>
            <result property="carName" column="cname"></result>
        </collection>
        <collection property="courses" ofType="com.yanglk.mybaties.test_mb.model.SC" column="student_id">
            <id  property="studentId" column="student_id"></id>
            <id property="courseId" column="sccid"></id>
            <result property="score" column="score"></result>
            <collection property="courses" ofType="com.yanglk.mybaties.test_mb.model.Course" column="sccid">
                <id property="courseId" column="coid"></id>
                <result property="courseName" column="coname"></result>
                <result property="period" column="period"></result>
                <result property="credit" column="credit"></result>
            </collection>
        </collection>
        <!--  mapper namespace 对应的java类的全路径-->
        <!--  property java类中变量名 -->
        <!--  column 数据库表中列名 在只用单表时,可以使用列名,当多表关联时,要对参照表列重命名,使用重命名的列名 如 <result property="deptName" column="dname" />-->
        <!--  association  collection 中的column 表示 参照表中的关联项  -->
        <!--  association  一对一  javaType是 对应的java类-->
        <!--  collection  一对多  ofType是 对应的java类-->
    </resultMap>

    <select id="selectWithDept" resultMap="studentMap" parameterType="int">
        select s.*, d.dept_name as dname, c.car_id as cid, c.car_name as cname ,
               sc.course_id as sccid, sc.score as score ,co.course_id as coid,
               co.course_name as coname,co.credit as credit,co.period as period
        from student s,dept d ,car c, sc,course as co
        where s.dept_id=d.dept_id and s.student_id=c.student_id and sc.student_id=s.student_id and sc.course_id=co.course_id and s.student_id=#{id}
    </select>
</mapper>

xml 太麻烦了,如果有关联查询,如果不是很复杂,直接写注解吧,
xml可以写一些查询判断,暂时还没有用到。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容