分享

MyBatis学习笔记(十)注解映射

 瀚海璨夜 2016-10-18

1.普通映射

  1. @Select("select * from mybatis_Student where id=#{id}")  
  2. public Student getStudent(int id);  
  3. @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
  4. public int insert(Student student);  
  5. @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
  6. public int update(Student student);  
  7. @Delete("delete from mybatis_Student where id=#{id}")  
  8. public int delete(int id);  

2.结果集映射

  1. @Select("select * from mybatis_Student")  
  2. @Results({  
  3.     @Result(id=true,property="id",column="id"),  
  4.     @Result(property="name",column="name"),  
  5.     @Result(property="age",column="age")  
  6. })  
  7. public List<Student> getAllStudents();  

3.关系映射

1),一对一

  1. @Select("select * from mybatis_Student")  
  2. @Results({  
  3.     @Result(id=true,property="id",column="id"),  
  4.     @Result(property="name",column="name"),  
  5.     @Result(property="age",column="age"),  
  6.     @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
  7. })  
  8. public List<Student> getAllStudents();  
2)一对多

  1. package com.skymr.mybatis.mappers;  
  2.   
  3. import org.apache.ibatis.annotations.Many;  
  4. import org.apache.ibatis.annotations.Result;  
  5. import org.apache.ibatis.annotations.Results;  
  6. import org.apache.ibatis.annotations.Select;  
  7.   
  8. import com.skymr.mybatis.model.Grade;  
  9.   
  10. public interface Grade2Mapper {  
  11.   
  12.     @Select("select * from mybatis_grade where id=#{id}")  
  13.     @Results({  
  14.         @Result(id=true,column="id",property="id"),  
  15.         @Result(column="grade_name",property="gradeName"),  
  16.         @Result(property="students",column="id",many=@Many(select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))  
  17.     })  
  18.     public Grade getGrade(int id);  
  19. }  

  1. package com.skymr.mybatis.mappers;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.annotations.Delete;  
  6. import org.apache.ibatis.annotations.Insert;  
  7. import org.apache.ibatis.annotations.One;  
  8. import org.apache.ibatis.annotations.Result;  
  9. import org.apache.ibatis.annotations.Results;  
  10. import org.apache.ibatis.annotations.Select;  
  11. import org.apache.ibatis.annotations.Update;  
  12.   
  13. import com.skymr.mybatis.model.Student;  
  14.   
  15. public interface Student2Mapper {  
  16.   
  17.     @Select("select * from mybatis_Student where id=#{id}")  
  18.     public Student getStudent(int id);  
  19.     @Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")  
  20.     public int insert(Student student);  
  21.     @Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")  
  22.     public int update(Student student);  
  23.     @Delete("delete from mybatis_Student where id=#{id}")  
  24.     public int delete(int id);  
  25.       
  26.     @Select("select * from mybatis_Student")  
  27.     @Results({  
  28.         @Result(id=true,property="id",column="id"),  
  29.         @Result(property="name",column="name"),  
  30.         @Result(property="age",column="age"),  
  31.         @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
  32.     })  
  33.     public List<Student> getAllStudents();  
  34.     @Select("select * from mybatis_Student where grade_id=#{gradeId}")  
  35.         @Results({  
  36.         @Result(id=true,property="id",column="id"),  
  37.         @Result(property="name",column="name"),  
  38.         @Result(property="age",column="age"),  
  39.         @Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))  
  40.     })  
  41.     public List<Student> getStudentsByGradeId(int gradeId);  
  42. }  


4.动态sql注解映射

provider类

  1. package com.skymr.mybatis.mappers.provider;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.apache.ibatis.jdbc.SQL;  
  6.   
  7. import com.skymr.mybatis.model.Student;  
  8.   
  9. public class StudentDynaSqlProvider {  
  10.   
  11.     public String insertStudent(final Student student){  
  12.         return new SQL(){  
  13.             {  
  14.                 INSERT_INTO("mybatis_Student");  
  15.                 if(student.getName() != null){  
  16.                     VALUES("name","#{name}");  
  17.                 }  
  18.                 if(student.getAge() > 0){  
  19.                     VALUES("age","#{age}");  
  20.                 }  
  21.             }  
  22.         }.toString();  
  23.     }  
  24.       
  25.     public String updateStudent(final Student student){  
  26.         return new SQL(){  
  27.             {  
  28.                 UPDATE("mybatis_Student");  
  29.                 if(student.getName() != null){  
  30.                     SET("name=#{name}");  
  31.                 }  
  32.                 if(student.getAge() > 0){  
  33.                     SET("age=#{age}");  
  34.                 }  
  35.                 WHERE("id=#{id}");  
  36.             }  
  37.         }.toString();  
  38.     }  
  39.       
  40.     public String getStudent(final Map<String,Object> map){  
  41.         return new SQL(){  
  42.             {  
  43.                 SELECT("*");  
  44.                 FROM("mybatis_Student");  
  45.                 if(map.containsKey("name")){  
  46.                     WHERE("name like #{name}");  
  47.                 }  
  48.                 if(map.containsKey("age")){  
  49.                     WHERE("age=#{age}");  
  50.                 }  
  51.             }  
  52.         }.toString();  
  53.     }  
  54.       
  55.     public String deleteStudent(){  
  56.         return new SQL(){  
  57.             {  
  58.                 DELETE_FROM("mybatis_Student");  
  59.                 WHERE("id=#{id}");  
  60.             }  
  61.         }.toString();  
  62.     }  
  63. }  
Mapper接口
  1. @SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent")  
  2. public List<Student> getStudents(Map<String,Object> map);  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多