1. 程式人生 > 實用技巧 >從一知半解到揭曉Java高階語法—泛型

從一知半解到揭曉Java高階語法—泛型

一、一對一

/*
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);
}