菜鸟第一次用springboot整合redis可能碰到的坑集合
网上很多整合springboot整合redis的教程,但是对于菜鸟来说一个教程基本上不可能成功,可能会碰到各种各样的问题,所以我特意在这里总结一下可能会碰到的坑
首先这是一个最简单的maven项目
首先第一步肯定是pom.xml
大家都知道pom依赖出问题是最懵逼的问题
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
一共就只有三个依赖,有少部分同学会出现一个依赖导不进的问题
这个时候在pom文件里加上
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
记得clean install之后刷新一下包
如果再不行就把仓库里的包删了再下载一遍,不过很少还有导不进包的情况了
接下来就是整合了
其实一共也就是5个类加一个配置文件
首先是实体类
package com.yirancn.bean;
import java.io.Serializable;
import java.util.Date;
/**
* @作者 依然这么溜
* @create 2020/12/14 上午10:02
*/
public class User implements Serializable {
private String name;
private int age;
private Date time;
public User(String name, int age, Date time) {
this.name = name;
this.age = age;
this.time = time;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", time=" + time +
'}';
}
}
当然你也可以偷懒用那三个偷懒专用注解,但是看这个教程的基本上都是新手,我想还是不偷这个懒为好,这里注意把实体类序列化就行,实体类没什么好说的
接下来是dao层,
package com.yirancn.dao;
import com.yirancn.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
/**
* @作者 依然这么溜
* @create 2020/12/14 上午10:05
*/
@Repository
public class UserRedis {
@Autowired
private RedisTemplate<Object, User> redisTemplate;
public RedisTemplate<Object, User> getRedisTemplate() {
return redisTemplate;
}
public void add(String key,User user){
redisTemplate.opsForValue().set(key,user);
}
public User get(String key){
return redisTemplate.opsForValue().get(key);
}
}
注意,此处有部分可能出现RedisTemplate<Object, User> redisTemplate;不自动注入问题,这个时候我们把RedisTemplate的泛型去掉然后返回的时候强转一下,算了我怕你们听不懂我还是把改好的代码拿出来让你们复制,尽量自己领悟哦
package com.yirancn.dao;
import com.yirancn.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
/**
* @作者 依然这么溜
* @create 2020/12/14 上午10:05
*/
@Repository
public class UserRedis {
@Autowired
private RedisTemplate redisTemplate;
public RedisTemplate<Object, User> getRedisTemplate() {
return redisTemplate;
}
public void add(String key,User user){
redisTemplate.opsForValue().set(key,user);
}
public User get(String key){
return (User) redisTemplate.opsForValue().get(key);
}
}
接下来是redisConfig
package com.yirancn.redisconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @作者 依然这么溜
* @create 2020/12/14 上午10:13
*/
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String,Object> redisTemplate(){
RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
//TODO:指定Key、Value的序列化策略
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
@Bean
public StringRedisTemplate stringRedisTemplate(){
StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
return stringRedisTemplate;
}
}
这里基本上没有会出错的,为了防止无法主动注入,记得把泛型去掉就行
接下来是启动类
package com.yirancn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @作者 依然这么溜
* @create 2020/12/14 上午10:45
*/
@SpringBootApplication
public class Springbootredisapplica {
public static void main(String[] args) {
SpringApplication.run(Springbootredisapplica.class);
}
}
这个我相信童鞋们应该都晓得,我就不多说了
#redis
spring.redis.host=47.107.108.32
spring.redis.port=6379
#spring.redis.password=
spring.redis.password=test@dbuser2018
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-avtive=8
spring.redis.jedis.pool.max-wait=0ms
这是配置文件,上面的是我借用的服务器的redis,你们本地的redis就是127.0.0.1了,顺便配置了Redis的jedis池的参数。端口是默认的6379
ps.别告诉我到这里了你redis都没装
** 然后是测试类**
值得注意的是测试类要和dao层的UserRedis在相同层次的包下哦
我的dao层是 com.yirancn.dao;
所以我测试类的的包名也叫这个包你可以划上去看我的包结构
package com.yirancn.dao;
import com.yirancn.bean.User;
import com.yirancn.Springbootredisapplica;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
/**
* @作者 依然这么溜
* @create 2020/12/14 上午10:15
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Springbootredisapplica.class)
public class UserRedisTest {
@Autowired
UserRedis userRedis;
@Test
public void tn() {
User user = new User();
user.setName("nihui");
user.setAge(24);
user.setTime(new Date());
userRedis.add(this.getClass().getName(), user);
System.out.println(userRedis.get(this.getClass().getName()));
}
}
在springboot下测试RunWith和SpringBootTest这两个注解都是要加的
SpringBootTest里的参数加上你springboot主启动类的class就行了
,
注意这个时候可能还有一部分人没有SpringBootTest或者还是注入失败,这个时候肯定是包没导进去,基本上就只有这几种问题了,还有问题欢迎大家评论留言