spring boot redis做mybatis二级缓存

本文介绍如何在Spring Boot项目中整合Redis作为缓存服务。通过添加必要的依赖配置及使用Spring Boot Starter Data Redis,实现自动配置。展示了如何通过@Cacheable等注解应用缓存逻辑,并提供了UserMapper接口示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 添加依赖

    <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注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值