• MyBatis 3 使用注解配置SQL映射器


    • l 在映射器Mapper接口上使用注解
    • l 映射语句

               @Insert,@Update,@Delete,@SeelctStatements

    • l 结果映射

               一对一映射

               一对多映射

    • l 动态SQL

               @SelectProvider

               @InsertProvider

               @UpdateProvider

               @DeleteProvider

    4.1 在映射器Mapper接口上使用注解

        MyBatis对于大部分的基于XML的映射器元素(包括<select>,<update>)提供了对应的基于注解的配置项。然而在某些情况下,基于注解配置 还不能支持基于XML的一些元素。

    4.2 映射语句

    MyBatis提供了多种注解来支持不同类型的语句(statement)如SELECT,INSERT,UPDATE,DELETE。让我们看一下具体怎样配置映射语句。

    4.2.1 @Insert

    我们可以使用@Insert注解来定义一个INSERT映射语句:

    1. package com.mybatis3.mappers;  
    2. public interface StudentMapper  
    3. {  
    4.     @Insert("INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,ADDR_ID, PHONE)  
    5.             VALUES(#{studId},#{name},#{email},#{address.addrId},#{phone})")  
    6.     int insertStudent(Student student);  
    7. }  
    使用了@Insert注解的insertMethod()方法将返回insert语句执行后影响的行数。

    [自动生成主键]

    在上一章中我们讨论过主键列值可以自动生成。我们可以使用@Options注解的userGeneratedKeys 和keyProperty属性让数据库产生auto_increment(自增长)列的值,然后将生成的值设置到输入参数对象的属性中。

    1. @Insert("INSERT INTO STUDENTS(NAME,EMAIL,ADDR_ID, PHONE)  
    2.         VALUES(#{name},#{email},#{address.addrId},#{phone})")  
    3. @Options(useGeneratedKeys = true, keyProperty = "studId")  
    4. int insertStudent(Student student);  

    这里STUD_ID列值将会通过MySQL数据库自动生成。并且生成的值将会被设置到student对象的studId属性中。
    1. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);  
    2. mapper.insertStudent(student);  
    3. int studentId = student.getStudId();  

    有一些数据库如Oracle,并不支持AUTO_INCREMENT列属性,它使用序列(SEQUENCE)来产生主键的值。

    我们可以使用@SelectKey注解来为任意SQL语句来指定主键值,作为主键列的值。

    假设我们有一个名为STUD_ID_SEQ的序列来生成STUD_ID主键值。
    1. @Insert("INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,ADDR_ID, PHONE)   
    2. VALUES(#{studId},#{name},#{email},#{address.addrId},#{phone})")  
    3. @SelectKey(statement="SELECT STUD_ID_SEQ.NEXTVAL FROM DUAL",   
    4. keyProperty="studId", resultType=int.class, before=true)  
    5. int insertStudent(Student student);  

    这里我们使用了@SelectKey来生成主键值,并且存储到了student对象的studId属性上。由于我们设置了before=true,该语句将会在执行INSERT语句之前执行。

    如果你使用序列作为触发器来设置主键值,我们可以在INSERT语句执行后,从sequence_name.currval获取数据库产生的主键值。
    1. @Insert("INSERT INTO STUDENTS(NAME,EMAIL,ADDR_ID, PHONE)   
    2. VALUES(#{name},#{email},#{address.addrId},#{phone})")  
    3. @SelectKey(statement="SELECT STUD_ID_SEQ.CURRVAL FROM DUAL",   
    4. keyProperty="studId", resultType=int.class, before=false)  
    5. int insertStudent(Student student);  

    4.2.2 @Update

    我们可以使用@Update注解来定义一个UPDATE映射语句,如下所示:

    1. @Update("UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email},   
    2. PHONE=#{phone} WHERE STUD_ID=#{studId}")  
    3. int updateStudent(Student student);  

    使用了@Update的updateStudent()方法将会返回执行了update语句后影响的行数。

    1. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);  
    2. int noOfRowsUpdated = mapper.updateStudent(student);  

    4.2.3 @Delete

    我们可以使用@Delete  注解来定义一个DELETE映射语句,如下所示:

    1. @Delete("DELETE FROM STUDENTS WHERE STUD_ID=#{studId}")  
    2. int deleteStudent(int studId);  
    使用了@Delete的deleteStudent()方法将会返回执行了update语句后影响的行数。

    4.2.4 @Select

    我们可以使用@ Select注解来定义一个SELECT映射语句。

    让我们看一下怎样使用注解配置一个简单的select查询。
    1. package com.mybatis3.mappers;  
    2. public interface StudentMapper  
    3. {  
    4.     @Select("SELECT STUD_ID AS STUDID, NAME, EMAIL, PHONE FROM  
    5.             STUDENTS WHERE STUD_ID=#{studId}")  
    6.     Student findStudentById(Integer studId);  
    7. }  

    为了将列名和Studentbean属性名匹配,我们为stud_id起了一个studId的别名。如果返回了多行结果,将抛出 TooManyResultsException异常。

    4.3 结果映射

    我们可以将查询结果通过别名或者是@Results注解与JavaBean属性映射起来。

    现在让我们看看怎样使用@Results注解将指定列于指定JavaBean属性映射器来,执行SELECT查询的:
    1. package com.mybatis3.mappers;  
    2. public interface StudentMapper  
    3. {  
    4. @Select("SELECT * FROM STUDENTS")  
    5. @Results(  
    6.     {  
    7. @Result(id = true, column = "stud_id", property = "studId"),  
    8. @Result(column = "name", property = "name"),  
    9. @Result(column = "email", property = "email"),  
    10. @Result(column = "addr_id", property = "address.addrId")  
    11.     })  
    12.     List<Student> findAllStudents();  
    13. }  

    例如,看下面的findStudentById()和findAllStudents()方法:
    1. @Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")  
    2. @Results(  
    3. {  
    4. @Result(id = true, column = "stud_id", property = "studId"),  
    5. @Result(column = "name", property = "name"),  
    6. @Result(column = "email", property = "email"),  
    7. @Result(column = "addr_id", property = "address.addrId")  
    8. })  
    9. Student findStudentById(int studId);  
    10. @Select("SELECT * FROM STUDENTS")  
    11. @Results(  
    12. {  
    13. @Result(id = true, column = "stud_id", property = "studId"),  
    14. @Result(column = "name", property = "name"),  
    15. @Result(column = "email", property = "email"),  
    16. @Result(column = "addr_id", property = "address.addrId")  
    17. })  
    18. List<Student> findAllStudents();  

    这里两个语句的@Results配置完全相同,但是我必须得重复它。这里有一个解决方法。我们可以创建一个映射器Mapper配置文件, 然后配置<resultMap>元素,然后使用@ResultMap注解引用此<resultMap>。

    在StudentMapper.xml中定义一个ID为StudentResult的<resultMap>。

    1. <mapper namespace="com.mybatis3.mappers.StudentMapper">  
    2. <resultMap type="Student" id="StudentResult">  
    3. <id property="studId" column="stud_id" />  
    4. <result property="name" column="name" />  
    5. <result property="email" column="email" />  
    6. <result property="phone" column="phone" />  
    7. </resultMap>  
    8. </mapper>  

    在StudentMapper.java中,使用@ResultMap引用名为StudentResult的resultMap。
    1. public interface StudentMapper  
    2. {  
    3. @Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")  
    4. @ResultMap("com.mybatis3.mappers.StudentMapper.StudentResult")  
    5.     Student findStudentById(int studId);  
    6. @Select("SELECT * FROM STUDENTS")  
    7. @ResultMap("com.mybatis3.mappers.StudentMapper.StudentResult")  
    8.     List<Student> findAllStudents();  
    9. }  

    4.3.1 一对一映射

    MyBatis提供了@One注解来使用嵌套select语句(Nested-Select)加载一对一关联查询数据。让我们看看怎样使用@One注解获取学生及其地址信息。
    1. public interface StudentMapper  
    2. {  
    3. @Select("SELECT ADDR_ID AS ADDRID, STREET, CITY, STATE, ZIP, COUNTRY  
    4.             FROM ADDRESSES WHERE ADDR_ID=#{id}")  
    5.     Address findAddressById(int id);  
    6. @Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId} ")  
    7. @Results(  
    8.     {  
    9. @Result(id = true, column = "stud_id", property = "studId"),  
    10. @Result(column = "name", property = "name"),  
    11. @Result(column = "email", property = "email"),  
    12. @Result(property = "address", column = "addr_id",  
    13.         one = @One(select = "com.mybatis3.mappers.StudentMapper.  
    14.         findAddressById"))  
    15.     })  
    16.     Student selectStudentWithAddress(int studId);  
    17. }  

    这里我们使用了@One注解的select属性来指定一个使用了完全限定名的方法上,该方法会返回一个Address对象。使用column=”addr_id”,则STUEDNTS表中列addr_id的值将会作为输入参数传递给findAddressById()方法。如果@OneSELECT查询返回了多行结果,则会抛出TooManyResultsException异常。
    1. int studId = 1;  
    2. StudentMapper studentMapper =   
    3. sqlSession.getMapper(StudentMapper.class);  
    4. Student student = studentMapper.selectStudentWithAddress(studId);  
    5. System.out.println("Student :"+student);  
    6. System.out.println("Address :"+student.getAddress());  

    在第三章,使用XML配置SQL映射器中我们讨论过,我们可以通过基于XML的映射器配置,使用嵌套结果ResultMap来加载一对一关联的查询。而MyBatis3.2.2版本,并没有对应的注解支持。但是我们可以在映射器Mapper配置文件中配置<resultMap>并且使用@ResultMap注解来引用它。

    在StudentMapper.xml中配置<resultMap>,如下所示:
    1. <mapper namespace="com.mybatis3.mappers.StudentMapper">  
    2.   <resultMap type="Address" id="AddressResult">  
    3.     <id property="addrId" column="addr_id" />  
    4.     <result property="street" column="street" />  
    5.     <result property="city" column="city" />  
    6.     <result property="state" column="state" />  
    7.     <result property="zip" column="zip" />  
    8.     <result property="country" column="country" />  
    9.   </resultMap>  
    10.   <resultMap type="Student" id="StudentWithAddressResult">  
    11.     <id property="studId" column="stud_id" />  
    12.     <result property="name" column="name" />  
    13.     <result property="email" column="email" />  
    14.     <association property="address" resultMap="AddressResult" />  
    15.   </resultMap>  
    16. </mapper>  
    1. public interface StudentMapper  
    2. {  
    3.     @Select("select stud_id, name, email, a.addr_id, street, city,  
    4.             state, zip, country" + " FROM students s left outer join addresses a  
    5.             on s.addr_id=a.addr_id" + " where stud_id=#{studId} ")  
    6.     @ResultMap("com.mybatis3.mappers.StudentMapper.  
    7.                StudentWithAddressResult")  
    8.     Student selectStudentWithAddress(int id);  
    9. }  

    4.3.2 一对多映射

    MyBatis提供了@Many注解,用来使用嵌套Select语句加载一对多关联查询。

    现在让我们看一下如何使用@Many注解获取一个讲师及其教授课程列表信息:
    1. public interface TutorMapper  
    2. {  
    3. @Select("select addr_id as addrId, street, city, state, zip,  
    4.             country from addresses where addr_id=#{id}")  
    5.     Address findAddressById(int id);  
    6. @Select("select * from courses where tutor_id=#{tutorId}")  
    7. @Results(  
    8.     {  
    9. @Result(id = true, column = "course_id", property = "courseId"),  
    10. @Result(column = "name", property = "name"),  
    11. @Result(column = "description", property = "description"),  
    12. @Result(column = "start_date" property = "startDate"),  
    13. @Result(column = "end_date" property = "endDate")  
    14.     })  
    15.     List<Course> findCoursesByTutorId(int tutorId);  
    16. @Select("SELECT tutor_id, name as tutor_name, email, addr_id  
    17.             FROM tutors where tutor_id=#{tutorId}")  
    18. @Results(  
    19.     {  
    20. @Result(id = true, column = "tutor_id", property = "tutorId"),  
    21. @Result(column = "tutor_name", property = "name"),  
    22. @Result(column = "email", property = "email"),  
    23. @Result(property = "address", column = "addr_id",  
    24.         one = @One(select = " com.mybatis3.  
    25.         mappers.TutorMapper.findAddressById")),  
    26. @Result(property = "courses", column = "tutor_id",  
    27.         many = @Many(select = "com.mybatis3.mappers.TutorMapper.  
    28.         findCoursesByTutorId"))  
    29.     })  
    30.     Tutor findTutorById(int tutorId);  
    31. }  

    这里我们使用了@Many注解的select属性来指向一个完全限定名称的方法,该方法将返回一个List<Course>对象。使用column=”tutor_id”,TUTORS表中的tutor_id列值将会作为输入参数传递给findCoursesByTutorId()方法。

    在第三章,使用XML配置SQL映射器中我们讨论过,我们可以通过基于XML的映射器配置,使用嵌套结果ResultMap来加载一对多关联的查询。而MyBatis3.2.2版本,并没有对应的注解支持。但是我们可以在映射器Mapper配置文件中配置<resultMap>并且使用@ResultMap注解来引用它。

    在TutorMapper.xml中配置<resultMap>,如下所示:

    1. <mapper namespace="com.mybatis3.mappers.TutorMapper">  
    2. <resultMap type="Address" id="AddressResult">  
    3. <id property="addrId" column="addr_id" />  
    4. <result property="street" column="street" />  
    5. <result property="city" column="city" />  
    6. <result property="state" column="state" />  
    7. <result property="zip" column="zip" />  
    8. <result property="country" column="country" />  
    9. </resultMap>  
    10. <resultMap type="Course" id="CourseResult">  
    11. <id column="course_id" property="courseId" />  
    12. <result column="name" property="name" />  
    13. <result column="description" property="description" />  
    14. <result column="start_date" property="startDate" />  
    15. <result column="end_date" property="endDate" />  
    16. </resultMap>  
    17. <resultMap type="Tutor" id="TutorResult">  
    18. <id column="tutor_id" property="tutorId" />  
    19. <result column="tutor_name" property="name" />  
    20. <result column="email" property="email" />  
    21. <association property="address" resultMap="AddressResult" />  
    22. <collection property="courses" resultMap="CourseResult" />  
    23. </resultMap>  
    24. </mapper>  
    1. public interface TutorMapper  
    2. {  
    3. @Select("SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL,  
    4.             A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY, COURSE_ID, C.NAME,  
    5.             DESCRIPTION, START_DATE, END_DATE  FROM TUTORS T LEFT OUTER  
    6.             JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID LEFT OUTER JOIN COURSES  
    7.             C ON T.TUTOR_ID=C.TUTOR_ID WHERE T.TUTOR_ID=#{tutorId}")  
    8. @ResultMap("com.mybatis3.mappers.TutorMapper.TutorResult")  
    9.     Tutor selectTutorById(int tutorId);  
    10. }  

    4.4 动态SQL

    有时候我们需要根据输入条件动态地构建SQL语句。MyBatis提供了各种注解如@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,来帮助构建动态SQL语句,然后让MyBatis执行这些SQL语句。

    4.4.1 @SelectProvider

    现在让我们来看一个使用@SelectProvider注解来创建一个简单的SELECT映射语句的例子。

    创建一个TutorDynaSqlProvider.java类,以及findTutorByIdSql()方法,如下所示:
    1. package com.mybatis3.sqlproviders;  
    2. import org.apache.ibatis.jdbc.SQL;  
    3. public class TutorDynaSqlProvider  
    4. {  
    5.     public String findTutorByIdSql(int tutorId)  
    6.     {  
    7.         return "SELECT TUTOR_ID AS tutorId, NAME, EMAIL FROM TUTORS  
    8.                WHERE TUTOR_ID=" + tutorId;  
    9.     }  
    10. }  
    在TutorMapper.java接口中创建一个映射语句,如下:
    1. @SelectProvider(type=TutorDynaSqlProvider.class, method="findTutorByIdSql")  
    2. Tutor findTutorById(int tutorId);  

    这里我们使用了@SelectProvider来指定了一个类,及其内部的方法,用来提供需要执行的SQL语句。

    但是使用字符串拼接的方法唉构建SQL语句是非常困难的,并且容易出错。所以MyBaits提供了一个SQL工具类不使用字符串拼接的方式,简化构造动态SQL语句。

    现在,让我们看看如何使用org.apache.ibatis.jdbc.SQL工具类来准备相同的SQL语句。

    1. package com.mybatis3.sqlproviders;  
    2. import org.apache.ibatis.jdbc.SQL;  
    3. public class TutorDynaSqlProvider  
    4. {  
    5.     public String findTutorByIdSql(final int tutorId)  
    6.     {  
    7.         return new SQL()  
    8.         {  
    9.             {  
    10.                 SELECT("tutor_id as tutorId, name, email");  
    11.                 FROM("tutors");  
    12.                 WHERE("tutor_id=" + tutorId);  
    13.             }  
    14.         } .toString();  
    15.     }  
    16. }  

    SQL工具类会处理以合适的空格前缀和后缀来构造SQL语句。

    动态SQL provider方法可以接收以下其中一种参数:

    •            ž 无参数
    •            ž 和映射器Mapper接口的方法同类型的参数
    •            ž java.util.Map

    如果SQL语句的准备不取决于输入参数,你可以使用不带参数的SQL Provider方法。

    例如:

    1. public String findTutorByIdSql()  
    2. {  
    3.     return new SQL()  
    4.     {  
    5.         {  
    6.             SELECT("tutor_id as tutorId, name, email");  
    7.             FROM("tutors");  
    8.             WHERE("tutor_id = #{tutorId}");  
    9.         }  
    10.     } .toString();  
    11. }  

    这里我们没有使用输入参数构造SQL语句,所以它可以是一个无参方法。

    如果映射器Mapper接口方法只有一个参数,那么可以定义SQLProvider方法,它接受一个与Mapper接口方法相同类型的参数。

    例如映射器Mapper接口有如下定义:

     

    <span style="font-family:Microsoft YaHei;font-size:12px;">Tutor findTutorById(int tutorId);</span>  

    这里findTutorById(int)方法只有一个int类型的参数。我们可以定义findTutorByIdSql(int)方法作为SQL provider方法。

    1. public String findTutorByIdSql(final int tutorId)  
    2. {  
    3.     return new SQL()  
    4.     {  
    5.         {  
    6.             SELECT("tutor_id as tutorId, name, email");  
    7.             FROM("tutors");  
    8.             WHERE("tutor_id=" + tutorId);  
    9.         }  
    10.     } .toString();  
    11. }  
    如果映射器Mapper接口有多个输入参数,我们可以使用参数类型为java.util.Map的方法作为SQLprovider方法。然后映射器Mapper接口方法所有的输入参数将会被放到map中,以param1,param2等等作为key,将输入参数按序作为value。你也可以使用0,1,2等作为key值来取的输入参数。
    1. @SelectProvider(type = TutorDynaSqlProvider.class,  
    2.                 method = "findTutorByNameAndEmailSql")  
    3. Tutor findTutorByNameAndEmail(String name, String email);  
    4. public String findTutorByNameAndEmailSql(Map<String, Object> map)  
    5. {  
    6.     String name = (String) map.get("param1");  
    7.     String email = (String) map.get("param2");  
    8.     //you can also get those values using 0,1 keys  
    9.     //String name = (String) map.get("0");  
    10.     //String email = (String) map.get("1");  
    11.     return new SQL()  
    12.     {  
    13.         {  
    14.             SELECT("tutor_id as tutorId, name, email");  
    15.             FROM("tutors");  
    16.             WHERE("name=#{name} AND email=#{email}");  
    17.         }  
    18.     } .toString();  
    19. }  

    SQL工具类也提供了其他的方法来表示JOINS,ORDER_BY,GROUP_BY等等。

    让我们看一个使用LEFT_OUTER_JOIN的例子:
    1. public class TutorDynaSqlProvider  
    2. {  
    3.     public String selectTutorById()  
    4.     {  
    5.         return new SQL()  
    6.         {  
    7.             {  
    8.                 SELECT("t.tutor_id, t.name as tutor_name, email");  
    9.                 SELECT("a.addr_id, street, city, state, zip, country");  
    10.                 SELECT("course_id, c.name as course_name, description,  
    11.                        start_date, end_date");  
    12.                 FROM("TUTORS t");  
    13.                 LEFT_OUTER_JOIN("addresses a on t.addr_id=a.addr_id");  
    14.                 LEFT_OUTER_JOIN("courses c on t.tutor_id=c.tutor_id");  
    15.                 WHERE("t.TUTOR_ID = #{id}");  
    16.             }  
    17.         } .toString();  
    18.     }  
    19. }  
    20.   
    21. public interface TutorMapper  
    22. {  
    23.     @SelectProvider(type = TutorDynaSqlProvider.class,  
    24.                     method = "selectTutorById")  
    25.     @ResultMap("com.mybatis3.mappers.TutorMapper.TutorResult")  
    26.     Tutor selectTutorById(int tutorId);  
    27. }  
    由于没有支持使用内嵌结果ResultMap的一对多关联映射的注解支持,我们可以使用基于XML的<resultMap>配置,然后与@ResultMap映射。
    1. <mapper namespace="com.mybatis3.mappers.TutorMapper">  
    2.   <resultMap type="Address" id="AddressResult">  
    3.     <id property="id" column="addr_id" />  
    4.     <result property="street" column="street" />  
    5.     <result property="city" column="city" />  
    6.     <result property="state" column="state" />  
    7.     <result property="zip" column="zip" />  
    8.     <result property="country" column="country" />  
    9.   </resultMap>  
    10.   <resultMap type="Course" id="CourseResult">  
    11.     <id column="course_id" property="id" />  
    12.     <result column="course_name" property="name" />  
    13.     <result column="description" property="description" />  
    14.     <result column="start_date" property="startDate" />  
    15.     <result column="end_date" property="endDate" />  
    16.   </resultMap>  
    17.   <resultMap type="Tutor" id="TutorResult">  
    18.     <id column="tutor_id" property="id" />  
    19.     <result column="tutor_name" property="name" />  
    20.     <result column="email" property="email" />  
    21.     <association property="address" resultMap="AddressResult" />  
    22.     <collection property="courses" resultMap="CourseResult"></collection>  
    23.   </resultMap>  
    24. </mapper>  
    使用了动态的SQL provider,我们可以取得讲师及其地址和课程明细。

    4.4.2 @InsertProvider

    我们可以使用@InsertProvider注解创建动态的INSERT语句,如下所示:

    1. public class TutorDynaSqlProvider  
    2. {  
    3.     public String insertTutor(final Tutor tutor)  
    4.     {  
    5.         return new SQL()  
    6.         {  
    7.             {  
    8.                 INSERT_INTO("TUTORS");  
    9.                 if (tutor.getName() != null)  
    10.                 {  
    11.                     VALUES("NAME", "#{name}");  
    12.                 }  
    13.                 if (tutor.getEmail() != null)  
    14.                 {  
    15.                     VALUES("EMAIL", "#{email}");  
    16.                 }  
    17.             }  
    18.         } .toString();  
    19.     }  
    20. }  
    21. public interface TutorMapper  
    22. {  
    23.     @InsertProvider(type = TutorDynaSqlProvider.class,  
    24.                     method = "insertTutor")  
    25.     @Options(useGeneratedKeys = true, keyProperty = "tutorId")  
    26.     int insertTutor(Tutor tutor);  
    27. }  

    4.4.3 @UpdateProvider

    我们可以通过@UpdateProvider注解创建UPDATE语句,如下所示:

    1. public class TutorDynaSqlProvider  
    2. {  
    3.     public String updateTutor(final Tutor tutor)  
    4.     {  
    5.         return new SQL()  
    6.         {  
    7.             {  
    8.                 UPDATE("TUTORS");  
    9.                 if (tutor.getName() != null)  
    10.                 {  
    11.                     SET("NAME = #{name}");  
    12.                 }  
    13.                 if (tutor.getEmail() != null)  
    14.                 {  
    15.                     SET("EMAIL = #{email}");  
    16.                 }  
    17.                 WHERE("TUTOR_ID = #{tutorId}");  
    18.             }  
    19.         } .toString();  
    20.     }  
    21. }  
    22. public interface TutorMapper  
    23. {  
    24.     @UpdateProvider(type = TutorDynaSqlProvider.class,  
    25.                     method = "updateTutor")  
    26.     int updateTutor(Tutor tutor);  
    27. }  

    4.4.4 @DeleteProvider

    我们可以使用@DeleteProvider注解创建动态地DELETE语句,如下所示:

    1. public class TutorDynaSqlProvider  
    2. {  
    3.     public String deleteTutor(int tutorId)  
    4.     {  
    5.         return new SQL()  
    6.         {  
    7.             {  
    8.                 DELETE_FROM("TUTORS");  
    9.                 WHERE("TUTOR_ID = #{tutorId}");  
    10.             }  
    11.         } .toString();  
    12.     }  
    13. }  
    14. public interface TutorMapper  
    15. {  
    16.     @DeleteProvider(type = TutorDynaSqlProvider.class,  
    17.                     method = "deleteTutor")  
    18.     int deleteTutor(int tutorId);  
    19. }  

    4.5 总结

    在本章中,我们学习了怎样使用注解书写SQL映射语句。讨论了如何配置简单语句,一对一关系语句和一对多关系语句。

  • 相关阅读:
    请求分页中的内存分配
    Android开发实例总结
    出现java.lang.IllegalArgumentException异常
    请求分页中的内存分配之续
    HTTPCLIENT使用学习
    传输控制层协议TCP概述抄书
    freemarker中空值 null的处理 ?exists ?if_exists ?default(“”)
    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 大新博客 推酷 360安全浏览器 7.1
    jade 学习笔记 gulp 自动编译
    @resource和@autowired的区别是什么CSDN论坛CSDN.NET中国最大的IT技术社区 Google Chrome
  • 原文地址:https://www.cnblogs.com/jtlgb/p/8780594.html
Copyright © 2020-2023  润新知