最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot整合redis進(jìn)行數(shù)據(jù)操作(推薦)

 更新時間:2017年10月13日 16:11:31   作者:meepoGuan  
springboot整合redis比較簡單,并且使用redistemplate可以讓我們更加方便的對數(shù)據(jù)進(jìn)行操作。下面通過本文給大家分享springboot整合redis進(jìn)行數(shù)據(jù)操作的相關(guān)知識,感興趣的朋友一起看看吧

redis是一種常見的nosql,日常開發(fā)中,我們使用它的頻率比較高,因為它的多種數(shù)據(jù)接口,很多場景中我們都可以用到,并且redis對分布式這塊做的非常好。

springboot整合redis比較簡單,并且使用redistemplate可以讓我們更加方便的對數(shù)據(jù)進(jìn)行操作。

1、添加依賴

 <dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-data-redis</artifactId> 
lt;/dependency> 

2、在application.properties中加入相關(guān)配置

spring.redis.database=0 
spring.redis.host=127.0.0.1 
spring.redis.port=6379 
spring.redis.password= 
spring.redis.pool.max-idle=8 
spring.redis.pool.min-idle=0 
spring.redis.pool.max-active=8 
spring.redis.pool.max-wait=-1 
spring.redis.timeout=5000 

3、編寫配置類

import org.springframework.cache.CacheManager; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.redis.cache.RedisCacheManager; 
import org.springframework.data.redis.connection.RedisConnectionFactory; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.core.StringRedisTemplate; 
@Configuration 
@EnableCaching 
public class RedisConfig { 
  @Bean 
  public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) { 
   CacheManager cacheManager = new RedisCacheManager(redisTemplate); 
   return cacheManager; 
  } 
  @Bean 
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { 
   RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); 
   redisTemplate.setConnectionFactory(factory); 
   return redisTemplate; 
  } 
  @Bean 
  public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { 
   StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); 
   stringRedisTemplate.setConnectionFactory(factory); 
   return stringRedisTemplate; 
  } 
} 

這里定義了兩個bean,一個是redisTemplate,另一個是stringRedisTemplate,它們的序列化方式不同,前者默認(rèn)jdk序列方式,后者默認(rèn)string的序列化方式,后者一般專門用于存儲string格式,前者我們可以用來保存對象等,這里我們都配置上,根據(jù)不同業(yè)務(wù)進(jìn)行不同使用。

4、編寫實體類

public class User implements Serializable{ 
 /** 
  * 
  */ 
 private static final long serialVersionUID = 3221700752972709820L; 
 private int id; 
 private String name; 
 private int age; 
 public int getId() { 
  return id; 
 } 
 public void setId(int id) { 
  this.id = id; 
 } 
 public String getName() { 
  return name; 
 } 
 public void setName(String name) { 
  this.name = name; 
 } 
 public int getAge() { 
  return age; 
 } 
 public void setAge(int age) { 
  this.age = age; 
 } 
 public User(int id, String name, int age) { 
  super(); 
  this.id = id; 
  this.name = name; 
  this.age = age; 
 } 
} 

5、編寫測試service

@Service 
public class UserService { 
 @Autowired 
 private StringRedisTemplate stringRedisTemplate; 
 @Autowired 
 private RedisTemplate<String, Object> redisTemplate; 
 public void set(String key, User user) { 
  redisTemplate.opsForValue().set(key, user); 
 } 
 public User get(String key) { 
  return (User) redisTemplate.boundValueOps(key).get(); 
 } 
 public void setCode(String key, String code) { 
  stringRedisTemplate.opsForValue().set(key, code, 60, TimeUnit.SECONDS); 
 } 
 public String getCode(String key) { 
  return stringRedisTemplate.boundValueOps(key).get(); 
 } 
} 

這里我們模擬兩種操作,一種是根據(jù)key存儲user對象,另一種是存儲key value均為string的操作,并且賦予數(shù)據(jù)過期時間,這種操作我們可以用于驗證碼存儲,在setcode方法中,我們存儲了一個有效時長為60s的數(shù)據(jù),當(dāng)60s過后,數(shù)據(jù)會自動銷毀。

6、編寫測試controller訪問

@RestController 
@RequestMapping("rest_redis") 
public class RedisController { 
 @Resource 
 private UserService userService; 
 @GetMapping("set") 
 public void set() { 
  userService.set("key1", new User(1, "meepoguan", 26)); 
 } 
 @GetMapping("get") 
 public String get() { 
  return userService.get("key1").getName(); 
 } 
 @GetMapping("stringset") 
 public void stringset() { 
  userService.setCode("stringkey", "meepoguan_coke"); 
 } 
 @GetMapping("stringget") 
 public String stringget() { 
  return userService.getCode("stringkey"); 
 } 
} 

對service中的方法進(jìn)行測試。

總結(jié)

以上所述是小編給大家介紹的springboot整合redis進(jìn)行數(shù)據(jù)操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限項目實踐

    mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限項目實踐

    本文主要介紹了mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限項目實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java刪除String中指定字符的11種方法匯總

    Java刪除String中指定字符的11種方法匯總

    這篇文章主要給大家介紹了關(guān)于Java刪除String中指定字符的11種方法,在Java中String類提供了許多方法來處理字符串,其中包括刪除指定字符的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Javamelody監(jiān)控不到sql的問題(親測有效)??

    Javamelody監(jiān)控不到sql的問題(親測有效)??

    JavaMelody是用來在QA和實際運行生產(chǎn)環(huán)境中監(jiān)控Java或Java?EE應(yīng)用程序服務(wù)器的一個開源框架,這篇文章主要介紹了Javamelody監(jiān)控不到sql(親測有效)??,需要的朋友可以參考下
    2022-10-10
  • 關(guān)于springboot-starter-undertow和tomcat的區(qū)別說明

    關(guān)于springboot-starter-undertow和tomcat的區(qū)別說明

    這篇文章主要介紹了關(guān)于springboot-starter-undertow和tomcat的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Mybatis-Plus 條件構(gòu)造器 QueryWrapper 的基本用法

    Mybatis-Plus 條件構(gòu)造器 QueryWrapper 的基本用法

    這篇文章主要介紹了Mybatis-Plus - 條件構(gòu)造器 QueryWrapper 的使用,通過實例代碼給大家介紹了查詢示例代碼及實現(xiàn)需求,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法

    java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法

    當(dāng)數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長時間不使用的情況下,該連接會自動回收并失效,就導(dǎo)致客戶端程序報“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常問題,下面給大家分享解決方案,一起看看吧
    2024-03-03
  • java的基本數(shù)據(jù)類型及屬性

    java的基本數(shù)據(jù)類型及屬性

    java的基本數(shù)據(jù)類型及屬性,需要的朋友可以可以參考一下
    2013-03-03
  • 一文帶你搞懂Java單例模式

    一文帶你搞懂Java單例模式

    單例就是單實例的意思,即在系統(tǒng)全局,一個類只創(chuàng)建一個對象,并且在系統(tǒng)全局都可以訪問這個對象而不用重新創(chuàng)建。本文將通過示例為大家詳細(xì)講解Java單例模式的使用,需要的可以參考一下
    2022-11-11
  • Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實例講解

    Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實例講解

    這里我們演示了Servelet驅(qū)動Tomcat來進(jìn)行HTTP下載的方法,接下來就詳細(xì)來看Java Web應(yīng)用程序?qū)崿F(xiàn)基礎(chǔ)的文件下載功能的實例講解
    2016-05-05
  • springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程

    springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程

    這篇文章主要介紹了springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務(wù)教程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04

最新評論

江口县| 海盐县| 昌乐县| 平和县| 八宿县| 林西县| 桐柏县| 临安市| 高唐县| 财经| 福贡县| 离岛区| 宝应县| 华安县| 余庆县| 太原市| 怀宁县| 罗源县| 余姚市| 颍上县| 永清县| 石门县| 蒙城县| 西乌珠穆沁旗| 南投县| 商南县| 班戈县| 临武县| 饶河县| 灵寿县| 子洲县| 方城县| 宁武县| 三河市| 大化| 正定县| 永川市| 正镶白旗| 温宿县| 常山县| 周至县|