首页 > 其他 > 详细

一对多,多对一

时间:2020-05-28 23:51:46      阅读:73      评论:0      收藏:0      [点我收藏+]

多对一

实体类

//Teacher的pojo类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
    private int id;
    private String name;
}

//Student的pojo类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private Teacher teacher;
}

配置文件

<typeAliases>
    <typeAlias alias="Teacher" type="com.chao.pojo.Teacher"/>
    <typeAlias alias="Student" type="com.chao.pojo.Student"/>
</typeAliases>

<mappers>
    <mapper resource="StudentMapper.xml"/>
    <mapper resource="TeacherMapper.xml"/>
</mappers>

Mapper.xml

按照查询嵌套处理
<mapper namespace="com.chao.mapper.TeacherMapper">
    <select id="getTeacher" resultType="Teacher">
        select * from ts.teacher
    </select>
</mapper>

<mapper namespace="com.chao.mapper.StudentMapper">
    <select id="getStudent" resultMap="StudentTeacher">
        select * from ts.student ;
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from ts.teacher where id = #{id}
    </select>
</mapper>

按照结果嵌套处理
<mapper namespace="com.chao.mapper.StudentMapper">
    <select id="getStudent" resultMap="StudentTeacher">
        select s.id sid, s.name sname, t.name tname ,t.id teid
        from ts.student s,ts.teacher t
        where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
            <result property="id" column="teid"/>
        </association>
    </resultMap>
</mapper>

测试

@Test
public void getTeacher(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    List<Student> studentList = mapper.getStudent();
    for (Student student : studentList){
        System.out.println(student);
    }
    sqlSession.close();
}

一对多

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private int tid;
}

配置文件

<typeAliases>
    <typeAlias alias="Teacher" type="com.chao.pojo.Teacher"/>
    <typeAlias alias="Student" type="com.chao.pojo.Student"/>
</typeAliases>

<mappers>
    <mapper resource="StudentMapper.xml"/>
    <mapper resource="TeacherMapper.xml"/>
</mappers>

Mapper.xml

<mapper namespace="com.chao.mapper.TeacherMapper">
    <select id="getTeacher" resultMap="TeacherS">
        select t.id tid, t.name tname,s.name sname, s.id sid
        from ts.teacher t,ts.student s
        where t.id = s.tid and t.id = #{tid}
    </select>
    <resultMap id="TeacherS" type="Teacher">
        <result property="name" column="tname"/>
        <result property="id" column="tid"/>
        <collection property="students" ofType="Student">
            <result property="name" column="sname"/>
            <result property="id" column="sid"/>
        </collection>
    </resultMap>
</mapper>

测试

@Test
public void getTeacher(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
    Teacher teacher = mapper.getTeacher(1);
    System.out.println(teacher);
    sqlSession.close();
}

一对多,多对一

原文:https://www.cnblogs.com/chaostudy/p/12984691.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!