映射注解示例
这个例子展示了如何使用 @SelectKey 注解来在插入前读取数据库序列的值:
- @Insert(“insert into table3 (id, name) values(#{nameId}, #{name})”)
- @SelectKey(statement=”call next value for TestSequence”, keyProperty=”nameId”, before=true, resultType=int.class)
- int insertTable3(Name name);
这个例子展示了如何使用 @SelectKey 注解来在插入后读取数据库自增列的值:
- @Insert(“insert into table2 (name) values(#{name})”)
- @SelectKey(statement=”call identity()”, keyProperty=”nameId”, before=false, resultType=int.class)
- int insertTable2(Name name);
这个例子展示了如何使用 @Flush 注解来调用 SqlSession#flushStatements():
- @Flush
- List<BatchResult> flush();
这些例子展示了如何通过指定 @Result 的 id 属性来命名结果集:
- @Results(id = “userResult”, value = {
- @Result(property = “id”, column = “uid”, id = true),
- @Result(property = “firstName”, column = “first_name”),
- @Result(property = “lastName”, column = “last_name”)
- })
- @Select(“select * from users where id = #{id}”)
- User getUserById(Integer id);
@Results(id = “companyResults”)
@ConstructorArgs({
@Arg(column = “cid”, javaType = Integer.class, id = true),
@Arg(column = “name”, javaType = String.class)
})
@Select(“select * from company where id = #{id}”)
Company getCompanyById(Integer id);
这个例子展示了如何使用单个参数的 @SqlProvider 注解:
- @SelectProvider(type = UserSqlBuilder.class, method = “buildGetUsersByName”)
- List<User> getUsersByName(String name);
class UserSqlBuilder {
public static String buildGetUsersByName(final String name) {
return new SQL(){{
SELECT(“*”);
FROM(“users”);
if (name != null) {
WHERE(“name like #{value} || ‘%’”);
}
ORDER_BY(“id”);
}}.toString();
}
}
这个例子展示了如何使用多个参数的 @SqlProvider 注解:
- @SelectProvider(type = UserSqlBuilder.class, method = “buildGetUsersByName”)
- List<User> getUsersByName(
- @Param(“name”) String name, @Param(“orderByColumn”) String orderByColumn);
class UserSqlBuilder {
// 如果不使用 @Param,就应该定义与 mapper 方法相同的参数
public static String buildGetUsersByName(
final String name, final String orderByColumn) {
return new SQL(){{
SELECT(“*”);
FROM(“users”);
WHERE(“name like #{name} || ‘%’”);
ORDER_BY(orderByColumn);
}}.toString();
}
// 如果使用 @Param,就可以只定义需要使用的参数
public static String buildGetUsersByName(@Param(“orderByColumn”) final String orderByColumn) {
return new SQL(){{
SELECT(“*”);
FROM(“users”);
WHERE(“name like #{name} || ‘%’”);
ORDER_BY(orderByColumn);
}}.toString();
}
}
以下例子展示了 ProviderMethodResolver(3.5.1 后可用)的默认实现使用方法:
- @SelectProvider(UserSqlProvider.class)
- List<User> getUsersByName(String name);
// 在你的 provider 类中实现 ProviderMethodResolver 接口
class UserSqlProvider implements ProviderMethodResolver {
// 默认实现中,会将映射器方法的调用解析到实现的同名方法上
public static String getUsersByName(final String name) {
return new SQL(){{
SELECT(“*”);
FROM(“users”);
if (name != null) {
WHERE(“name like #{value} || ‘%’”);
}
ORDER_BY(“id”);
}}.toString();
}
}
This example shows usage the databaseId attribute on the statement annotation(Available since 3.5.5):
- @Select(value = “SELECT SYS_GUID() FROM dual”, databaseId = “oracle”) // Use this statement if DatabaseIdProvider provide “oracle”
- @Select(value = “SELECT uuid_generate_v4()”, databaseId = “postgres”) // Use this statement if DatabaseIdProvider provide “postgres”
- @Select(“SELECT RANDOM_UUID()”) // Use this statement if the DatabaseIdProvider not configured or not matches databaseId
- String generateId();