前面 写了整合数据库的代码
这篇文章 写的是如何整合redis 以及redis 的介绍
redis 是一个开源的非常好用的缓存库,key-value 键值对存取
redis有五种数据存储 用的比较频繁的是 string map list 这三种存储
先不说redis 如何使用 先来谈谈 redis 如何和spring boot 整合
1.jar包依赖 下面是我的pom 文件
`<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
com.example
demo2
0.0.1-SNAPSHOT
demo-1
Demo project for Spring Boot
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 数据连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<!-- boot 配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 配置redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
` 2.yml文件配置
jedis :
pool :
password :123
host : 192.168.32.5
port : 6379
timeout : 30000
db : 0
config :
maxTotal: 1000
maxIdle: 200
maxWaitMillis : 2000
3.spring 工厂 自动注入redis
package online.example.development.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
@Value("${jedis.pool.host}")
private String host;
@Value("${jedis.pool.password}")
private String password;
@Value("${jedis.pool.port}")
private int port;
@Value("${jedis.pool.timeout}")
private int timeout;
@Value("${jedis.pool.db}")
private int db;
@Bean(name= "jedis.pool")
@Autowired
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config
) {
return new JedisPool(config, host, port, db) ;
}
@Bean(name= "jedis.pool.config")
@Autowired
public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
@Value("${jedis.pool.config.maxIdle}")int maxIdle,
@Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
return config;
}
@Bean(name="jedis.pool.redisConnectionFactory")
@Autowired
public JedisConnectionFactory jedisCon(@Qualifier("jedis.pool.config")JedisPoolConfig config) {
JedisConnectionFactory factory=new JedisConnectionFactory();
factory.setPoolConfig(config);
factory.setHostName(host);
factory.setTimeout(timeout);
factory.setPort(port);
factory.setPassword(password);
factory.setDatabase(db);
return factory;
}
/**
* 设置数据存入 redis 的序列化方式
*
* @param redisTemplate
* @param factory
*/
@Bean(name="redisTemplate")
@Autowired
public RedisTemplate initDomainRedisTemplate(@Qualifier("jedis.pool.redisConnectionFactory") RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
4.代码整合完毕 (demo) 运用spring 工厂得注意 方法不能私有化
package com.example.demo.controller;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiImplicitParam;
//import io.swagger.annotations.ApiImplicitParams;
//import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.FlatOpenRecordService;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/demo")
public class demo {
@Autowired
private FlatOpenRecordService service;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@RequestMapping("/getFlatOpenRecordList.do")
@ResponseBody
public Map<String, Object> getFlatOpenRecordList(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "20") Integer rows,
Integer id) {
Map<String, Object> map=new HashMap<String,Object>();
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
opsForValue.set("demo", "spring CESHI");
return map;
}
}
5.最后连接redis 看是否存入进去