如何使用SpEL表達(dá)式實(shí)現(xiàn)動(dòng)態(tài)分表查詢
這篇文章主要介紹了如何使用SpEL表達(dá)式實(shí)現(xiàn)動(dòng)態(tài)分表查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
這里的動(dòng)態(tài)分表查詢并不是動(dòng)態(tài)構(gòu)造sql語句,而是利用SpEL操作同一結(jié)構(gòu)的不同張表。
也可以參考Spring Data Jpa中的章節(jié)http://docs.spring.io/spring-data/jpa/docs/1.11.3.RELEASE/reference/html/#jpa.query.spel-expressions
背景如下:
因?yàn)閿?shù)據(jù)量較大,將數(shù)據(jù)按年份進(jìn)行了分表,表結(jié)構(gòu)都是一致的。例如現(xiàn)在有兩張表分別表示2017/2018年數(shù)據(jù)
表中只有id和name兩個(gè)字段
DROP TABLE IF EXISTS "public"."data_2017"; CREATE TABLE "public"."data_2017" ( "id" int4 NOT NULL, "name" varchar(255) COLLATE "default" ) WITH (OIDS=FALSE)
實(shí)際工作中們需要根據(jù)請求去選擇查詢17年的表還是18年的表。執(zhí)行的sql語句除了表名不一致,其他均一致。
SELECT id,name FROM table
利用JPA實(shí)現(xiàn)
因?yàn)镴PA中實(shí)體與表示一一對應(yīng)的,而實(shí)際查詢的語句又是一樣的,那么如果按照傳統(tǒng)JPA方法,就需要建立N個(gè)Entity,N個(gè)Repository,N個(gè)查詢方法。
現(xiàn)在使用SpELl表達(dá)式可以簡化Entity及Repository中的代碼編寫,相對提高效率。
1、建立一個(gè)抽象實(shí)體
/** * Created by dingshuo on 2017/6/5. */ @MappedSuperclass public class AbstractMappedType { private int id; private String name; @Id @Column(name = "id") @JsonIgnore public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } }
2、建立17/18年表對應(yīng)的實(shí)體,繼承抽象實(shí)體
/** * Created by dingshuo on 2017/6/5. */ @Entity @Table(name = "DATA_2017", schema = "public", catalog = "powermanager") public class Data2017 extends AbstractMappedType { } /** * Created by dingshuo on 2017/6/5. */ @Entity @Table(name = "DATA_2018", schema = "public", catalog = "powermanager") public class Data2018 extends AbstractMappedType { }
3、建立抽象Repository
/** * Created by dingshuo on 2017/6/5. */ @NoRepositoryBean public interface MappedTypeRepository<T extends AbstractMappedType> extends Repository<T, Long> { @Query("select t from #{#entityName} t where t.id = ?1") List<T> findById(int id); @Query("select t from #{#entityName} t ") List<T> findALL(); }
4、建立17年實(shí)體的Repository,繼承抽象Repository
/** * Created by dingshuo on 2017/6/5. */ public interface Data2017Repository extends MappedTypeRepository<Data2017>{ }
5、測試
@RestController public class TestController { @Autowired Data2017Repository repository; @GetMapping(value = "/test") public String test(){ List<Data2017> object=repository.findById(1); List<Data2017> objec1t=repository.findALL(); return "OK"; } }
如上就可以相對簡化的使用JPA查詢結(jié)構(gòu)相同,表名不同的系列表數(shù)據(jù)了。
當(dāng)然,還是得建立N個(gè)Entity和N個(gè)Repository,還是比較麻煩的。。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring實(shí)戰(zhàn)之SpEl語法實(shí)例詳解
- Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語言支持操作示例
- Spring組件開發(fā)模式支持SPEL表達(dá)式
- Spring spel表達(dá)式使用方法示例
- Spring實(shí)戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作示例
- 使用Spring安全表達(dá)式控制系統(tǒng)功能訪問權(quán)限問題
- springtask 的使用方法和 cron 表達(dá)式解析
- SpringMVC請求的路徑變量里面寫正則表達(dá)式的方法
- springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法
- Spring表達(dá)式語言SpEL用法詳解
相關(guān)文章
SpringBoot接入輕量級分布式日志框架(GrayLog)的操作方法
這篇文章主要介紹了SpringBoot接入輕量級分布式日志框架(GrayLog)的方法,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03mybatis plus generator 根據(jù)數(shù)據(jù)庫自動(dòng)生成實(shí)體類的實(shí)現(xiàn)示例
本文主要介紹了mybatis plus generator 根據(jù)數(shù)據(jù)庫自動(dòng)生成實(shí)體類的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09spring通過filter,Interceptor統(tǒng)一處理ResponseBody的返回值操作
這篇文章主要介紹了spring通過filter,Interceptor統(tǒng)一處理ResponseBody的返回值操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Post請求參數(shù)是數(shù)組或者List時(shí)的請求處理方式
這篇文章主要介紹了Post請求參數(shù)是數(shù)組或者List時(shí)的請求處理方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot Redis緩存數(shù)據(jù)實(shí)現(xiàn)解析
這篇文章主要介紹了SpringBoot Redis緩存數(shù)據(jù)實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01LRU算法及Apache?LRUMap源碼實(shí)例解析
這篇文章主要給大家介紹了關(guān)于LRU算法及Apache?LRUMap源碼解析的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11