springboot整合redis官方是有文档的:
英文看不懂可以翻译,代码应该看得懂,
这个是自动注入的。当然也可以xml注入,手动配置。
整合步骤:
pom文件:
<!-- spring boot web -->
<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>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<!-- springboot整合 redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
配置文件:
#端口设置
server:
port: 9999
#mybatis连接设置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/ehuoyuan?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
maxActive: 20 #最大活跃度
initialSize: 1 #初始化数量
maxWait: 60000 #超时配置
poolPreparedStatements: true #打开PSCache
maxPoolPreparedStatementPerConnectionSize: 20 #制定每个PSCache 大小
validation-query: select 1
validationQueryTimeout: 5
testWhileIdle: true
#redis配置
redis:
host: 127.0.0.1
#password: 123456
port: 6379
pool:
max-idle: 100
min-idle: 1
max-active: 1000
max-wait: -1
#mybatis 配置文件映射
mybatis:
mapper-locations: classpath*:mapper/**/*.xml
java代码:
package com.siyuan.app.h5.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.siyuan.app.h5.model.UserModel;
import com.siyuan.app.h5.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@RestController
@RequestMapping("app/")
@Api("app测试类")
public class DemoController {
@Autowired
private UserService userService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@RequestMapping("test")
@ResponseBody
@ApiOperation(value="查询用户", notes="某某是需要必填")
public List<String> inde(){
List<String> list = new ArrayList<String>();
List<UserModel> userList = userService.getUesrs();
for (UserModel user : userList) {
list.add(user.getName());
}
return list;
}
@RequestMapping(value="redisAdd",method=RequestMethod.POST)
@ApiOperation(value="redisAdd", notes="redis新增")
public void redisAdd(@ApiParam(name = "key", value = "redisKey",required = true) @RequestParam(name = "key", required = true) String key,
@ApiParam(name = "value",value = "redisValue",required = true)@RequestParam(name = "value",required = true)String value
){
//操作String类型的数据
ValueOperations<String, String> valueStr = redisTemplate.opsForValue();
//存储一条数据
valueStr.set(key,value);
}
@RequestMapping(value="getRides",method=RequestMethod.POST)
@ResponseBody
@ApiOperation(value="getRides", notes="redis查询")
public String getRides(@ApiParam(name = "key", value = "redisKey",required = true) @RequestParam(name = "key", required = true) String key){
//操作String类型的数据
ValueOperations<String, String> valueStr = redisTemplate.opsForValue();
//存储一条数据
String vale = valueStr.get(key);
return vale;
}
}
其中上面注解是swagger传值的注解,仅供测试使用,其他使用就是鸡肋了。
测试界面:
上面就是简单的实现redis的存数据,取数据。具体怎么用看你们,如果redis不会安装可以去看看简单的安装教程。这里就不一一描述了。
最后建议还是去看官方文档,官方文档比网上什么教程要更标准。
做技术最难能可贵的就是持续的学习,吸收知识,不断的思考形成自己的理解和模式。