1. 配置环境Redis 4 安装教程自行google
Spring 5.0.9
Mybatis 3.4.6
2. 依赖
注意:默认你配置好了SSM的依赖环境,我只说在SSM配好过后怎么把redis加进去
我用的依赖是如下的版本,基本都没有最新jar包的教程,网上的老教程千奇百怪,真的是痛心。
如果你不是用的spring-data-redis的2.X的版本你可以不用看了,看了也没用。
2.9.0
2.1.0.RELEASE
redis.clients
jedis
${jedis.version}
org.springframework.data
spring-data-redis
${redis.version}
3.配置
首先是redisConfig.java,哇,这个是真的一个巨坑,改动贼多。老教程根本无法使用。作为一个新手瑟瑟发抖。不多说了,先贴代码,再解释import org.springframework.beans.factory.annotation.Value;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.*;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.JdkSerializationRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;@Configuration@PropertySource("classpath:config/redis.properties")@EnableCachingpublic class RedisConfig { @Value("${redis.maxTotal}") private int maxTotal; @Value("${redis.maxIdle}") private int maxIdle; @Value("${redis.maxWaitMillis}") private long maxWaitMillis; @Value("${redis.testOnBorrow}") private boolean testOnBorrow; @Value("${redis.host}") private String host; @Value("${redis.port}") private int port; @Value("${redis.timeout}") private int timeout; @Value("${redis.password}") private String password; @Value("${redis.database}") private int database; @Value("${redis.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @Value("${redis.softMinEvictableIdleTimeMillis}") private int softMinEvictableIdleTimeMillis; @Value("${redis.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @Value("${redis.numTestsPerEvictionRun}") private int numTestsPerEvictionRun; @Value("${r