pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qing</groupId>
<artifactId>shpringboot-rediscache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shpringboot-rediscache</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.4.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--集成springmvc框架并实现自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--辅助包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
application.properties关于redis配置
#redis 地址
spring.redis.host=localhost
spring.redis.port=6379
# redis密码记得改
spring.redis.password=123456
spring.redis.timeout=4000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1000
spring.redis.lettuce.pool.min-idle=1
#缓存类型 指定缓存提供者
spring.cache.type=redis
#缓存名称, 在redis 的key 中前缀会加这个 key的形式是 realTimeCache::
spring.cache.cache-names=realTimeCache
application.yml关于mybatis-plus配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
# type: com.alibaba.druid.pool.DruidDataSource #连接池配置
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
write-dates-as-timestamps: false
server:
port: 80
servlet:
context-path: /
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml # 扫描mapper.xml的位置
type-aliases-package: com.qing.entity # 实体类别名,在mapper.xml中resultType可以直接写类名,不用写完整类名
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
项目结构
数据库
controller
package com.qing.shpringbootrediscache.controller;
import com.qing.shpringbootrediscache.entity.TUser;
import com.qing.shpringbootrediscache.service.TUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* (TUser)表控制层
*
* @author makejava
* @since 2020-12-01 11:04:56
*/
@RestController
@RequestMapping("tUser")
public class TUserController {
/**
* 服务对象
*/
@Resource
private TUserService tUserService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public TUser selectOne(Long id) {
return this.tUserService.queryById(id);
}
@PostMapping("/add")
public TUser add(@RequestBody TUser user) {
return this.tUserService.add(user);
}
@GetMapping("/delById/{id}")
public boolean delById(@PathVariable("id")int id) {
return this.tUserService.delById(id);
}
}
service
package com.qing.shpringbootrediscache.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qing.shpringbootrediscache.entity.TUser;
import java.util.List;
/**
* (TUser)表服务接口
*
* @author makejava
* @since 2020-12-01 11:04:54
*/
public interface TUserService extends IService<TUser> {
TUser queryById(Long id);
TUser add(TUser user);
boolean delById(int id);
}
serviceImpl
package com.qing.shpringbootrediscache.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.shpringbootrediscache.entity.TUser;
import com.qing.shpringbootrediscache.mapper.TUserMapper;
import com.qing.shpringbootrediscache.service.TUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* (TUser)表服务实现类
*
* @author makejava
* @since 2020-12-01 11:04:54
*/
@Service("tUserService")
public class TUserServiceImpl extends ServiceImpl<TUserMapper,TUser> implements TUserService {
@Resource
private TUserMapper tUserMapper;
/**
* 通过ID查询单条数据
* 如果redis中有数据,会从redis中获取,不会走方法的代码
* @param id 主键
* @return 实例对象
*/
@Override
//将查询出来的值存入redis
@Cacheable(cacheNames = "realTimeCache",key = "'user_'+#id")
public TUser queryById(Long id) {
return this.tUserMapper.selectById(id);
}
@Override
//添加数据时将对应的key的值进行更新,更新的内容为方法返回的值
@CachePut(cacheNames = "realTimeCache",key = "'user_'+#result.id")
public TUser add(TUser user) {
int insert = this.tUserMapper.insert(user);
if (insert == 0) {
return null;
}
return user;
}
@Override
//通过id删除时,如果方法中的 i !=0,则删除对应的key,key使用spel表达式
@CacheEvict(cacheNames = "realTimeCache",key = "'user_'+#id",condition = "#i != 0")
public boolean delById(int id) {
int i = this.tUserMapper.deleteById(id);
return i == 0 ? false:true;
}
}
mapper
package com.qing.shpringbootrediscache.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qing.shpringbootrediscache.entity.TUser;
import org.apache.ibatis.annotations.Mapper;
/**
* (TUser)表数据库访问层
*
* @author makejava
* @since 2020-12-01 11:04:54
*/
public interface TUserMapper extends BaseMapper<TUser>{
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qing.shpringbootrediscache.mapper.TUserMapper">
</mapper>
entity
package com.qing.shpringbootrediscache.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
/**
* (TUser)实体类
*
* @author makejava
* @since 2020-12-01 11:04:52
*/
@Data
@TableName("t_user")
public class TUser implements Serializable {
private static final long serialVersionUID = 361943400023171272L;
@TableId(type = IdType.AUTO)
private Long id;
private Integer age;
private String note;
private String userName;
}
启动类
package com.qing.shpringbootrediscache;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableCaching//开启缓存
@MapperScan("com.qing.shpringbootrediscache.mapper")//mapper包扫描
@EnableTransactionManagement//开启事务管理
public class ShpringbootRediscacheApplication {
public static void main(String[] args) {
SpringApplication.run(ShpringbootRediscacheApplication.class, args);
}
}
测试
redis中的数据格式