SpringBoot 整合redis ,使用配置文件设置参数方式
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>
application.properties
springboot.redis.pool.max-idle=10
springboot.redis.pool.min-idle=5
springboot.redis.pool.max-totla=20
springboot.redis.hostName=127.0.0.1
springboot.redis.port=6379
springboot.redis.Password=123456
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);
}
}
RedisConfig
package com.alan.SpringBootDataRedis.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 对象。在该对象中完成一些连接池配置
* @ConfigurationProperties(prefix = "springboot.redis")
* 作用:会将前缀相同的部分内容创建为一个实体对象
* @param
* @return {@link JedisPoolConfig}
* @throws
* @user Alan_liu
* @date 2021/5/5 22:18
*/
@Bean
@ConfigurationProperties(prefix = "springboot.redis.pool")
public JedisPoolConfig jedispoolConfig(){
JedisPoolConfig config=new JedisPoolConfig();
//最大空闲数
//最大空闲数
//最大链接数
return config;
}
/***
* 2:创建 JedisConnectionFactory:配置redis链接信息
*
* @param null
* @return {@link }
* @throws
* @user Alan_liu
* @date 2021/5/5 22:18
*/
@Bean
@ConfigurationProperties(prefix = "springboot.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
JedisConnectionFactory factory=new JedisConnectionFactory();
//关联连接池的配置对象
//配置链接redis的信息
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;
}
}
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);
}
}
使用案例
Users
package com.alan.SpringBootDataRedis.pojo;
/*
* Todo
*
* @author: Alan_liu
* @date 2021/5/25 21:10
*/
public class Users {
private Integer id;
private String name ;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
RedisTest
package com.alan.SpringBootDataRedis;
import com.alan.SpringBootDataRedis.pojo.Users;
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.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.sound.midi.Soundbank;
import javax.swing.plaf.PanelUI;
/*
* 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);
}
/**
*
* 添加一个user 对象
* @param null
* @return {@link }
* @throws
* @user Alan_liu
* @date 2021/5/25 21:12
*/
@Test
public void testSetUsers(){
Users users=new Users();
users.setId(1);
users.setName("张三丰");
users.setAge(120);
//设置序列化操作
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("users",users);
}
/***
*
* 从rdeis 中却users 对象
* @param
* @return {@link void}
* @throws
* @user Alan_liu
* @date 2021/5/25 21:21
*/
@Test
public void testGetUsers(){
//重新设置序列化器
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
Users users=(Users) this.redisTemplate.opsForValue().get("users");
System.out.println(users);
}
/**
* 基于Json 格式存储User对象
*
* @param null
* @return {@link }
* @throws
* @user Alan_liu
* @date 2021/5/25 21:29
*/
@Test
public void testSetUsersUseJson(){
Users users=new Users();
users.setId(1);
users.setName("张三丰");
users.setAge(120);
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Users>(Users.class));
this.redisTemplate.opsForValue().set("usersJson",users);
}
/***
*
* 基于Json 格式取出User对象
* @param
* @return {@link void}
* @throws
* @user Alan_liu
* @date 2021/5/25 21:34
*/
@Test
public void testGetUsersUseJson(){
this.redisTemplate.setKeySerializer(new Jackson2JsonRedisSerializer<Users>(Users.class));
Users users=(Users) this.redisTemplate.opsForValue().get("usersJson");
System.out.println(users);
}
}
StringRedisTemplate常用方法:
这里给大家一个链接,StringRedisTemplate常用方法都在里面我就不一一列出来了
RedisTemplate常用方法
RedisTemplate中定义了对5种数据结构操作
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
转载请标注出处!