HasorDB 注解化Mapper

2022-01-10 11:50 更新

如果對于編寫大量的 ?Mapper ?文件比較反感可以采用注解方式,這種方式對于一些 ?簡單的查詢? 和可以使用 ?快速規(guī)則? 的查詢來說,既可以減少復雜度也可以更靈活。

由于注解化 ?Mapper ?最大的特點就是無需 ?Mapper ?文件,SQL 配置會集中在 Mapper 接口的注解上。因此作為 注解化 Mapper 需要標記 ?@SimpleMapper? 注解。

??提示??
請注意 ?@SimpleMapper? 和 ?@RefMapper? 兩個注解不能同時使用,但它們都能夠正確處理注解化 Mapper。 不過仍然要注意的是請避免 注解化 Mapper 和 Mapper 文件的定義沖突,遇到?jīng)_突 HasorDB 也不能正確處理好它們。

@Query注解?

用于配置一個 ?select ?查詢,例如下列 Mapper 和接口可以簡化成一個注解化 Mapper 方法。

<select id="queryUser">
select * from `test_user`
where
<if test="age != null">
and age = #{age}
</if>
</select>

下面使用 ?快速'或'條件? 簡化 ?if? 之后在利用 ?@Query ?注解化的結(jié)果。

@SimpleMapper()
public interface UserMapper {
@Query(value = "select * from `test_user` where @{and, age = :age }",
resultType = TestUser.class)
List<TestUser> queryUser(@Param("age") int age);
}

?@Query? 注解擁有很多屬性,這些屬性和 ?<select>? 標簽是相對應的

屬性名 描述
value 獨有 必選,配置 SQL 查詢
xml 獨有 可選,value 中的內(nèi)容如果使用了 動態(tài) SQL 中定義的動態(tài)SQL標簽,那么需要設(shè)置為 true。表示這是 XML格式的片段。
statementType 可選,STATEMENT、PREPARED、CALLABLE 對應了 StatementPreparedStatement 或 CallableStatement 中的一種。默認值為 PREPARED
timeout 可選,當配置的值大于 0 時會被設(shè)置到 Statement.setQueryTimeout,用于表示查詢最長等待的超時時間。默認值是 -1
resultMap 可選,對于映射配置的引用。select 標簽可以使用 resultMap 和 resultType 其中的一種,不應該同時使用它們。如果沒有配置將會按照 map 來處理
resultType 可選,將返回的預期類型的完全限定類名或別名。注意,在集合的情況下,這應該是集合包含的類型,而不是集合本身的類型,不應該同時使用resultMap 和 resultType。
fetchSize 可選,當配置的值大于 0 時會被設(shè)置到 Statement.setQueryTimeout,用于表示查詢最長等待的超時時間。默認值是 -1
resultSetType 可選,FORWARD_ONLY、SCROLL_INSENSITIVE、SCROLL_SENSITIVE 和 DEFAULT 其中的一種。默認值是 DEFAULT 相當于未設(shè)置。
multipleResult 可選,FIRSTLAST、ALL 用于處理多結(jié)果集的情況。它們對應的行為是 保留第一個結(jié)果集、保留最后一個結(jié)果集、全部保留。默認配置是 LAST

@Insert注解?

?@Insert? 注解的功效和 ?<insert>? 標簽相同,下面是示例:

<insert id="insertUser">
insert into `test_user` (
`id`, `name`, `age`, `create_time`
) values (
#{id}, #{name}, #{age}, #{createTime}
)
</insert>

使用 ?@Insert? 注解化方式。

@SimpleMapper()
public interface UserMapper {
@Insert(value = "insert into `test_user` ( `id`, `name`, `age`, `create_time` ) "+
" values ( #{id}, #{name}, #{age}, #{createTime})")
int queryUser(TestUser user);
}

?@Insert? 注解擁有很多屬性,這些屬性和 ?<insert>? 標簽是相對應的

屬性名 描述
value 獨有 必選,配置 SQL 查詢
xml 獨有 可選,value 中的內(nèi)容如果使用了 動態(tài) SQL 中定義的動態(tài)SQL標簽,那么需要設(shè)置為 true。表示這是 XML格式的片段。
statementType 可選,STATEMENT、PREPARED、CALLABLE 對應了 StatementPreparedStatement 或 CallableStatement 中的一種。默認值為 PREPARED
timeout 可選,當配置的值大于 0 時會被設(shè)置到 Statement.setQueryTimeout,用于表示查詢最長等待的超時時間。默認值是 -1

@Update注解?

?@Update? 注解的功效和 ?<update>? 標簽相同,下面是示例:

<update id="updateAge">
update `test_user` set age = #{age} where id = #{id}
</update>

使用 ?@Update? 注解化方式。

@SimpleMapper()
public interface UserMapper {
@Update(value = "update `test_user` set age = #{age} where id = #{id}")
int updateAge(@Param("age") int age, @Param("id") int id);
}

?@Update? 注解擁有很多屬性,這些屬性和 ?<update>? 標簽是相對應的

屬性名 描述
value 獨有 必選,配置 SQL 查詢
xml 獨有 可選,value 中的內(nèi)容如果使用了 動態(tài) SQL 中定義的動態(tài)SQL標簽,那么需要設(shè)置為 true。表示這是 XML格式的片段。
statementType 可選,STATEMENTPREPARED、CALLABLE 對應了 StatementPreparedStatement 或 CallableStatement 中的一種。默認值為 PREPARED
timeout 可選,當配置的值大于 0 時會被設(shè)置到 Statement.setQueryTimeout,用于表示查詢最長等待的超時時間。默認值是 -1

@Delete注解?

?@Delete? 注解的功效和 ?<delete>? 標簽相同,下面是示例:

<delete id="deleteById">
delete from `test_user` where id = #{id}
</delete>

使用 ?@Delete? 注解化方式。

@SimpleMapper()
public interface UserMapper {
@Update(value = "delete from `test_user` where id = #{id}")
int updateAge(@Param("id") int id);
}

?@Delete? 注解擁有很多屬性,這些屬性和? <delete>? 標簽是相對應的

屬性名 描述
value 獨有 必選,配置 SQL 查詢
xml 獨有 可選,value 中的內(nèi)容如果使用了 動態(tài) SQL 中定義的動態(tài)SQL標簽,那么需要設(shè)置為 true。表示這是 XML格式的片段。
statementType 可選,STATEMENT、PREPARED、CALLABLE 對應了 StatementPreparedStatement 或 CallableStatement 中的一種。默認值為 PREPARED
timeout 可選,當配置的值大于 0 時會被設(shè)置到 Statement.setQueryTimeout,用于表示查詢最長等待的超時時間。默認值是 -1

@Param注解?

?@Param ?注解只能標記在方法的參數(shù)上,用來在拼接動態(tài) SQL 時候引用到對應的屬性。

比較常見的方式如下,?#{id}? 對應? @Param("id")?

@Update(value = "delete from `test_user` where id = #{id}")
int updateAge(@Param("id") int id);

如果方法入?yún)⒂星覂H有一個對象,那么這個對象無需標記 ?@Param? 注解,它的每一個屬性都會被識別。例如:

public class TestUser {
private Integer id;
private String name;
private Integer age;
private Date createTime;

// getters and setters omitted
}

@Insert(value = "insert into `test_user` "+
"values ( #{id}, #{name}, #{age}, #{createTime})")
int queryUser(TestUser user);

如果參數(shù)沒有標記 ?@Param? 注解那么會以參數(shù)的所在順序 前面加上固定的 ?arg ?來替代,例如:

@Update(value = "update `test_user` set age = #{arg0} where id = #{arg1}")
int updateAge(int age, int id);


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號