欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-30 15:51:35
...

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();
    }
}