欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用redisRepository和redistemplate操作redis的過程解析

 更新時間:2022年05月30日 09:51:22   作者:march?of?Time  
本文給大家介紹springboot整合redis/分別用redisRepository和redistemplate操作redis,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

導入依賴

菜單大部分情況下不會出現(xiàn)變化,我們可以將其放入Redis 加快加載速度

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- commons-pool2 對象池依賴 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

基本配置

redis:
 timeout: 10000ms # 連接超時時間
 host: 192.168.10.100 # Redis服務器地址
 port: 6379 # Redis服務器端口
 database: 0 # 選擇哪個庫,默認0庫
 lettuce:
  pool:
  max-active: 1024 # 最大連接數(shù),默認 8
  max-wait: 10000ms # 最大連接阻塞等待時間,單位毫秒,默認 -1
  max-idle: 200 # 最大空閑連接,默認 8
  min-idle: 5 # 最小空閑連接,默認 0

使用RedisTemplate訪問redis

RedisConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import
org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis配置類
*
* @author zhoubin
* @since 1.0.0
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory
redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
//為string類型key設置序列器
redisTemplate.setKeySerializer(new StringRedisSerializer());
//為string類型value設置序列器
redisTemplate.setValueSerializer(new
GenericJackson2JsonRedisSerializer());
//為hash類型key設置序列器
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//為hash類型value設置序列器
redisTemplate.setHashValueSerializer(new
GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}

修改菜單方法:
MenuServiceImpl.java

/**
* 通過用戶id獲取菜單列表
*
* @return
*/
@Override
public List<Menu> getMenusByAdminId() {
Integer adminId = ((Admin)
SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
ValueOperations<String, Object> valueOperations =
redisTemplate.opsForValue();
//查詢緩存中是否有數(shù)據(jù)
List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + adminId);
if (CollectionUtils.isEmpty(menus)){
menus = menuMapper.getMenusByAdminId(adminId);
valueOperations.set("menu_"+adminId,menus);
}
return menus;
}

使用Redisrepository訪問redis

需要聲明一配置項用于啟用Repository以及模板

public class RedisConfig {
    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("127.0.0.1", 6567);
        redisStandaloneConfiguration.setPassword("password");//如果有密碼需要通過這個函數(shù)設置密碼
        return new LettuceConnectionFactory(redisStandaloneConfiguration);
    }

@Bean
    public JedisConnectionFactory jedisConnectionFactory(){
        //如果使用jedis作為客戶端也需要聲明該bean 使用與上面的類似
}
@Bean
    public RedisTemplate<?,?> redisTemplate(){
        RedisTemplate<String,Object> template = new RedisTemplate<>();
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
    template.setConnectionFactory(redisConnectionFactory());
    template.setKeySerializer(stringSerializer);
    template.setHashKeySerializer(stringSerializer);
    template.setValueSerializer(stringSerializer);
    template.setHashValueSerializer(jdkSerializationRedisSerializer);
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
    
}
}

實例:

entity

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
@Accessors(chain = true)
@Data
@RedisHash(value="Student",timeToLive = 10)
public class stu implements Serializable {
    public enum Gender{
        MALE,FEMALE
    }
    private String id;
    @Indexed
    private String name;
    private Gender gender;
    //RedisHash注解用于聲明該實體將被存儲與RedisHash中,如果需要使用repository的數(shù)據(jù)訪問形式,這個注解是必須用到的 timetoLive為實體對象在數(shù)據(jù)庫的有效期
    //@Indexed用于標注需要作為查詢條件的屬性
}

寫接口
repository:

@Repository
public interface StuRepository extends CrudRepository<stu,String> {
stu findByName(String name);
//自定義查詢方法,使用了@Indexed的name屬性查詢
}

調(diào)用:

@SpringBootTest
public class RedisApplicationTests {
    @Autowired
    StuRepository stuRepository;

    @Test
    void testSave(){
        stu student = new stu().setId("0002").setName("xiaoming").setGender(stu.Gender.FEMALE);
        stuRepository.save(student);//根據(jù)id更新或者新增記錄
    }
    @Test
    void testFindBy(){
        //使用主鍵查詢
        assert stuRepository.findById("0002").isPresent();
        //根據(jù)自定義方法查詢
        assert stuRepository.findByName("xiaoming") !=null;
    }
}

到此這篇關于springboot使用redisRepository和redistemplate操作redis的過程解析的文章就介紹到這了,更多相關springboot整合redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • SpringBoot+Ajax+redis實現(xiàn)隱藏重要接口地址的方法

    SpringBoot+Ajax+redis實現(xiàn)隱藏重要接口地址的方法

    這篇文章主要介紹了SpringBoot+Ajax+redis實現(xiàn)隱藏重要接口地址,本篇文章主要講訴使用SpringBoot項目配合Ajax和redis實現(xiàn)隱藏重要接口地址,這里我以隱藏秒殺地址為例,需要的朋友可以參考下
    2024-03-03
  • Jpa?Specification如何實現(xiàn)and和or同時使用查詢

    Jpa?Specification如何實現(xiàn)and和or同時使用查詢

    這篇文章主要介紹了Jpa?Specification如何實現(xiàn)and和or同時使用查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java實現(xiàn)基于UDP協(xié)議網(wǎng)絡Socket編程(C/S通信)

    java實現(xiàn)基于UDP協(xié)議網(wǎng)絡Socket編程(C/S通信)

    這篇文章主要介紹了java實現(xiàn)基于UDP協(xié)議網(wǎng)絡Socket編程(C/S通信),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • 詳解JUC 常用4大并發(fā)工具類

    詳解JUC 常用4大并發(fā)工具類

    這篇文章主要介紹了JUC 常用4大并發(fā)工具類的相關資料,幫助大家更好的理解和學習Java 并發(fā)編程,感興趣的朋友可以了解下
    2020-10-10
  • JAVA線程同步實例教程

    JAVA線程同步實例教程

    這篇文章主要介紹了JAVA線程同步實例教程,在Java程序設計中有著非常廣泛的應用,需要的朋友可以參考下
    2014-08-08
  • Map如何根據(jù)key指定條件進行過濾篩選

    Map如何根據(jù)key指定條件進行過濾篩選

    這篇文章主要介紹了Map如何根據(jù)key指定條件進行過濾篩選問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot使用Jackson配置全局時間日期格式

    SpringBoot使用Jackson配置全局時間日期格式

    本文主要介紹了SpringBoot使用Jackson配置全局時間日期格式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • 淺析Java 對象引用和對象本身

    淺析Java 對象引用和對象本身

    這篇文章主要介紹了Java 對象引用和對象本身的相關資料,幫助大家更好的理解和學習Java,感興趣的朋友可以了解下
    2020-08-08
  • Springboot中如何集成Druid

    Springboot中如何集成Druid

    Druid是Java語言中最好的數(shù)據(jù)庫連接池,Druid能夠提供強大的監(jiān)控和擴展功能,本篇主要講解一下?Springboot中如何集成?Druid?,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • JAVA模擬新增順序表及單鏈表

    JAVA模擬新增順序表及單鏈表

    這篇文章主要介紹了JAVA模擬新增順序表及單鏈表,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07

最新評論