1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <!-- mybatis(注解方式) --> 7 <dependency> 8 <groupId>org.mybatis.spring.boot</groupId> 9 <artifactId>mybatis-spring-boot-starter</artifactId> 10 <version>1.3.1</version> 11 </dependency> 12 13 <!-- Mysql --> 14 <dependency> 15 <groupId>mysql</groupId> 16 <artifactId>mysql-connector-java</artifactId> 17 </dependency> 18 19 <!-- redis依赖 --> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-redis</artifactId> 23 </dependency>
1 #数据库配置 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 3 spring.datasource.username=root 4 spring.datasource.password= 5 spring.datasource.url=jdbc:mysql://localhost:3306/springboot 6 7 #redis单服务器配置 8 spring.redis.database=0 9 spring.redis.host=127.0.0.1 10 spring.redis.port=6379 11 spring.redis.pool.max-active=8 12 spring.reids.pool.max-wait=-1 13 spring.redis.pool.max-idle=8 14 spring.redis.pool.min-idle=0 15 spring.redis.timeout=0 16
在启动类上添加@EnableCaching 注解
1 package com.wu.app; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.cache.annotation.EnableCaching; 7 8 9 @SpringBootApplication(scanBasePackages={"com.wu.controller","com.wu.service"}) 10 @MapperScan("com.wu.mapper")//需要单独扫描 11 @EnableCaching//开启缓存 12 public class SpringApplications { 13 //程序启动入口 14 public static void main(String []args){ 15 SpringApplication.run(SpringApplications.class, args); 16 } 17 }
在需要缓存的service层方法中添加@Cacheable 注解
1 package com.wu.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.cache.annotation.Cacheable; 5 import org.springframework.stereotype.Service; 6 7 import com.wu.mapper.UsersMapper; 8 import com.wu.pojo.Users; 9 @Service 10 public class UsersServiceImp implements UsersService { 11 @Autowired 12 private UsersMapper mapper; 13 14 @Cacheable(value="user")//设置键值 15 @Override 16 public Users selectByName(String name) { 17 System.out.println("从数据库中查找"); 18 return mapper.selectByName(name); 19 } 20 21 22 }
实体类需要实现序列化接口 implements Serializable
1 public class Users implements Serializable{ 2 private Integer id; 3 4 private String name; 5 6 private String password; 7 8 private String email; 9 10 private Date birthday; 11 12 public Integer getId() { 13 return id; 14 } 15 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name == null ? null : name.trim(); 26 } 27 28 public String getPassword() { 29 return password; 30 } 31 32 public void setPassword(String password) { 33 this.password = password == null ? null : password.trim(); 34 } 35 36 public String getEmail() { 37 return email; 38 } 39 40 public void setEmail(String email) { 41 this.email = email == null ? null : email.trim(); 42 } 43 44 public Date getBirthday() { 45 return birthday; 46 } 47 48 public void setBirthday(Date birthday) { 49 this.birthday = birthday; 50 }
这样第一次查询时即从数据库查询,再次查询即从缓存中加载
原文:https://www.cnblogs.com/wuba/p/11256111.html