zoukankan      html  css  js  c++  java
  • mybatis注解开发

    一、一对一

    /*
    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);
    }
  • 相关阅读:
    Button 样式设置
    WPF 运行报错:在使用 ItemsSource 之前,项集合必须为空。
    c# List 按条件查找、删除
    c# WPF DataGrid设置一列自增一
    C# WPF DataGrid去掉最左侧自动生成一列
    int 转换成定长的 byte数组
    字节数组 byte[] 与 int型数字的相互转换
    [ c# ] int 类型转换为固定长度的字符串
    ListView 绑定 字典
    不能引用的文件,却需要在程序底层使用的文件 的存放位置
  • 原文地址:https://www.cnblogs.com/linding/p/13648555.html
Copyright © 2011-2022 走看看