MyBatis注解开发的多表操作

1.一对一

环境准备

数据表为之前多表操作db1的数据表

bean.Card:

package Mybatis5.bean;

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 +
                '}';
    }
}

bean.Person:

package Mybatis5.bean;

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 +
                '}';
    }
}

PersonMapper接口:

package Mybatis5.one_to_one;

import Mybatis5.bean.Person;
import org.apache.ibatis.annotations.Select;

public interface PersonMapper {
    //根据id查询
    @Select("SELECT * FROM person WHERE id=#{id}")
    public abstract Person selectById(Integer id);
}

CardMapper接口:

package Mybatis5.one_to_one;

import Mybatis5.bean.Card;

import java.util.List;

public interface CardMapper {
    //查询全部
    public abstract List selectAll();
}

功能实现 

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

使用注解配置CardMapper:

package Mybatis5.one_to_one;

import Mybatis5.bean.Card;
import Mybatis5.bean.Person;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

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",             // 根据查询出的card表中的pid字段来查询person表
                    /*
                        one、@One 一对一固定写法
                        select属性:指定调用哪个接口中的哪个方法
                     */
                    one = @One(select = "Mybatis5.one_to_one.PersonMapper.selectById")
            )
    })
    public abstract List selectAll();
}

MybatisConfig.xml:

测试类:

package Mybatis5.one_to_one;

import Mybatis5.bean.Card;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        CardMapper mapper = sqlSession.getMapper(CardMapper.class);

        List list = mapper.selectAll();
        for (Card card : list) {
            System.out.println(card);
        }

        sqlSession.close();
        is.close();
    }
}

2.一对多

环境准备 

bean.Student:

package Mybatis5.bean;

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 +
                '}';
    }
}

bean.classes:

package Mybatis5.bean;

import java.util.List;

public class Classes {
    public Integer id; //主键id
    private String name; //班级名称
    private List students; //班级中所有学生对象

    public Classes() {
    }

    public Classes(Integer id, String name, List 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 getStudents() {
        return students;
    }

    public void setStudents(List students) {
        this.students = students;
    }

    @Override
    public String toString() {
        return "Classes{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", students=" + students +
                '}';
    }
}

功能实现

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

StudentMapper接口:

package Mybatis5.one_to_many;

import Mybatis5.bean.Student;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {
    //根据cid查询student表
    @Select("SELECT * FROM student WHERE cid=#{cid}")
    public abstract List selectByCid(Integer cid);
}

使用注解配置ClassesMapper:

package Mybatis5.one_to_many;

import Mybatis5.bean.Classes;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

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属性:指定调用哪个接口中的哪个查询方法
                     */
                    many = @Many(select = "Mybatis5.one_to_many.StudentMapper.selectByCid")
            )
    })
    public abstract List selectAll();
}

测试类:

package Mybatis5.one_to_many;

import Mybatis5.bean.Classes;
import Mybatis5.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);

        List list = mapper.selectAll();
        for (Classes cls : list) {
            System.out.println(cls.getId() + "," + cls.getName());
            List students = cls.getStudents();
            for (Student student : students) {
                System.out.println("\t" + student);
            }
        }

        sqlSession.close();
        is.close();
    }
}

3.多对多

环境准备

bean.Course:

package Mybatis5.bean;

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 + '\'' +
                '}';
    }
}

修改bean.Student:

package Mybatis5.bean;

import java.util.List;

public class Student {
    private Integer id; //主键id
    private String name; //学生姓名
    private Integer age; //学生年龄
    private List courses; //学生所选择的课程对象

    public Student() {
    }

    public Student(Integer id, String name, Integer age, List 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 getCourses() {
        return courses;
    }

    public void setCourses(List courses) {
        this.courses = courses;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", courses=" + courses +
                '}';
    }
}

功能实现

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

CourseMapper接口:

package Mybatis5.many_to_many;

import Mybatis5.bean.Course;
import org.apache.ibatis.annotations.Select;

import java.util.List;

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 selectBySid(Integer id);
}





    
    
    
    
        
    

    
    
        


    
    
        
    

        
    
        
        
            
            
            
            
                
                
                
                
                
            
        
    
    
    
    
        
        
        
        
    
    

使用注解配置StudentMapper:

package Mybatis5.many_to_many;

import Mybatis5.bean.Student;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

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",          // 根据查询出student表的id来作为关联条件,去查询中间表和课程表
                    /*
                        many、@Many 一对多查询的固定写法
                        select属性:指定调用哪个接口中的哪个查询方法
                     */
                    many = @Many(select = "Mybatis5.many_to_many.CourseMapper.selectBySid")
            )
    })
    public abstract List selectAll();
}

测试类:

package Mybatis5.many_to_many;

import Mybatis5.bean.Course;
import Mybatis5.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List list = mapper.selectAll();
        for (Student student : list) {
            System.out.println(student.getId() + "," + student.getName() + "," + student.getAge());
            List courses = student.getCourses();
            for (Course cours : courses) {
                System.out.println("\t" + cours);
            }
        }

        sqlSession.close();
        is.close();
    }
}

你可能感兴趣的:(Mybatis,mybatis,java,spring)