mybatis的assosiation 和 collection 查询

分别有2种查询方式
一种是查询嵌套,实体类中的复杂属性另写一sql进行查询封装
一种是结果嵌套,先进行sql语句通过连接的方式查询,得到许多列,将查询结果中需要封装成复杂属性的列交给resultMap中的
collection或者association来封装

assosiation用法

student表:id,name,tid
teacher表:id,name

student类:id name teacher
Teacher类 id name List students
许多学生都关联一个老师,多对一的关系

查询嵌套

<!--
    查询嵌套
    1、查询所有的学生信息,传给对应的resultMap,resultmap根据column拿值/传值给子查询,javaType是返回类型,
    2、根据查询出来的学生的id的tid,寻找对应的老师! -子查询
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>

<resultMap id="StudentTeacher" type="com.rui.pojo.Student">
    <!--复杂的属性,我们需要单独处理  对象:association  集合:collection-->
    <result property="id" column="id">
    <result property="name" column="name">
    <association property="teacher" column="tid" javaType="com.rui.pojo.Teacher" select="getTeacher"/>
</resultMap>

<select id="getTeacher" resultType="com.rui.pojo.Teacher">
    select * from teacher where id = #{id}
</select>

结果嵌套

<!--按照结果嵌套处理 第一个select查询出 sid,sname,tname,tid 通过column传给resultmap,resultmap这里相当于
把拿到的tid,tname 封装进teacher对象 然后返回-->
<select id="getStudent2" resultMap="StudentTeacher2">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid=t.id;
</select>

<resultMap id="StudentTeacher2" type="com.rui.pojo.Student">
    <result property="id" column="sid"/>  <!--如果不写会以初始值封装进Student,如int类型为0-->
    <result property="name" column="sname"/>
    <association property="teacher" javaType="com.rui.pojo.Teacher">
        <result property="id" column="tid"></result>
        <result property="name" column="tname"></result>
    </association>

</resultMap>

collection 一对多,查询具有多个学生的老师

下面是实体类

@Data
public class Teacher {
    private int id;
    private String name;

    //一个老师拥有多个学生
    private List<Student> students;
}
@Data
public class Student {
    private int id;
    private String name;
    private int tid;

}

查询嵌套

resultMap中property是实体类中的属性名
column是传入的属性名

<select id="getTeacher" resultMap="TeacherStudent">
    select * from teacher where id=#{abcid}
</select>

<resultMap id="TeacherStudent" type="pojo.Teacher">
    <result property="id" column="id"></result>
    <result property="name" column="name"><result>
    <collection property="students" javaType="ArrayList" ofType="pojo.Student" select="getStudentByTeacherId" column="id"></collection>
</resultMap>

<select id="getStudentByTeacherId" resultType="com.rui.pojo.Student">
    select * from mybatis.student where tid = #{tid}
</select>
<!-- 第一个seleclt接收从dao传来的参数 tid 的所有学生.
最后一个select 接收resultmap传的column="id"
-->

结果嵌套

<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid=t.id and t.id=#{id}
</select>

<resultMap id="TeacherStudent" type="pojo.Teacher">
    <result property="id" column="tid"/> <!--如果不写会以初始值封装进Student,如int类型为0-->
    <result property="name" column="tname"/>
    <collection property="students"  ofType="pojo.Student">
         <result property="id" column="sid"></result>
         <result property="name" column="sname"></result>
         <result property="tid" column="tid"></result>
    </collection>
</resultMap>


mybatis dao 接口的注解使用

mapper接口中有多个参数(每个对象或参数都要加@Param注解), 或一个 String, 必须使用@Param注解,
如果不使用@param的情况,在xml的中的sql语句 ...where id=#{abc}也不需要特定参数名。

 posted on 2020-03-19 15:32  whiplasher  阅读(623)  评论(0编辑  收藏  举报