在spring data jpa中使用redis的通用list及entity儲存方法
/** * 從redis中獲取物件。注意:未進行haskey檢測 * * @param e * @param redis * @param KEY * @param KEY_LIST * @param INDEX * @return * @version 1.0 * @since Service 1.0 * @date 2014年2月11日 上午10:59:06 */ public static <E extends IdEntity> List<E> getList(Class<E> e, RedisClient redis, String KEY, String KEY_LIST, String INDEX) { List<E> objects = Lists.newArrayList(); for (Long l : ListUtil.NotNullList(redis.getList(KEY_LIST + INDEX))) { if (redis.hasKey(KEY + l)) { objects.add(redis.getEntity(e, KEY + l)); } } return objects; } /** * 在reids中注入快取物件 * * @param objects * @param redis * @param KEY * @param KEY_LIST * @param INDEX * @return * @version 1.0 * @since Service 1.0 * @date 2014年2月11日 上午10:59:50 */ public static <E extends IdEntity> List<E> creatList(List<E> objects, RedisClient redis, String KEY, String KEY_LIST, String INDEX) { List<E> list = ListUtil.NotNullList(objects); for (E e : list) { redis.addList(KEY_LIST + INDEX, e.getId()); redis.saveEntity(KEY, e); } return list; } /** * 獲取一個特定的實體類。如果redis中不存在,則從資料中獲取 * * @param e * @param redis * @param dao * @param KEY * @param id * @return * @version 1.0 * @since Service 1.0 * @date 2014年2月11日 上午11:00:21 */ public static <E extends IdEntity> E getEntity(Class<E> e, RedisClient redis, CrudRepository<E, Long> dao, String KEY, Long id) { if (redis.hasKey(KEY + id)) { return BeanUtil.convertMap(e, redis.getHashOPS().entries(KEY + id)); } else { synchronized (KEY) { if (redis.hasKey(KEY + id)) { return getEntity(e, redis, dao, KEY, id); } else { E entity = dao.findOne(id); if (entity != null) { redis.saveEntity(KEY, entity); } return entity; } } } }
/** * 對RedisTemplate的封裝 * * @author 盼庚 * @version 1.0 * @since Service 1.0 * @date 2014年2月11日 上午11:31:00 * @control */ @Component public class RedisClient { @Autowired private WebApplicationContext context; public void expire(String key, Integer timeout) { getRedis().expire(key, timeout, TimeUnit.MINUTES); } @SuppressWarnings("unchecked") public RedisTemplate<Serializable, Serializable> getRedis() { return (RedisTemplate<Serializable, Serializable>) context.getBean("redisTemplate"); } @SuppressWarnings("unchecked") public <V> RedisTemplate<String, V> getRedis(Class<V> v) { return (RedisTemplate<String, V>) context.getBean("redisTemplate"); } @SuppressWarnings("unchecked") public <V, K> RedisTemplate<K, V> getRedis(Class<K> k, Class<V> v) { return (RedisTemplate<K, V>) context.getBean("redisTemplate"); } public <K, V> HashOperations<K, String, V> getHashOPS(Class<K> k, Class<V> v) { return getRedis(k, v).opsForHash(); } public <T> HashOperations<String, Object, T> getHashOPS(Class<T> T) { return getRedis(Object.class).opsForHash(); } public HashOperations<String, Object, Object> getHashOPS() { return getRedis(Object.class).opsForHash(); } public HashOperations<String, Object, String> getHashOPSByString() { return getRedis(Object.class).opsForHash(); } public HashOperations<String, Object, Long> getHashOPSByLong() { return getRedis(Object.class).opsForHash(); } public <V> ListOperations<String, V> getListOPS(Class<V> v) { return getRedis(v).opsForList(); } public <V> ListOperations<String, Long> getListOPSByLong() { return getRedis(Long.class).opsForList(); } public <V> SetOperations<String, V> getSetOPS(Class<V> v) { return getRedis(v).opsForSet(); } public <V> ValueOperations<String, V> getValueOPS(Class<V> v) { return getRedis(v).opsForValue(); } public ValueOperations<String, Object> getObject(String key) { return getRedis(Object.class).opsForValue(); } public ValueOperations<String, String> getString(String key) { return getRedis(String.class).opsForValue(); } public ValueOperations<String, Long> getLong(String key) { return getRedis(Long.class).opsForValue(); } public <V> ZSetOperations<String, V> getZSetOPS(Class<V> v) { return getRedis(v).opsForZSet(); } public <T extends IdEntity> void saveEntity(String key, T t) { getHashOPS().putAll(key + t.getId(), BeanUtil.convertBean(t)); } public void addList(String key, Long id) { getListOPSByLong().rightPush(key, id); } public List<Long> getList(String key) { return getListOPSByLong().range(key, 0, -1); } public <T extends IdEntity> T getEntity(Class<T> t, String key) { return BeanUtil.convertMap(t, getHashOPS().entries(key)); } public Boolean hasKey(String key) { return getRedis().hasKey(key); }
相關推薦
在spring data jpa中使用redis的通用list及entity儲存方法
/** * 從redis中獲取物件。注意:未進行haskey檢測 * * @param e * @param redis * @param KEY * @param KEY_LIST * @param
Spring Data JPA 中Repository裡的方法查詢引數,需要判斷是否為空的問題
問題: 動態查詢需要判斷引數是否為 null ,在repository裡的那樣的寫法,在網上並沒找到真正適用的寫法。 package com......pubcascade.repository.dao; 簡述: Spring Data JPA雖然大大的簡化了持
Spring data JPA 的分頁功能及複雜語句查詢
測試的資料表 // 實體類 @Entity @Data @AllArgsConstructor @NoArgsConstructor @Table(name="demo_user") public class DemoUser { @Id @Gener
Spring+Data+JPA持久層的使用及注意事項(三)——Failed to load ApplicationContext無法載入配置檔案
Failed to load ApplicationContext這個問題是有很多的,我的檢查過程是這樣:1,檢查web.xml中對於spring的listener的配置是否有問題,因為我這是一鍵生成的,所以主要是看路徑問題,也就是location有沒有配對 <!
Spring Data JPA中的@Query註解使用方式
使用@Query的方式 這種查詢可以宣告在繼承JpaRepository介面方法中,可以在自定義的查詢方法上使用@Query,來指定該方法要執行的自定義的查詢語句,比如: 例子1: @Query("select o from Us
spring data jpa 中@Query註解分頁pageable查詢
持有層程式碼:Repository @Query(value = "select * from user_liveapp where user_Id = :userId ORDER BY ?#{#pageable}", nativeQuery = true) Page&
Spring-Data-JPA中使用Specification實現動態查詢
最近專案技術選型db框架選擇了使用JPA,剛開始時,使用jpa進行一些單表簡單的查詢非常輕鬆,大家寫的不亦樂乎,後來在遇到多條件動態查詢的業務場景時,發現現有的JpaRepository提供的方法和自己寫@Query已經滿足了不了需求,難不成要對所有的條件和欄位進
Spring Data JPA中常用的註解詳解
好吧!今天就來講下Spring Data JAP中的常用註解~~!先附上昨天的實體類程式碼!我們先看看類前面的兩個註解~!@Entity標識這個實體類是一個JPA實體,告訴JPA在程式執行的時候記得生成這個實體類所對應的表~!@Table(name = "自定義的表名")自定
spring data jpa中@Query中的模糊查詢
/** * * @author: WYL * @createTime: 2015年7月3日 上午10:14:31 * @history: * @param keyName 進行匹配的關鍵字
SpringBoot中使用Spring Data Jpa 實現簡單的動態查詢的兩種方法
ppr eat value table 得到 blog .net ride integer 首先謝謝大佬的簡書文章:http://www.jianshu.com/p/45ad65690e33# 這篇文章中講的是spring中使用spring data jpa,使用了xml配
spring boot 中Spring data jpa數據庫表字段命名策略
_id -s ber data 駝峰命名 org body strategy 命名 spring boot 中Spring data jpa命名策略 數據庫,表字段命名是駝峰命名法(UserID),Spring data jpa 自動更新之後是 user_id, 表字段不對
在Spring Boot中使用Spring-data-jpa實現分頁查詢(轉)
分頁查詢 log def inpu database ext identity odin btn 在我們平時的工作中,查詢列表在我們的系統中基本隨處可見,那麽我們如何使用jpa進行多條件查詢以及查詢列表分頁呢?下面我將介紹兩種多條件查詢方式。 1、引入起步依賴
Spring data jpa 調用存儲過程處理返回參數及結果集
align 只需要 數據庫 num pri 調用 column mysq ini 一、環境 1.此隨筆內容基於spring boot整合的spring data jpa項目, 2.數據庫為mysql 5.7.9版本 二、內容 1. 新建存儲過程 pro_query_o
【spring data jpa】jpa中使用count計數方法
class spa spring span pre data 根據 直接 uid spring data jpa中使用count計數方法很簡單 直接在dao層寫方法即可 int countByUidAndTenementId(String parentUid, Strin
Spring Boot中使用Spring-data-jpa訪問資料
一 點睛 只需要通過編寫一個繼承自JpaRepository的介面就能完成資料訪問。 二 實戰 1 新建依賴 <dependencies> <dependency> <groupId>org
基於Springboot技術的部落格系統實踐及應用之三(Spring Data JPA)
本部落格將從JPA簡介、Spring Data JPA用法介紹、Spring Data JPA、Hibernate與SpringBoot整合以及資料持久化實戰四個方面進行詳細描述Spring Data JPA的用法和應用。 1、JA
Spring Boot中的spring-data-jpa
Spring Boot中的spring-data-jpa 簡介 1. pom.xml 2. application.properties 3.實體類 4. repository 5.測試 簡介 JPA:Java Per
Spring Data JPA的Pageable物件,實現對已查詢出list的分頁
Pageable物件 但是有一種情況就是,pageable無法對已查詢出資料的list結果進行分頁,這在實際開發中幾乎不可避免,很多複雜業務,出於簡化開發或者考慮實際的原因,不可能通過一個sql查詢實現需求,肯定會對查詢出的list資料進行過濾,而這時pag
Spring-data-JPA清空OneToMany關係中Many一方的資料
/**父物件*/ class Parent { @OneToMany(mappedBy = "parent") List<Child> children; } /**子物件*/ class Child{ Parent parent; } 在
Spring Boot中使用Spring-data-jpa讓資料訪問更簡單、更優雅
在上一篇Spring中使用JdbcTemplate訪問資料庫 中介紹了一種基本的資料訪問方式,結合構建RESTful API、使用Thymeleaf模板引擎渲染Web檢視的內容就已經可以完成App服務端和Web站點的開發任務了。 然而,在實際開發過程中,對資料庫的操作