美文网首页
MyBatis中的几种注解映射

MyBatis中的几种注解映射

作者: 音乐与咖啡Bean | 来源:发表于2018-06-06 15:09 被阅读0次

    原文链接:http://blog.csdn.net/naruto_mr/article/details/48207437

    1.普通映射

    @Select("select * from mybatis_Student where id=#{id}")    

    public Student getStudent(int id);    

    @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")    

    public int insert(Student student);    

    @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")    

    public int update(Student student);    

    @Delete("delete from mybatis_Student where id=#{id}")    

    public int delete(int id);    

    2.结果集映射

    @Select("select * from mybatis_Student")    

    @Results({    

    @Result(id=true,property="id",column="id"),    

    @Result(property="name",column="name"),    

    @Result(property="age",column="age")    

    })    

    public List getAllStudents();    

    3.关系映射

    3.1一对一

    @Select("select * from mybatis_Student")    

    @Results({    

    @Result(id=true,property="id",column="id"),    

    @Result(property="name",column="name"),    

    @Result(property="age",column="age"),    

    @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))    

    })    

    public List getAllStudents();   

    3.2一对多

    @Select("select * from mybatis_grade where id=#{id}")    

    @Results({    

    @Result(id=true,column="id",property="id"),    

    @Result(column="grade_name",property="gradeName"),    

    @Result(property="students",column="id",many=@Many(select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))    

       })    

    public Grade getGrade(int id); 

    相关文章

      网友评论

          本文标题:MyBatis中的几种注解映射

          本文链接:https://www.haomeiwen.com/subject/bqbbsftx.html