jpa原生query_JPA执行原生SQL语句
前言
作業(yè)的項目終于告一段落了暫時,這周繼續(xù)進行日志系統(tǒng)的編寫,只可惜這周開始練科三了,一開始是全天練車,導(dǎo)致每天寫代碼的時間減少了好多,后來時間進行了調(diào)整(早上四點半到七點半,晚上五點到七點多),也算有挺多的時間了。
(駕校不正規(guī),練車兩行淚)
需求
由于日志系統(tǒng)每天的數(shù)據(jù)量較大,隨著時間的推移,有很多之前的數(shù)據(jù)可能就沒用了,但是在數(shù)據(jù)表里放著就占資源,于是要刪除三個月之前的數(shù)據(jù),就需要自己寫原生SQL語句了。
Jdbc
按照教程給的源碼進行了功能的編寫,由于沒寫單元測試,所以雖說功能實現(xiàn)了,但是還是有很大的缺陷的,后來問了一下潘老師,老師推薦使用的是JPA
@Override
public List getData() {
/\* 定義實現(xiàn)了RowCallbackHandler接口的對象\*/
RowCallbackHandler rowCallbackHandler \= new RowCallbackHandler() {
/\*\*
\* 該方法用于執(zhí)行jdbcTemplate.query后的回調(diào),每行數(shù)據(jù)回調(diào)1次。比如DayLog表中有兩行數(shù)據(jù),則回調(diào)此方法兩次。
\* @param resultSet 查詢結(jié)果,每次一行
\* @throws SQLException 查詢出錯時,將拋出此異常,暫時不處理。
\*/
@Override
public void processRow(ResultSet resultSet) throws SQLException {
DayLog dayLog \= new DayLog();
dayLog.setId(resultSet.getLong("id"));
dayLog.setDay(resultSet.getDate("day"));
dayLogs.add(dayLog);
logger.info(resultSet.toString());
}
};
String query \= "select \* from day\_log where \`day\` <= curdate() - interval 3 month";
jdbcTemplate.query(query,rowCallbackHandler);
return dayLogs;
}
@Override
public boolean delete()
{
String sql \= String.format("delete from day\_log where \`day\` <= curdate() - interval 3 month");
this.jdbcTemplate.update(sql);
return true;
}
}
JPA
后來找了好多博客,看了一下它的用法,發(fā)現(xiàn)還是挺好用的:
public interface LogRepository extends JpaRepository, JpaSpecificationExecutor {
/**
* 根據(jù)月份查找所有記錄
*
* @return
*/
@Query(value = "SELECT * FROM `log` where `timestamp` <= curdate() - interval 3 month",nativeQuery = true)
List getLogOfThreeMonth();
/**
* 根據(jù)月份刪除所有記錄
* @return
*/
@Transactional(rollbackFor = Exception.class)
@Modifying
@Query(value = "delete from log where `timestamp` <= curdate() - interval 3 month",nativeQuery = true)
void deleteLogOfThreeMonth();
}
@Query
JPA執(zhí)行原生SQL語句,首先要讓倉庫繼承 JpaRepository ,然后加@Query注解,value表示要執(zhí)行的語句,對于nativeQuery = true ,有的博客是這樣解釋的:
有nativeQuery = true時,是可以執(zhí)行原生sql語句,所謂原生sql,也就是說這段sql拷貝到數(shù)據(jù)庫中,然后把參數(shù)值給一下就能運行了
沒有nativeQuery = true時,就不是原生sql,而其中的select * from xxx中xxx也不是數(shù)據(jù)庫對應(yīng)的真正的表名,而是對應(yīng)的實體名,并且sql中的字段名也不是數(shù)據(jù)庫中真正的字段名,而是實體的字段名。
@Modifying
對于該注解的解釋,官方給出的說明是這樣的:
As the queries themselves are tied to the Java method that executes them, you can actually bind them directly by using the Spring Data JPA @Query annotation
rather than annotating them to the domain class.
You can modify queries that only need parameter binding by annotating the query method with @Modifying
The @Modifying annotation is only relevant in combination with the @Query annotation. Derived query methods or custom methods do not require this Annotation.
Doing so triggers the query annotated to the method as an updating query instead of a selecting one.
由于查詢本身綁定到執(zhí)行它們的Java方法,實際上可以通過使用Spring Data JPA@Query注釋直接綁定它們,而不是將它們注釋到域類。
通過使用@Modifying注釋查詢方法,可以修改只需要參數(shù)綁定的查詢
@Modifying注釋只與@Query注釋結(jié)合使用。派生查詢方法或自定義方法不需要此批注。
這樣做將觸發(fā)作為更新查詢(更新、插入、刪除)而不是選擇查詢注釋到方法的查詢。
也就是說,當(dāng)我們要通過自已寫的更新、插入、刪除SQL語句來實現(xiàn)更新、插入、刪除操作時,至少需要用兩個步驟:
1.@Query來注入我們自定義的sql;
2.使用@Modifying來標注是一個更新類的自定義語句。
@Transactional
官方給出的說明是:.
By default, CRUD methods on repository instances are transactional. For read operations, the transaction configuration readOnly flag is set to true. All others are configured with a plain @Transactional so that default transaction configuration applies. For details, see JavaDoc of SimpleJpaRepository. If you need to tweak transaction configuration for one of the methods declared in a repository, redeclare the method in your repository interface, as follows:
_Example. Custom transaction configuration for CRUD_
默認情況下,存儲庫實例上的CRUD方法是事務(wù)性的。對于讀取操作,事務(wù)配置readOnly標志設(shè)置為true。所有其他的都配置了一個普通的@Transactional,以便應(yīng)用默認的事務(wù)配置。有關(guān)詳細信息,請參見SimpleJpaRepository的JavaDoc。如果需要調(diào)整存儲庫中聲明的方法之一的事務(wù)配置,請在存儲庫界面中重新聲明該方法,如下所示:
例子。CRUD的自定義事務(wù)配置
對于更新查詢(更新(UPDATE)、刪除( DELETE))來說,都需要加@Transactional注解。
疑惑及解決
之前Repository類繼承的都是CrudRepository這個類,以實現(xiàn)save,delete等方法,但是為什么我繼承JpaRepository不影響save等操作呢,進入JpaRepository這個類,然后發(fā)現(xiàn)它也是繼承的其他的類:
public interface JpaRepository extends PagingAndSortingRepository, QueryByExampleExecutor {
他繼承的這個類,繼承的就是CrudRepository:
public interface PagingAndSortingRepository extends CrudRepository {
這也解決了我另一個疑惑,為什么有的類繼承的 PagingAndSortingRepository對save等方法也沒有影響,原來他也是繼承的。
總結(jié)
有些東西看起來復(fù)雜,寫起來還是挺順手的,對于SQL這個東西,要了解的還是挺多的,畢竟現(xiàn)在用的語句還是查出來的。
總結(jié)
以上是生活随笔為你收集整理的jpa原生query_JPA执行原生SQL语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: websocket网络层详解_【技术分享
- 下一篇: centos7 mysql数据库安装和配