1、导入Redis相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置application.yml
server:
port: 3060
spring:
redis:
host: 127.0.0.1
port: 6379
password: *********
3、配置config
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class ConfigRedis {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 序列化key
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
// 序列化hash
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
// 连接redis数据库
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
4、UserPojo
package com.redis.start.pojo;
public class UserPojo {
private String id;
private String name;
private String pwd;
public UserPojo() {
super();
}
@Override
public String toString() {
return "UserPojo{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
public UserPojo(String id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
5、Controller
import com.redis.start.pojo.UserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/redis")
@CrossOrigin
@ResponseBody
public class Controller {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/object")
public String putObject(){
UserPojo user = new UserPojo("001", "zhangSan","123456");
// 序列化对象
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(UserPojo.class));
this.redisTemplate.opsForValue().set("cookie@001", user,30, TimeUnit.SECONDS);
System.out.print("/Object:");
System.out.println(this.redisTemplate.opsForValue().get("cookie@001").toString());
return "1";
}
@GetMapping("/list")
public String putList(){
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(UserPojo.class));
this.redisTemplate.opsForList().leftPush("cookie@001", new UserPojo("001", "zhangSan","123456"));
System.out.print("/List:");
System.out.println(this.redisTemplate.opsForList().range("cookie@001", 0, -1));
return "1";
}
@GetMapping(value = "/key")
public String putKey(String key, String value){
this.redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS);
System.out.print("/Key:");
System.out.println(this.redisTemplate.opsForValue().get(key));
return "1";
}
@GetMapping("/map")
public String putMap(){
Map<String, String> myMap = new HashMap<>();
myMap.put("name", "zhangSan");
myMap.put("age", "20");
this.redisTemplate.opsForHash().putAll("map", myMap);
System.out.print("/Map:");
System.out.println(this.redisTemplate.opsForHash().get("map", "name"));
return "1";
}
}
6、MyRedisApplicationStart启动类
package com.redis.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyRedisApplicationStart {
public static void main(String[] args) {
SpringApplication.run(MyRedisApplicationStart.class,args);
}
}
7、启动测试
成功