Mybatis基于注解方式表关系实现总结

前提:mybatis注解方式开发环境搭建完成。

一、一对一

目录:

Mybatis基于注解方式表关系实现总结_第1张图片

Mybatis基于注解方式表关系实现总结_第2张图片

SqlMapConfig.xml:





    
    
        
        
            
            
            
            
                
                
                
                
                
            
        
    

    
    
        
        
        
    

实体类:

public class Student implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private Date birthday;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}
public class Scores implements Serializable {
    private Integer id;
    private Integer score;
    private Integer sid;
    //从表实体类应该包含一个主表实体类的对象引用
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    @Override
    public String toString() {
        return "Scores{" +
                "id=" + id +
                ", score=" + score +
                ", sid=" + sid +
                '}';
    }
}

接口:

public interface ScoresDao {

    /**
     * 查询所有分数
     * @return
     */
    @Results(id = "scoresMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "sid",property = "sid"),
            @Result(column = "score",property = "score"),
            @Result(column = "sid",property = "student",one = @One(select = "com.chenlei.dao.StudentDao.findById",fetchType = FetchType.EAGER))
    })
    @Select("select * from scores")
    List findAll();
}
public interface StudentDao {
    /**
     * 查找所有操作
     * @return
     */
    @Results(id = "studentMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "birthday",property = "birthday")
    })
    @Select("select * from student")
    List findAll();

    /**
     * 根据id查询学生
     * @param id
     */
    @Select("select * from student where id =#{id}")
    @ResultMap("studentMap")
    Student findById (Integer id);
}

测试类:

public class ScoresTest {
    private InputStream in;
    private SqlSession sqlSession;
    private ScoresDao scoresDao;

    //用于测试方法测试之前执行
    @Before
    public void init() throws IOException {
        //1.读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.使用工厂生产SqlSession对象
        sqlSession = factory.openSession();
        ///4.使用SqlSession创建Dao接口的代理对象
        scoresDao = sqlSession.getMapper(ScoresDao.class);
    }

    //用于测试方法之后执行
    @After
    public void destroy() throws IOException {
        //提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
        in.close();
    }

    @Test
    public void findAll() {
        //执行查询所有对象方法
        List scores = scoresDao.findAll();
        for (Scores scorez : scores) {
            System.out.println("-------------");
            System.out.println(scorez);
            System.out.println(scorez.getStudent());
        }
    }
}

结果:

Mybatis基于注解方式表关系实现总结_第3张图片

注意问题:

对于ScoresDao接口注释中one = @One中select属性是相当于是给我们一个对应的编号进行查询学生对象,从而获取这个对应的对象值,故select属性中所填的值是根据id查询学生对象的接口路径。

二、一对多

表格(一个学生有多个成绩):

Mybatis基于注解方式表关系实现总结_第4张图片

Mybatis基于注解方式表关系实现总结_第5张图片

实体类改动:

public class Student implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private Date birthday;
    //一对多的关系映射:主表实体应该包含从表实体的集合引用
    private List scores;

    public List getScores() {
        return scores;
    }

    public void setScores(List scores) {
        this.scores = scores;
    }

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}
public class Scores implements Serializable {
    private Integer id;
    private Integer score;
    private Integer sid;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    @Override
    public String toString() {
        return "Scores{" +
                "id=" + id +
                ", score=" + score +
                ", sid=" + sid +
                '}';
    }
}

接口:

public interface ScoresDao {
    /**
     * 根据学生信息查询成绩
     * @param sid
     * @return
     */
    @Select("select * from scores where sid=#{#sid}")
    List findScoresByid(Integer sid);
}
public interface StudentDao {
    /**
     * 查找所有操作
     * @return
     */
    @Results(id = "studentMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "birthday",property = "birthday"),
            @Result(property = "scores",column = "id",many = @Many(
                    select = "com.chenlei.dao.ScoresDao.findScoresByid",
                    fetchType = FetchType.EAGER
            ))
    })
    @Select("select * from student")
    List findAll();
} 

测试类:

public class MybatisTest {
    private InputStream in;
    private SqlSession sqlSession;
    private StudentDao studentDao;

    //用于测试方法测试之前执行
    @Before
    public void init() throws IOException {
        //1.读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.使用工厂生产SqlSession对象
        sqlSession = factory.openSession();
        ///4.使用SqlSession创建Dao接口的代理对象
        studentDao = sqlSession.getMapper(StudentDao.class);
    }

    //用于测试方法之后执行
    @After
    public void destroy() throws IOException {
        //提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
        in.close();
    }

    @Test
    public void findAll() {
        //执行查询所有对象方法
        List students = studentDao.findAll();
        for (Student student : students) {
            System.out.println(student);
            System.out.println(student.getScores());
        }
    }
}

结果:

Mybatis基于注解方式表关系实现总结_第6张图片

顺带提一下开启二级缓存的注解:

你可能感兴趣的:(Mybatis)