一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

一、一对一

/*
1、在mybatis.xml配置文件中,开启二级缓存
    <settings>
        <!--开启二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
*/
@CacheNamespace(blocking = true)// 2、启用二级缓存
public interface StudentMapper {
    Integer insertStudent(Student student);

    List<Student> selectAllStudent(Map map);

    Student selectStudentGradeById(Integer id);

    List<Student> selectStudentsByGrade(Integer gradeId);

    // 一对一通常使用立即加载(FetchType.EAGER)
    @Select("select * from student where id=#{id}")
    @Results(id = "studentMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "age", property = "age"),
            @Result(column = "sex", property = "sex"),
            @Result(column = "grade_id", property = "gradeId"),
            @Result(column = "grade_id", property = "grade",
                    one = @One(select = "com.wuxi.daos.GradeMapper.selectById",
                            fetchType = FetchType.EAGER)),
    })
    Student selectStudentByIdAnno(Integer id);

    @Select("select * from student")
    @ResultMap("studentMap")
    List<Student> selectAllStudentAnno();

    @Insert("insert into student(name,age,sex,grade_id) values(#{name},#{age},#{sex},#{gradeId})")
    Integer insertStudentAnno(Student student);

    @Update("update student set name=#{name}, age=#{age}, sex=#{sex} where id=#{id}")
    Integer updateStudentByIdAnno(Student student);

    @Delete("delete from student where id=#{id}")
    Integer deleteStudentByIdAnno(Integer id);
}

二、一对多

public interface GradeMapper {
    // 注解和xml配置可以同时使用
    Grade selectById(Integer id);

    // 一对多通常使用延迟加载(FetchType.LAZY)
    @Select("select * from grade where id = #{id}")
    @Results(id = "gradeMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "id", property = "students",
                    many = @Many(select = "com.wuxi.daos.StudentMapper.selectStudentsByGrade",
                            fetchType = FetchType.LAZY)),
    })
    Grade selectByIdAnno(Integer id);
}

 

posted on 2020-09-10 21:35  一路繁花似锦绣前程  阅读(125)  评论(0编辑  收藏  举报