转自:https://blog.csdn.net/yalishadaa/article/details/56038992
1、普通映射
@Select("select * from user where id=#{id}") public User getUser(int id); @Insert("insert into user(id,name) values (#{id},#{name})") public int insert(User user); @Update(update user set name=#{name} where id=#{id}) public int update(User user); @Delete("delete from user where id=#{id}") public int delete(int id);
2、结果集映射
@Select("select * from user") @Results({ @Result(id=true,property =""id,column = "id") @Result(property = "name" column="name") }) public List<User> getAllUsers();
3、关系映射
一对一
@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<Student> getAllStudents();
一对多
@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);