/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

SpringBoot 整合redis


SpringBoot 整合 redis 之Pom配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.Alan</groupId>
    <artifactId>SpringBootDataRedis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootDataRedis</name>
    <description>SpringBootDataRedis</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!--springboot的启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--thymeleaf 的启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--springboot data redis的启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--对redis 客户端的 jedis 包的引入。高版本 已经不是自动引入该包。需要手动在pom文件中增加该包配置信息-->
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


设置 Java调用redis 的配置信息


RedisConfig.java

package com.alan.SpringBootDataRedis.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/*
 * Todo
 *  完成对redis 的整合一些配置
 * @author: Alan_liu
 * @date 2021/5/5 21:54
 */
@Configuration
public class RedisConfig {
 /***
  * 1:创建 JedispoolConfig 对象。在该对象中完成一些连接池配置
  *
   * @param
  * @return {@link JedisPoolConfig}
  * @throws
  * @user Alan_liu
  * @date 2021/5/5 22:18
  */
  @Bean
  public JedisPoolConfig jedispoolConfig(){
      JedisPoolConfig config=new JedisPoolConfig();
      //最大空闲数
      config.setMaxIdle(10);
      //最大空闲数
      config.setMaxIdle(5);
      //最大链接数
      config.setMaxTotal(20);
      return config;
  }

   /***
    *  2:创建 JedisConnectionFactory:配置redis链接信息
    *
     * @param null
    * @return {@link }
    * @throws
    * @user Alan_liu
    * @date 2021/5/5 22:18
    */
  @Bean
  public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
   JedisConnectionFactory factory=new JedisConnectionFactory();
   //关联连接池的配置对象
   factory.setPoolConfig(config);
   //配置链接redis的信息
   factory.setHostName("127.0.0.1");//主机地址
   factory.setPort(6379);//端口
   factory.setPassword("123456");//设置密码
   // factory.setDatabase(0);//访问操作redis数据库。以0到13的序号为下标。如果不设置,默认为第0个库
   return  factory;
  }
  /***
   * 3:创建redisTemplate:用于执行redis操作的方法
   *
    * @param
   * @return {@link RedisTemplate< String, Object>}
   * @throws
   * @user Alan_liu
   * @date 2021/5/5 22:38
   */
  @Bean
  public RedisTemplate<String,Object> redisTemplate(  JedisConnectionFactory factory){
      RedisTemplate<String ,Object> template=new RedisTemplate<>();
      //关联
      template.setConnectionFactory(factory);
      //为key设置序列号器
      template.setKeySerializer(new StringRedisSerializer());
      //为value 设置序列号器
      template.setValueSerializer(new StringRedisSerializer());

      return  template;
  }

}


SpringBootDataRedisApplication

package com.alan.SpringBootDataRedis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDataRedisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDataRedisApplication.class, args);
    }

}


SpringBootDataRedisApplicationTests

package com.alan.SpringBootDataRedis;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDataRedisApplicationTests {

    @Test
    void contextLoads() {
    }

}


测试调用情况

RedisTest

package com.alan.SpringBootDataRedis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/*
 *  Springboot redis 测试类
 *
 * @author: Alan_liu
 * @date 2021/5/23 22:01
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=SpringBootDataRedisApplication.class)
public class RedisTest {

  @Autowired
  private  RedisTemplate<String,Object> redisTemplate;

  // 添加1个字符
 @Test
  public  void  testSet(){
     this.redisTemplate.opsForValue().set("SpringBootRedisTestKey","AlanLiu");
 }
 //取出一个字符串
 @Test
 public void testGet(){
    String value= (String) this.redisTemplate.opsForValue().get("SpringBootRedisTestKey");
    System.out.println(value);
 }

}


执行测试结果:

2343de90b5aa6c8fa45720ff84d17eb

4d23ff8da95dd2a616c9311148d2f6d

c440d15ce50f9b4f4abadfcceb5803d

posted @ 2021-05-23 23:28  一品堂.技术学习笔记  阅读(60)  评论(0编辑  收藏  举报