版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hundan_520520/article/details/81742661
项目文件截图
pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhihua</groupId>
<artifactId>SpringBoot_Redis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot_Redis Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</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 主要是简单配置一个redis的地址
spring.redis.host=localhost
User.java 模拟bean
package com.zhihua.redis.bean;
import java.io.Serializable;
/**
* 要支持序列化,方便redis存储
* @author caizh
*
*/
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public User() {
}
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = 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 "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
MyRedisConfig.java 自定义配置redisTemplate的序列化方式
package com.zhihua.redis.config;
import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
@Configuration
public class MyRedisConfig {
/**
* 自定义redisTemplate配置
* @return
*/
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory
)throws UnknownHostException{
RedisTemplate<Object, Object> template = new RedisTemplate<Object,Object>();
template.setConnectionFactory(redisConnectionFactory);
// 自定义序列化方式
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
SpringbootRedisApplication.java 启动类
package com.zhihua;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 1、配置RedisProperties相关属性
* 2、可以直接使用
* StringRedisTemplate:常用的String操作
* RedisTemplate<Object, Object>:
* xxxxTemplate:
* JdbcTemplate,RestTemplate
*
*/
@SpringBootApplication
public class SpringbootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRedisApplication.class, args);
}
}
SpringbootRedisApplicationTests.java 测试类
package com.zhihua.redis;
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.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import com.zhihua.redis.bean.User;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
@Autowired
StringRedisTemplate stringRedisTemplate; // k-v都是字符串
@Autowired
RedisTemplate<Object, Object> redisTemplate;//
/**
* 字符串(strings),stringRedisTemplate.opsForValue()
* 散列(hashes),stringRedisTemplate.opsForHash()
* 列表(lists),stringRedisTemplate.opsForList()
* 集合(sets),stringRedisTemplate.opsForSet()
* 有序集合(sorted sets)stringRedisTemplate.opsForZSet()
*/
@Test
public void testStringRedisTemplate() {
//stringRedisTemplate.opsForValue().set("msg", "这是redis");
String msg = stringRedisTemplate.opsForValue().get("msg");
System.out.println(msg);
}
@Test
public void testRedisTemplate() {
redisTemplate.opsForValue().set("user",new User(1,"zhangsan",18));
User user = (User) redisTemplate.opsForValue().get("user");
System.out.println(user);
}
}