映射注解示例

这个例子展示了如何使用 @SelectKey 注解来在插入前读取数据库序列的值:

  1. @Insert(“insert into table3 (id, name) values(#{nameId}, #{name})”)
  2. @SelectKey(statement=”call next value for TestSequence”, keyProperty=”nameId”, before=true, resultType=int.class)
  3. int insertTable3(Name name);

这个例子展示了如何使用 @SelectKey 注解来在插入后读取数据库自增列的值:

  1. @Insert(“insert into table2 (name) values(#{name})”)
  2. @SelectKey(statement=”call identity()”, keyProperty=”nameId”, before=false, resultType=int.class)
  3. int insertTable2(Name name);

这个例子展示了如何使用 @Flush 注解来调用 SqlSession#flushStatements():

  1. @Flush
  2. List<BatchResult> flush();

这些例子展示了如何通过指定 @Result 的 id 属性来命名结果集:

  1. @Results(id = userResult”, value = {
  2. @Result(property = id”, column = uid”, id = true),
  3. @Result(property = firstName”, column = first_name”),
  4. @Result(property = lastName”, column = last_name”)
  5. })
  6. @Select(“select * from users where id = #{id}”)
  7. User getUserById(Integer id);

  8. @Results(id = companyResults”)

  9. @ConstructorArgs({

  10. @Arg(column = cid”, javaType = Integer.class, id = true),

  11. @Arg(column = name”, javaType = String.class)

  12. })

  13. @Select(“select * from company where id = #{id}”)

  14. Company getCompanyById(Integer id);

这个例子展示了如何使用单个参数的 @SqlProvider 注解:

  1. @SelectProvider(type = UserSqlBuilder.class, method = buildGetUsersByName”)
  2. List<User> getUsersByName(String name);

  3. class UserSqlBuilder {

  4. public static String buildGetUsersByName(final String name) {

  5. return new SQL(){{

  6. SELECT(“*”);

  7. FROM(“users”);

  8. if (name != null) {

  9. WHERE(“name like #{value} || ‘%’”);

  10. }

  11. ORDER_BY(“id”);

  12. }}.toString();

  13. }

  14. }

这个例子展示了如何使用多个参数的 @SqlProvider 注解:

  1. @SelectProvider(type = UserSqlBuilder.class, method = buildGetUsersByName”)
  2. List<User> getUsersByName(
  3. @Param(“name”) String name, @Param(“orderByColumn”) String orderByColumn);

  4. class UserSqlBuilder {

  5. // 如果不使用 @Param,就应该定义与 mapper 方法相同的参数

  6. public static String buildGetUsersByName(

  7. final String name, final String orderByColumn) {

  8. return new SQL(){{

  9. SELECT(“*”);

  10. FROM(“users”);

  11. WHERE(“name like #{name} || ‘%’”);

  12. ORDER_BY(orderByColumn);

  13. }}.toString();

  14. }

  15. // 如果使用 @Param,就可以只定义需要使用的参数

  16. public static String buildGetUsersByName(@Param(“orderByColumn”) final String orderByColumn) {

  17. return new SQL(){{

  18. SELECT(“*”);

  19. FROM(“users”);

  20. WHERE(“name like #{name} || ‘%’”);

  21. ORDER_BY(orderByColumn);

  22. }}.toString();

  23. }

  24. }

以下例子展示了 ProviderMethodResolver(3.5.1 后可用)的默认实现使用方法:

  1. @SelectProvider(UserSqlProvider.class)
  2. List<User> getUsersByName(String name);

  3. // 在你的 provider 类中实现 ProviderMethodResolver 接口

  4. class UserSqlProvider implements ProviderMethodResolver {

  5. // 默认实现中,会将映射器方法的调用解析到实现的同名方法上

  6. public static String getUsersByName(final String name) {

  7. return new SQL(){{

  8. SELECT(“*”);

  9. FROM(“users”);

  10. if (name != null) {

  11. WHERE(“name like #{value} || ‘%’”);

  12. }

  13. ORDER_BY(“id”);

  14. }}.toString();

  15. }

  16. }

This example shows usage the databaseId attribute on the statement annotation(Available since 3.5.5):

  1. @Select(value = SELECT SYS_GUID() FROM dual”, databaseId = oracle”) // Use this statement if DatabaseIdProvider provide “oracle”
  2. @Select(value = SELECT uuid_generate_v4()”, databaseId = postgres”) // Use this statement if DatabaseIdProvider provide “postgres”
  3. @Select(“SELECT RANDOM_UUID()”) // Use this statement if the DatabaseIdProvider not configured or not matches databaseId
  4. String generateId();