码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis按结果嵌套处理实现多对一处理和一对多处理

时间:2020-07-08 23:11:05      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:apach   import   HERE   pac   实体类   oci   ati   tst   lis   

1.多对一

实体类

//这里用到了lombok
@Data
public class Student {
    private int id;
    private String name;

    //学生需要关联一个老师!

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

Mapper配置文件

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.StudentMapper">

        <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="student">
                <result property="id" column="sid"/>
                <result property="name" column="sname"/>
                <association property="teacher" javaType="teacher">
                        <result property="id" column="tid"/>
                        <result property="name" column="tname"/>
                </association>
        </resultMap>
</mapper>

测试

package com.you.dao;

import com.you.utils.MybatisUtils;
import com.you.vo.Student;
import com.you.vo.Teacher;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class TeacherMapperTest {

    @Test
    public void getStudent(){
        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();
    }
}

2.一对多

实体类

@Data
public class Student {

    private int id;
    private String name;
    private int tid;

}

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

    //一个老师拥有多个学生
    private List<Student> students;
}

Mapper配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.you.dao.TeacherMapper">

<!--    按结果嵌套查询-->
    <select id="getTeacherByid" 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 = #{tid}
    </select>

    <resultMap id="TeacherStudent" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>

        <!--复杂的属性,我们需要单独处理 对象:association 集合:collection
        JavaType=“” 指定属性的类型!
        集合中的泛型信息,我们使用ofType
        -->
        <collection property="students" ofType="student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>

</mapper>

测试

package com.you.dao;

import com.you.utils.MybatisUtils;
import com.you.vo.Teacher;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class getMapperTeat {
   
    @Test
    public void getTeacherByid(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacherByid = mapper.getTeacherByid(1);
        System.out.println(teacherByid);
        sqlSession.close();
    }
}

Mybatis按结果嵌套处理实现多对一处理和一对多处理

标签:apach   import   HERE   pac   实体类   oci   ati   tst   lis   

原文地址:https://www.cnblogs.com/Ydharma/p/13269599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!