MyBatis最佳实践:注解开发

  1. 注解:

    1. @Insert:添加
    2. @Update:修改
    3. @Delete:删除
    4. @Select:查询
    5. @Result:实现结果集封装
    6. @Results:可以和 @Reslult 一起使用,封装多个结果集
    7. @One:实现一对一和多对一的结果集封装
    8. @Many:实现一对多结果集封装
  2. MyBatis 注解不能实现动态SQL 

  3. 使用:

    1. SqlMapConfig.xml 配置文件
      
          
          
          
          
          
      
    2. 编写注解:
      1. 增删改查:
        @Select("select * from user")
        
        @Results(id = "userMap" ,value = {
                @Result(property = "id",column = "id"),
                @Result(property = "username",column = "username"),
                @Result(property = "birthday",column = "birthday"),
                @Result(property = "sex",column = "sex"),
                @Result(property = "address",column = "address")
        })
        List findAll();
        
        @Select("select * from user where id = #{id}")
        @ResultMap(value = "userMap")
        List findById(Integer id);
        
        @Select("select * from user where username = #{name}")
        @ResultMap(value = "userMap")
        List findByName(String name);
        
        @Update("update user set username = #{username},birthday = #{birthday},sex = #{sex},address = #{address} where id = #{id}")
        int update(User user);
        
        @Delete("delete from user where  id = #{id}")
        int delete(Integer id);
        
        @Insert("insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})")
        int insert(User user);
    3. 多对一注解查询:
      1. 立即加载:
        @Select("select student.*,teacher.Tname from student left join teacher on student.t_id = teacher.id")
        @Results(id = "StudentMap",value = {
                @Result(property = "id",column = "id"),
                @Result(property = "Sname",column = "Sname"),
                @Result(property = "sex",column = "sex"),
                @Result(property = "age",column = "age"),
                @Result(property = "t_id",column = "t_id"),
                @Result(property = "teacher.Tname",column = "Tname")
        })
        List SelectStudentTeacher();
      2. 延迟加载:
        @Select("select * from student")
        @Results(id = "StudentMap",value = {
                @Result(property = "id",column = "id"),
                @Result(property = "Sname",column = "Sname"),
                @Result(property = "sex",column = "sex"),
                @Result(property = "age",column = "age"),
                @Result(property = "t_id",column = "t_id"),
                @Result(property = "teacher",column = "t_id",one = @One(select = "com.mybatis.Dao.TeacherDao.findTeacherById"))
        })
        List SelectStudentTeacher();
        
        //teacherDao接口
        @Select("select * from teacher where id = #{t_id}")
        List findTeacherById(Integer t_id);
      3. 一对多注解查询:
        @Select("select * from teacher")
        @Results(id = "teacherMap",value = {
                @Result(property = "id",column = "id"),
                @Result(property = "Tname",column = "Tname"),
                @Result(property = "student",column = "id",
                        many = @Many(select = "com.mybatis.Dao.StudentDao.selectStudentByTeacherId",
                                fetchType = FetchType.LAZY))    //延迟加载
        })
        List selectTeacherStudent();
        
        
        //studentDao接口
        @Select("select * from student where t_id = #{id}")
        List selectStudentByTeacherId(Integer id);

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