本篇将介绍springboot整合redis。笔者在之前的redis系列做了细致的介绍和展示,这里不做过多的说明。
本篇介绍单机版redis的集成。
1.开启redis服务
虚拟机系统:centos6.5
redis版本:3.2.1
默认端口:6379,
ip:192.168.72.133
启动redis服务
redis-server redis.conf
连接redis客户端
redis-cli -p 6379
这里又一点需要注意:
redis必须是以非守护进程进行启动,设置daemonize为no,不然在存储的时候会因为连接不上redis服务而报错。
daemonize no
2.添加redis依赖
pom.xml
springboot1.4+以后使用的是spring-boot-starter-data-redis,所以在配置的时候不要配置错了,springboot1.4-使用的是spring-boot-starter-redis,笔者这里使用的spring1.5.9,所以配置如下
<!-- 配置redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.配置springboot配置文件
#redis配置,使用redis作为缓存
spring.redis.host=192.168.72.133
spring.redis.port=6379
4.创建实体类
UserInfo
注意这里需要序列化,不然无法网络传输
package com.tang.bean;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.alibaba.fastjson.annotation.JSONField;
@Entity
@Table(name="springboot_userinfo")
public class UserInfo implements Serializable{
@Id
@GeneratedValue
private Integer id;
private String userName;
private String loginName;
//不返回json
@JSONField(serialize=false)
private String password;
private Integer age;
@JSONField(format="yyyy-MM-dd HH:mm")
private Date birthDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", userName=" + userName + ", loginName="
+ loginName + ", password=" + password + ", age=" + age
+ ", birthDate=" + birthDate + "]";
}
}
5.创建controller
这里使用的mybatis的一些配置,仅列举部分。如果不使用mybatis配置,可以直接使用springdatajpa做简单的测试即可,不必集成mybatis,笔者这里是跟随前面一篇集成mybaits做的案例。
package com.tang.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tang.bean.UserInfo;
import com.tang.service.UserInfoMybatisService;
@RestController
public class UserInfoMybatisController {
@Autowired
private UserInfoMybatisService userInfoMybatisService;
@RequestMapping(value="/mybatisGetAllUserInfos")
public List<UserInfo> getAllUserInfos(){
return userInfoMybatisService.getUserInfosByMybatis();
}
@RequestMapping(value="/mybatisGetUserInfoById/{uid}")
public UserInfo getUserInfoById(@PathVariable("uid") Integer id){
return userInfoMybatisService.getUserInfosByMybatisById(id);
}
}
6.创建service
使用@Cacheable(value=”getUserInfosByMybatis”)注解,将getUserInfosByMybatis作为redis存储的key值。并且第一次请求打印提示,如果缓存起作用则第二次请求不会打印提示信息。
package com.tang.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.tang.bean.UserInfo;
import com.tang.mapper.UserInfoMybatisMapper;
import com.tang.service.UserInfoMybatisService;
@Service("userInfoMybatisService")
public class UserInfoMybatisServiceImpl implements UserInfoMybatisService{
@Autowired
public UserInfoMybatisMapper userInfoMybatisMapper;
@Cacheable(value="getUserInfosByMybatis")
public List<UserInfo> getUserInfosByMybatis() {
System.out.println("如果我被打印了,说明没有使用缓存");
List<UserInfo> allUserInfos = userInfoMybatisMapper.getAllUserInfos();
return allUserInfos;
}
@Cacheable(value="getUserInfosByMybatisById")
public UserInfo getUserInfosByMybatisById(Integer id) {
System.out.println("如果我被打印了,说明没有使用缓存");
return userInfoMybatisMapper.getUserInfoById(id);
}
}
7.创建mapper
package com.tang.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.tang.bean.UserInfo;
/**
* 添加mybatis的@Mapper注解,将接口放入ioc容器,@Repository不起作用
*
*/
@Mapper
public interface UserInfoMybatisMapper {
public List<UserInfo> getAllUserInfos();
public UserInfo getUserInfoById(@Param("uid") Integer id);
}
8.修改启动类
添加@EnableCaching注解开启缓存
package com.tang;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@SpringBootApplication
@MapperScan(value="com.tang.mapper.*")
@EnableCaching
public class App extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.创建converter
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.创建fastJson配置参数
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.为convert设置配置项
fastConverter.setFastJsonConfig(fastJsonConfig);
//4.将convert添加到onverters配置
converters.add(fastConverter);
}
}
测试:
http://localhost:8080/springboot/mybatisGetAllUserInfos
提示信息就打印了一次,然后进入到redis客户端,查看信息
查询的数据已经存储到redis中。