缓存:即将常用的并且不会频繁变更的数据存入内存当中,从而减少服务器数据库压力,提升访问速率。
使用redis作为缓存技术方案,主要是redis缓存技术的特点就在于高效,因为目前涉及的数据量逐渐增多,在对于数据的存储上面和sql以及服务器资源优化上面就来的特别的重要。而redis可以帮助解决由于数据库压力造成的延迟现象,针对于很少做改变的数据并且经常使用的数据,我们可以一致性加入内存。这样可以一方面减少数据库压力,一方面提高读写效率。下面笔者通过一个demo演示springboot整合mybatis以及redis的详细教程,对于springboot集成mybatis不熟悉的请移步这里查看教程详解《最全面的springBoot集成mybatis+mysql项目搭建经验分享》
1、在pom.xml文件添加Redis依赖
<!-- Spring Boot Reids 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
2、在application.yml文件中配置redis
#默认是8080,此处使用配置8060
server:
port: 8060
spring:
#数据源配置
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#springboot集成redis配置
redis:
# Redis服务器地址
host: 127.0.0.1
# Redis数据库索引(默认为0
database: 0
port: 6379
# Redis服务器连接密码(默认为空)
password: 123456
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
max-idle: 8
min-idle: 0
mybatis:
#扫描实体类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写实体类的全路径名了
typeAliasesPackage: com.guo.springboot01.entity
mapperLocations: classpath:mapper/*Dao.xml
3、controller,service,dao以及mapping映射文件代码
@RestController
@EnableAutoConfiguration
public class UserController {
@Autowired
private IUserService userService;
@AnalysisActuator(note = "测试自定义注解")
@RequestMapping("findById")
public User getUser(Integer id) {
return userService.getUserById(id);
}
@RequestMapping("findAll")
public List<User> getAllUser() {
return userService.findAll();
}
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserDao userDao;
@Autowired
private RedisTemplate redisTemplate;
/**
* 获取用户策略:先从缓存中获取用户,没有则取数据表中查
* 数据,再将数据写入缓存
*/
@Override
public User getUserById(Integer id) {
String key = id+"";
ValueOperations <String,User> operations = redisTemplate.opsForValue();
boolean hasKey = redisTemplate.hasKey(key);
//如果缓存存在
if(hasKey){
User user = operations.get(key);
System.out.println("==========从缓存中获得数据=========");
System.out.println(user.getUserName());
System.out.println("==============================");
return user;
}else{
User user = userDao.findById(id);
System.out.println("==========从数据表中获得数据=========");
System.out.println(user.getUserName());
System.out.println("==============================");
//插入缓存
operations.set(key, user,5, TimeUnit.MINUTES);
return user;
}
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
public interface UserDao {
public User findById(Integer id);
public List<User> findAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.guo.springboot01.dao.UserDao" >
<resultMap id="BaseResultMap" type="com.guo.springboot01.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from user where id = #{id}
</select>
<select id="findAll" resultMap="BaseResultMap">
select * from user
</select>
</mapper>
4、浏览器测试
测试前先在本地mysql数据库里新建user表,插入2条测试数据,并且启动本地Redis服务
第一次请求,控制台输出:
第二次请求,控制台输出