目录
前言
先上源码: https://github.com/YKRY35/SpringBoot_C_Redis
编译运行下先:
gradlew build
_start.bat
测试在:13. 测试
人生苦短,运行不起来就赶紧去找下一家。
踩了一天的坑,刚爬出来。
START
1. 新建项目
2. 配置 build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'redis' // 生成jar名称
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8 //java 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-redis")
}
3. 保存后,右下角 import changes 一下
等待依赖包下载
4. 这里的项目目录结构
5. 编写Application.java
@ComponentScan 里添加一下自己需要扫描的包
package app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"controller","util"})
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
6. 编写RedisConfig.java
目的是获得RedisTemplate。
class注释Annotation标注为@Configuration。
创建返回RedisTemplate的方法,标注为@Bean。
package util;
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;
@Configuration
public class RedisConfig {
@Bean
RedisTemplate template(RedisConnectionFactory redisConnectionFactory){
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
7. 编写RedisUtil.java
保存RedisTemplate,包装一些基本的操作。
片段:
@Component
public class RedisUtil {
@Bean
RedisUtil util(RedisTemplate template){
return new RedisUtil(template);
}
private RedisTemplate template;
public RedisUtil(RedisTemplate template){
this.template = template;
}
}
那么 只要声明@Autowired RedisUtil redisUtil,系统就自己找到带Bean注释的返回RedisUtil的方法,它的唯一参数template的获得方法在6.RedisConfig中。会调用RedisConfig中的template方法,参数RedisConnectionFactory factory由系统自己传入,然后再一路往回,创建出RedisUtil,其中template是单例的,不同的Redisutil的template在内存中是指向同一个东西。
RedisUtil构造函数把接收到的template给保存。
读写redis数据库这里只有简单的两种。
package util;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisUtil {
@Bean
RedisUtil util(RedisTemplate template){
return new RedisUtil(template);
}
private RedisTemplate template;
public RedisUtil(RedisTemplate template){
this.template = template;
}
//value
public boolean setValue(Object key, Object value){
try{
template.opsForValue().set(key, value);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public Object getValue(Object key){
return template.opsForValue().get(key);
}
}
8. 编写C.java
测试类。接收到http请求时写或者读sql数据库。
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import util.RedisUtil;
@RestController
public class C {
@Autowired
RedisUtil redisUtil;
@RequestMapping("/set")
public void set(@RequestParam(value="key", defaultValue = "k") String key,
@RequestParam(value = "value", defaultValue = "v") String value){
redisUtil.setValue(key, value);
}
@RequestMapping("/get")
public Object get(@RequestParam(value="key", defaultValue = "k") String key){
return redisUtil.getValue(key);
}
}
9. 配置文件application.properties
这里Redis端口用的是25001
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=25001
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
10. 启动Redis服务
测试一下启动了没:
恩,启动了。
11. 编译
gradlew build
编译成功:
生成了jar。
12. 运行生成的jar
java -jar build/libs/redis-0.1.0.jar --server.port=15001
这里web服务端口用的是15001。
13. 测试
先清空redis数据库。
flushall
浏览器打开:
http://localhost:15001/set?key=abc&value=def
浏览器内容空白,没报错。
应该调用了set方法。
瞅一眼redis数据库。
keys *
写进去了。
测试一下get。
浏览器打开:
http://localhost:15001/get?key=abc
显示:
啊,行了。
另外
RedisUtil根据需要,去包装。
项目地址: https://github.com/YKRY35/SpringBoot_C_Redis