Springboot整合redis+mybatis-plus及自定义json规则的CacheManager

Springboot整合redis及自定义json规则的CacheManager


1.导入依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- mybatis plus 代码生成器 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2.配置application.properties文件:

spring.datasource.url=jdbc:mysql://localhost:3306/eshop?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

#redis配置需要datasource数据源
spring.redis.host=虚拟机Ip
spring.redis.port=6379

3.主启动类:

@MapperScan(basePackages = "xxxxxx.mapper")
@SpringBootApplication
@EnableCaching  //开启缓存
public class Springboot211RedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot211RedisApplication.class, args);
    }
}

4.创建实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User implements Serializable {
    private String id;
    private String username;
    private String password;
    private String img;
}

5.创建mapper接口:

public interface UserMapper extends BaseMapper<User> {
}

6.service层:

@Service
@CacheConfig(cacheNames = "user")
public class UserServiceImpl{
    @Resource
    private UserMapper userMapper;

    @Cacheable(value = "user")
    public List<User> getUsers(){
        return userMapper.selectList(null);
    }
}

7.controller层:

@RestController
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/users")
    public List<User> getUsers(){
        return userService.getUsers();
    }
}

8.redis配置类:

@Configuration
public class MyRedisConfig {
    /**
     * 自定义redisCacheManager中自定义json序列化规则
     * @param factory
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration cacheConfiguration =
                RedisCacheConfiguration.defaultCacheConfig()
                        .entryTtl(Duration.ofDays(1))
                        .disableCachingNullValues()
                        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new
                                GenericJackson2JsonRedisSerializer()));
        return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

记或往

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值