-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> 复制代码
-
application.properties
spring.redis.host=127.0.0.1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
复制代码 -
Aplication
@SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } 复制代码
-
mapper
@Component //为了消除Controller里面的红线警告 @Mapper @CacheConfig(cacheNames = "users") public interface UserMapper { @Cacheable User get(Long id); void insert(User user); void update(User user); List<User> list(UserQueryObject qo); void delete(Long id); User findByUsername(String username); Set<String> getRoleNamesByUserId(Long userId); Set<String> getPermissionsNameByRoleNames(@Param("roleNames") Set<String> roleNames); } 复制代码
在需要缓存的地方添加@Cacheable注解