为了减轻数据库压力,提高性能, 一般都会在项目中使用缓存技术 .本章是spring-boot在集成了mybatis后, 集成Redis,将查到的数据存入Redis数据库中,来减轻关系型数据库的压力, 以及访问的效率;集成mybatis参考链接:http://blog.csdn.net/lengtianya/article/details/78782659
一:依照惯例,首先添加相关的依赖文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
二: 配置Redis数据源
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
….
三:开启缓存
在入口处添加开启缓存的注解
@SpringBootApplication
@EnableCaching //开启缓存
@MapperScan(basePackages = “com.surd.springboot128.dao”)
public class SpringBoot128Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot128Application.class, args);
}
}
四:缓存设置
@Cacheable(value = “findAll”) //缓存设置
public User findAll(){
System.out.println(“是否缓存了呢?”);
return userDao.findAll();
}
可以测试,第一次会打印, 当被缓存之后, 便不会再打印方法中的语句了.
到此spring boot已经成功的集成了Redis; 当然这个只是一个入门;