spring springmvc mybatis整合redis缓存
一、搭建基础的mvc框架
1.1新建maven项目
- 注:本文使用idea最新版可以直接创建web项目
1.2添加项目相关依赖
- 为了方便起见,在pom文件中统一配置各个依赖版本号
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.7.0</junit.version>
<spring.version>5.2.0.RELEASE</spring.version>
<mybatis.version>3.4.6</mybatis.version>
<mysql.version>8.0.22</mysql.version>
<druid.version>1.2.4</druid.version>
<jedis.version>3.4.1</jedis.version>
<fastjson.version>1.2.75</fastjson.version>
<mybatis.spring.version>1.3.2</mybatis.spring.version>
<spring.data.redis.version>2.4.2</spring.data.redis.version>
- 添加相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis.version}</version>
</dependency>
- 完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>codetiger.ssmredis</groupId>
<artifactId>ssm-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ssm-redis</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.7.0</junit.version>
<spring.version>5.2.0.RELEASE</spring.version>
<mybatis.version>3.4.6</mybatis.version>
<mysql.version>8.0.22</mysql.version>
<druid.version>1.2.4</druid.version>
<jedis.version>3.4.1</jedis.version>
<fastjson.version>1.2.75</fastjson.version>
<mybatis.spring.version>1.3.2</mybatis.spring.version>
<spring.data.redis.version>2.4.2</spring.data.redis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</project>
1.3添加配置文件
1.3.1 添加spring 基础配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置包自动扫描-->
<context:component-scan base-package="codetiger.ssmredis.ssm_redis">
<!-- 排除视图层-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
1.3.2 添加mybatis配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 配置基本属性-->
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 其他属性-->
<!-- 初始化大小-->
<property name="initialSize" value="5"/>
<!-- 最小连接池数量-->
<property name="minIdle" value="5"/>
<!-- 最大连接池数量-->
<property name="maxActive" value="20"/>
<!-- 获取连接时最大等待时间,单位毫秒-->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒-->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒-->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<!-- 测试连接-->
<property name="validationQuery" value="SELECT 1 from DUAL"/>
<!-- 申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性-->
<property name="testWhileIdle" value="true"/>
<!-- 获取连接时执行检测,建议关闭,影响性能-->
<property name="testOnBorrow" value="false"/>
<!-- 归还连接时执行检测,建议关闭,影响性能-->
<property name="testOnReturn" value="false"/>
<!-- 是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭-->
<property name="poolPreparedStatements" value="false"/>
<!-- 开启poolPreparedStatements后生效-->
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
<!-- 配置扩展插件,常用的插件有=>stat:监控统计 slf4j:日志 wall:防御sql注入-->
<property name="filters" value="stat,wall,slf4j"/>
<property name="connectionProperties" value="druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000"/>
</bean>
<!-- 配置SqlSession的工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!--配置Mybatis的映射xml文件路径-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--指定Mybatis的mapper接口扫描包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定刚才我们配置好的sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定mapper接口扫描包-->
<property name="basePackage" value="codetiger.ssmredis.ssm_redis.dao"/>
</bean>
<!--配置事务管理器,如果不配置这个,不启动事务扫描的话,那么发生了异常也不会触发回滚-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--还得指定数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--启动事务的扫描-->
<tx:annotation-driven/>
</beans>
1.3.3 添加springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置视图解析器,这样控制器里就直接返回文件名就好了-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--配置静态资源过滤,不然静态资源比如css是访问不到的-->
<mvc:default-servlet-handler/>
<!--配置扫描的包-->
<context:component-scan base-package="nn.apartment" use-default-filters="false">
<!--只扫描controller,这边MVC就只扫描controller,就不会IOC那边冲突,否则事务会被覆盖,IOC那边就要排除这个controller-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--启用MVC的注解-->
<mvc:annotation-driven/>
</beans>
1.3.4添加mybatis控制文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置全局设置-->
<settings>
<!--启用日志,并指定日志实现方式-->
<setting name="logImpl" value="SLF4J"/>
<!--启用主键生成策略-->
<setting name="useGeneratedKeys" value="true"/>
<!--配置启用下划线转驼峰的映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--启用二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
1.3.5修改web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--1.配置Spring IOC容器的创建,如果不配置这个,Mybatis就在web应用里无法使用-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--spring配置文件路径-->
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--SpringMVC整合配置文件路径-->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--3.配置字符编码过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
1.3.6添加数据源
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://db:3306/apartment?useUnicode=true&characterEncoding=utf-8
jdbc.username=
jdbc.password=
1.3.7 添加redis相关配置,使用Java文件配置
RedisConfig.java
package codetiger.ssmredis.ssm_redis.utils;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:35
**/
@Configuration
public class RedisConfig {
String redishost = "program";
int port = 6379;
//配置jedispool
@Bean("JedisPoolConfig")
public JedisPoolConfig getJedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 配置最大连接数
jedisPoolConfig.setMaxTotal(10);
// 配置最大空闲连接数
jedisPoolConfig.setMaxIdle(5);
// 配置最小空闲连接数
jedisPoolConfig.setMinIdle(0);
// 配置是否需要等待
jedisPoolConfig.setBlockWhenExhausted(true);
// 配置最大等待时间毫秒
jedisPoolConfig.setMaxWaitMillis(60000);
// 是否进行连接有效性检测
jedisPoolConfig.setTestOnBorrow(true);
// 是否进行释放有效性检测
jedisPoolConfig.setTestOnReturn(true);
// 是否开启JMX监控
jedisPoolConfig.setJmxEnabled(true);
return jedisPoolConfig;
}
//配置连接工厂
@Bean("JedisConnectionFactory")
public JedisConnectionFactory getJedisConnectionFactory(@Qualifier("JedisPoolConfig") JedisPoolConfig jedisPoolConfig){
try {
//单机版jedis
RedisStandaloneConfiguration redisStandaloneConfiguration =
new RedisStandaloneConfiguration();
//设置redis服务器的host或者ip地址
redisStandaloneConfiguration.setHostName(redishost);
//设置默认使用的数据库
// redisStandaloneConfiguration.setDatabase(0);
//设置密码
// redisStandaloneConfiguration.setPassword(RedisPassword.of("123456"));
//设置redis的服务的端口号
redisStandaloneConfiguration.setPort(port);
//获得默认的连接池构造器(怎么设计的,为什么不抽象出单独类,供用户使用呢)
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
//指定jedisPoolConifig来修改默认的连接池构造器(真麻烦,滥用设计模式!)
jpcb.poolConfig(jedisPoolConfig);
//通过构造器来构造jedis客户端配置
JedisClientConfiguration jedisClientConfiguration = jpcb.build();
//单机配置 + 客户端配置 = jedis连接工厂
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
catch (Exception e){
return null;
}
}
//配置redis模板
@Bean
public RedisTemplate<String, String> redisTemplate(@Qualifier("JedisConnectionFactory") JedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(cf);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(JedisConnectionFactory factory) {
FastJsonRedisSerializer<Object> fastJsonUtil = new FastJsonRedisSerializer<>(Object.class);
// 生成两套默认配置,通过 Config 对象即可对缓存进行自定义配置
RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
// 设置过期时间 10 分钟
.entryTtl(Duration.ofMinutes(10))
// 设置缓存前缀
.prefixKeysWith("luyu:")
// 禁止缓存 null 值
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonUtil));
// 设置 key 序列化
// .serializeKeysWith()
// // 设置 value 序列化
// .serializeValuesWith();
// 返回 Redis 缓存管理器
return RedisCacheManager.builder(factory)
.withCacheConfiguration("user", cacheConfig).build();
}
}
序列化工具:
FastJsonRedisSerializer.java
package codetiger.ssmredis.ssm_redis.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.StandardCharsets;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:38
**/
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
private final Class<T> clazz;
public FastJsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(StandardCharsets.UTF_8);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
//添加fastjson白名单
ParserConfig.getGlobalInstance().addAccept("codetiger.ssmredis.ssm_redis.entily");
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, StandardCharsets.UTF_8);
return JSON.parseObject(str, clazz);
}
}
文件结构如下:
二、编写测试用例
2.1 用户实体
package codetiger.ssmredis.ssm_redis.entily;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:43
**/
public class User {
int id;
String username;
String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.2数据库映射
UserDao.java
package codetiger.ssmredis.ssm_redis.dao;
import codetiger.ssmredis.ssm_redis.entily.User;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:45
**/
@Repository
public interface UserDao {
List<User> selectAll();
}
2.3用户服务
UserService.java
package codetiger.ssmredis.ssm_redis.service;
import codetiger.ssmredis.ssm_redis.dao.UserDao;
import codetiger.ssmredis.ssm_redis.entily.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:45
**/
@Service
public class UserService {
@Autowired
UserDao userDao;
public List<User> selectAll(){
return userDao.selectAll();
}
}
2.4视图层
UserController.java
package codetiger.ssmredis.ssm_redis.controller;
import codetiger.ssmredis.ssm_redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:48
**/
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/user/selectAll")
@ResponseBody
public String seltctAll(){
return userService.selectAll().toString();
}
}
2.5 mybatis映射文件
UserMapper.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="codetiger.ssmredis.ssm_redis.dao.UserDao">
<!--开启缓存-->
<cache/>
<!--从数据库中查询出所有的User对象-->
<select id="selectAll" resultType="codetiger.ssmredis.ssm_redis.entily.User">
select * from user
</select>
</mapper>
项目目录结构:
2.6 运行测试是否成功
三、改造实现redis缓存
3.1 改造service层
package codetiger.ssmredis.ssm_redis.service;
import codetiger.ssmredis.ssm_redis.dao.UserDao;
import codetiger.ssmredis.ssm_redis.entily.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: ssm-redis
* @description:
* @author: Mr.Nie
* @create: 2020-12-31 18:45
**/
@Service
public class UserService {
@Autowired
UserDao userDao;
@Cacheable(value = "user", key = "'test'")
public List<User> selectAll(){
return userDao.selectAll();
}
}
3.2 运行项目检查redis是否有数据存入