首先需要下载redis https://redis.io/
这里我只是简单搭建,在本机window上弄的,就不发文件了
首先搭建好springboot,然后开始搭建redis(这里使用最简单的redis搭建方式)
第一步:引入redis 官方指定的jedis jar包
-
<dependency>
-
<groupId>redis.clients
</groupId>
-
<artifactId>jedis
</artifactId>
-
<version>2.8.2
</version>
-
</dependency>
第二步:在application.properties文件下加入redis配置属性
jedis.host = 127.0.0.1
jedis.port = 6379
jedis.maxTotal = 101
jedis.maxIdle = 10
jedis.maxWaitMillis = 100000
第三步:添加jedis属性类,配置类,客户端类
jedis属性类
-
package com.java.Olym.redis;
-
-
import org.springframework.boot.context.properties.ConfigurationProperties;
-
-
@ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)
-
public
class JedisProperties {
-
-
public
static
final String JEDIS_PREFIX =
"jedis";
-
-
private String host;
-
-
private
int port;
-
-
private
int maxTotal;
-
-
private
int maxIdle;
-
-
private
int maxWaitMillis;
-
-
public String getHost() {
-
return host;
-
}
-
-
public void setHost(String host) {
-
this.host = host;
-
}
-
-
public int getPort() {
-
return port;
-
}
-
-
public void setPort(int port) {
-
this.port = port;
-
}
-
-
public int getMaxTotal() {
-
return maxTotal;
-
}
-
-
public void setMaxTotal(int maxTotal) {
-
this.maxTotal = maxTotal;
-
}
-
-
public int getMaxIdle() {
-
return maxIdle;
-
}
-
-
public void setMaxIdle(int maxIdle) {
-
this.maxIdle = maxIdle;
-
}
-
-
public int getMaxWaitMillis() {
-
return maxWaitMillis;
-
}
-
-
public void setMaxWaitMillis(int maxWaitMillis) {
-
this.maxWaitMillis = maxWaitMillis;
-
}
-
-
}
jedis自动配置类
-
package com.java.Olym.redis;
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.beans.factory.annotation.Qualifier;
-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
-
import redis.clients.jedis.JedisPool;
-
import redis.clients.jedis.JedisPoolConfig;
-
-
@Configuration
-
@EnableConfigurationProperties(JedisProperties.class)
//开启属性注入,通过@autowired注入
-
@ConditionalOnClass(RedisClient.class)
//判断这个类是否在classpath中存在
-
public
class JedisAutoConfiguration {
-
-
@Autowired
-
private JedisProperties prop;
-
-
@Bean(name=
"jedisPool")
-
public JedisPool jedisPool() {
-
JedisPoolConfig config =
new JedisPoolConfig();
-
config.setMaxTotal(prop.getMaxTotal());
-
config.setMaxIdle(prop.getMaxIdle());
-
config.setMaxWaitMillis(prop.getMaxWaitMillis());
-
return
new JedisPool(config, prop.getHost(), prop.getPort());
-
}
-
-
@Bean
-
@ConditionalOnMissingBean(RedisClient.class)
//容器中如果没有RedisClient这个类,那么自动配置这个RedisClient
-
public RedisClient redisClient(@Qualifier("jedisPool")JedisPool pool) {
-
RedisClient redisClient =
new RedisClient();
-
redisClient.setJedisPool(pool);
-
return redisClient;
-
}
-
-
}
jedis客户端类
-
package com.java.Olym.redis;
-
-
import redis.clients.jedis.Jedis;
-
import redis.clients.jedis.JedisPool;
-
-
public
class RedisClient {
-
-
private JedisPool jedisPool;
-
-
public void set(String key, String value) throws Exception {
-
Jedis jedis =
null;
-
try {
-
jedis = jedisPool.getResource();
-
jedis.set(key, value);
-
}
finally {
-
//返还到连接池
-
if(jedis!=
null){
-
jedis.close();
-
}
-
}
-
}
-
-
public String get(String key) throws Exception {
-
Jedis jedis =
null;
-
try {
-
jedis = jedisPool.getResource();
-
return jedis.get(key);
-
}
finally {
-
//返还到连接池
-
if(jedis!=
null){
-
jedis.close();
-
}
-
}
-
}
-
-
public JedisPool getJedisPool() {
-
return jedisPool;
-
}
-
-
public void setJedisPool(JedisPool jedisPool) {
-
this.jedisPool = jedisPool;
-
}
-
-
}
-
前端jsp代码
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
-
pageEncoding=
"UTF-8"%>
-
<%@ include file="./res.jsp"%>
-
<html>
-
<head>
-
<script>
-
function setter(){
-
var key = $(
'input[name="key"]').val();
-
var value = $(
'input[name="value"]').val();
-
$.getJSON(
'<%=ctx%>/redis/set',{
"key":key,
"value":value},
function(result1){
-
if(result1!=
null){
-
alert(result1)
-
}
-
});
-
}
-
function getter(){
-
var key = $(
'input[name="key"]').val();
-
$.getJSON(
"<%=ctx%>/redis/get",{
"key":key},
function(result1){
-
if(result1!=
null){
-
$(
'input[name="value"]').val(result1);
-
}
-
});
-
}
-
</script>
-
</head>
-
<body>
-
<div>
-
<span>输入key
<input type="text" name="key" >
</span>
<p>
-
<span>输入value
<input type="text" name="value" >
</span>
<p>
-
<input type="button" value="提交" id="setter" onclick="setter();">
<p>
-
<input type="button" value="拿取" id="getter" onclick="getter();">
<p>
-
</div>
-
</body>
-
</html>
后端controller
-
package com.java.Olym.explore.controller;
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.ResponseBody;
-
import org.springframework.web.bind.annotation.RestController;
-
-
import com.java.Olym.redis.RedisClient;
-
-
@RequestMapping(
"redis")
-
@RestController
-
public
class RedisController {
-
@Autowired
-
private RedisClient redisClient;
-
-
@RequestMapping(
"set")
-
public
-
String
set
(String key,String value)
throws Exception{
-
redisClient.set(key, value);
-
return
"yes";
-
}
-
-
@RequestMapping(
"get")
-
public
-
String
get
(String key)
throws Exception{
-
return redisClient.get(key);
-
}
-
-
}
开始测试
点击提交,然后再redis-cli.exe 拿取数据
至此,成功
在其中也遇到问题:jedis异常:NoSuchElementException: Timeout waiting for idle object
然后按照网上的方法,要回收redis对象,然而还是不行,最后根据debug发现jedisproperties类未能读取redis的配置信息,后来改了写法,就可行了。
,注释前读不到,properties前面有,望知道问题的老友,告诉我一声。