Mybatis-注解多表操作

发布于:2023-01-20 ⋅ 阅读:(6) ⋅ 点赞:(0) ⋅ 评论:(0)
  • 目录

    一对一的环境准备

    一对一的功能实现

    实例演示

    一对多的环境准备

    一对多的功能实现

    实例演示

    多对多的环境准备

    多对多的功能实现

    实例演示


  • 一对一的环境准备

  • (数据表参考之前映射配置版多表操作)
  • public class Person {
        private Integer id; //主键id
        private String name;//人的姓名
        private Integer age;//人的年龄
    
        public Person() {
        }
    
        public Person(Integer id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
  • public class Card {
        private Integer id; //主键id
        private String number; //身份证号
        private Person p;//所属人的对象
    
        public Card() {
        }
    
        public Card(Integer id, String number, Person p) {
            this.id = id;
            this.number = number;
            this.p = p;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getNumber() {
            return number;
        }
    
        public void setNumber(String number) {
            this.number = number;
        }
    
        public Person getP() {
            return p;
        }
    
        public void setP(Person p) {
            this.p = p;
        }
    
        @Override
        public String toString() {
            return "Card{" +
                    "id=" + id +
                    ", number='" + number + '\'' +
                    ", p=" + p +
                    '}';
        }
    }
  • 一对一的功能实现

  • @Results:封装映射关系的父注解
  • 里面有Result[] value():定义了Result数组
  • @Result:封装映射关系的子注解
  • column属性:查询出的表中字段名称
  • property属性:实体对象中的属性名称
  • javaType属性:被包含对象的数据类型
  • one属性:一对一查询固定属性
  • @One:一对一查询的注解
  • select属性:指定调用某个接口中的方法
  • 实例演示

  • public interface CardMapper {
        //查询全部
        @Select("SELECT * FROM card")
        @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "number",property = "number"),
            @Result(
                    property = "p",
                    javaType = Person.class,
                    column = "pid",
    
                    one = @One(select = "demo6.one_to_one.PersonMapper.selectById")
            )
        })
        public abstract List<Card> selectAll();
    }
  • public interface PersonMapper {
        //根据id查询
        @Select("SELECT * FROM person WHERE id=#{id}")
        public abstract Person selectById(Integer id);
    }
  • <package name="demo6.one_to_one"/>
  • @Test
        public void selectAll() throws Exception{
            //1.加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            //4.获取CardMapper接口的实现类对象
            CardMapper mapper = sqlSession.getMapper(CardMapper.class);
            //5.调用实现类的方法,接收结果
            List<Card> list = mapper.selectAll();
            //6.处理结果
            for (Card card : list) {
                System.out.println(card);
            }
            //7.释放资源
            sqlSession.close();
            is.close();
        }
  • 一对多的环境准备

  • (数据表参考之前映射配置版多表操作)
  • public class Student {
        private Integer id; //主键id
        private String name; //学生姓名
        private Integer age; //学生年龄
    
        public Student() {
        }
    
        public Student(Integer id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
  • public class Classes {
        public Integer id; //主键id
        private String name; //班级名称
        private List<Student> students; //班级中所有学生对象
    
        public Classes() {
        }
    
        public Classes(Integer id, String name, List<Student> students) {
            this.id = id;
            this.name = name;
            this.students = students;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Student> getStudents() {
            return students;
        }
    
        public void setStudents(List<Student> students) {
            this.students = students;
        }
    
        @Override
        public String toString() {
            return "Classes{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", students=" + students +
                    '}';
        }
    }
  • <package name="demo6.one_to_many"/>
  • 一对多的功能实现

  • @Results:封装映射关系的父注解
  • 里面有Result[] value():定义了Result数组
  • @Result:封装映射关系的子注解
  • column属性:查询出的表中字段名称
  • property属性:实体对象中的属性名称
  • javaType属性:被包含对象的数据类型
  • many属性:一对多查询固定属性
  • @Many:一对多查询的注解
  • select属性:指定调用某个接口中的方法
  • 实例演示

  • public interface ClassesMapper {
        //查询全部
        @Select("SELECT * FROM classes")
        @Results({
                @Result(column = "id",property = "id"),
                @Result(column = "name",property = "name"),
                @Result(
                        property = "students",   //被包含对象的变量名
                        javaType = List.class, //被包含对象的实际数据类型
                        column = "id",         //根据查询出的classes表的id字段来查询student表
                        many = @Many(select = "demo6.one_to_many.StudentMapper.selectByCid")
                )
        })
        public abstract List<Classes> selectAll();
    }
  • public interface StudentMapper {
        //根据cid查询student表
        @Select("SELECT * FROM student WHERE cid=#{cid}")
        public abstract List<Student> selectByCid(Integer cid);
    }
  • public class Test02 {
        @Test
        public void selectAll() throws Exception {
            //1.加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            //4.获取ClassesMapper接口的实现类对象
            ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);
            //5.调用实现类的方法,接收结果
            List<Classes> list = mapper.selectAll();
            //6.处理结果
            for (Classes cls : list) {
                System.out.println(cls.getId() + "," + cls.getName());
                List<Student> students = cls.getStudents();
                for (Student student : students) {
                    System.out.println("\t" + student);
                }
            }
            //7.释放资源
            sqlSession.close();
            is.close();
        }
    }
  • 多对多的环境准备

  • (数据表参考之前映射配置版多表操作)
  • public class Course {
        private Integer id; //主键id
        private String name;//课程名称
    
        public Course() {
        }
    
        public Course(Integer id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Course{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
  • public class Student {
        private Integer id; //主键id
        private String name; //学生姓名
        private Integer age; //学生年龄
        private List<Course> courses; //学生所选择的课程对象
    
        public Student() {
        }
    
        public Student(Integer id, String name, Integer age, List<Course> courses) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.courses = courses;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public List<Course> getCourses() {
            return courses;
        }
    
        public void setCourses(List<Course> courses) {
            this.courses = courses;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", courses=" + courses +
                    '}';
        }
    }
  • <package name="demo6.many_to_many"/>
  • 多对多的功能实现

  • @Results:封装映射关系的父注解
  • 里面有Result[] value():定义了Result数组
  • @Result:封装映射关系的子注解
  • column属性:查询出的表中字段名称
  • property属性:实体对象中的属性名称
  • javaType属性:被包含对象的数据类型
  • many属性:一对多查询固定属性
  • @Many:一对多查询的注解
  • select属性:指定调用某个接口中的方法
  • 实例演示

  • public interface StudentMapper {
        //查询全部
        @Select("SELECT DISTINCT s.id,s.name,s.age FROM student s,stu_cr sc WHERE sc.sid=s.id")
        @Results({
                @Result(column = "id",property = "id"),
                @Result(column = "name",property = "name"),
                @Result(column = "age",property = "age"),
                @Result(
                        property = "courses",
                        javaType = List.class,
                        column = "id",
                        many = @Many(select = "demo6.many_to_many.CourseMapper.selectBySid")
                )
        })
        public abstract List<Student> selectAll();
    }
  • public interface CourseMapper {
        //根据学生id查询所选课程
        @Select("SELECT c.id,c.name FROM stu_cr sc,course c WHERE sc.cid=c.id AND sc.sid=#{id}")
        public abstract List<Course> selectBySid(Integer sid);
    }
  • public class Test03 {
        @Test
        public void selectAll() throws Exception {
            //1.加载核心配置文件
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            //4.获取StudentMapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.调用实现类的方法,接收结果
            List<Student> list = mapper.selectAll();
            //6.处理结果
            for (Student student : list) {
                System.out.println(student.getId() + "," + student.getName()+","+student.getAge());
                List<Course> courses = student.getCourses();
                for (Course cours : courses) {
                    System.out.println("\t" + cours);
                }
            }
            //7.释放资源
            sqlSession.close();
            is.close();
        }
    }