这里,对于springboot和redis 不做介绍,大家可以自己去查找资料~~~~~~~~~~~
https://blog.csdn.net/WYpersist/article/details/81211345
话不多说,来就是开搞!
4.0.0
com.vk.ioc
vk-ioc-redis
0.0.1-SNAPSHOT
jar
vanke-ioc-redis
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
test
org.springframework.boot
spring-boot-starter-redis
1.3.8.RELEASE
mysql
mysql-connector-java
5.1.38
com.alibaba
fastjson
1.2.41
com.alibaba
druid
1.1.0
org.springframework.data
spring-data-redis
2.0.3.RELEASE
org.springframework.data
spring-data-redis
2.0.3.RELEASE
junit
junit
org.springframework.boot
spring-boot-test
org.springframework
spring-test
4.3.17.RELEASE
org.springframework.boot
spring-boot-maven-plugin
spring:
datasource:
# 驱动配置信息
url: jdbc:mysql://localhost:3306/sb?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
# 连接池的配置信息
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
redis:
jedis:
pool:
max-active: 100
max-wait: 10000
max-idle: 10
host: 127.0.0.1
port: 6379
password: 123456
timeout: 0
获取application.yml配置文件一些参数
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public JedisPoolConfig getRedisConf() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
return jedisPoolConfig;
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setUsePool(true);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate, ?> getRedisTemplate() {
JedisConnectionFactory factory = getConnectionFactory();
RedisTemplate, ?> template = new StringRedisTemplate(factory);
return template;
}
}
以上三个方法分别为获取JedisPoolConfig配置、获取JedisConnectionFactory工厂和获取RedisTemplate模板。
@Configuration 注解是用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
@EnableAutoConfiguration 注解是启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于类路径和定义的bean应用。
@ConfigurationProperties 注解是用于读取配置文件的信息,在这里是读取配置在yml里的redis的相关配置项。
@Bean 注解用在方法上,告诉Spring容器,你可以从下面这个方法中拿到一个Bean
在包里创建RedisService接口,在这个接口定义了一些redis的基本操作。在这里我把所有存取操作都封装成了基于json字符串完成,就没有对于list或者对于object等单独定义方法。所有的数据类型的存储都由代码转换成json字符串方式进行。所以这里就只有四个方法。
public interface RedisService {
/**
* set存数据
*
* @param key
* @param value
* @return
*/
boolean set(String key, String value);
/**
* get获取数据
*
* @param key
* @return
*/
String get(String key);
/**
* 设置有效天数
*
* @param key
* @param expire
* @return
*/
boolean expire(String key, long expire);
/**
* 移除数据
*
* @param key
* @return
*/
boolean remove(String key);
}
@Service
public class RedisServiceImpl implements RedisService {
@Autowired
private RedisTemplate redisTemplate;
@Override
public boolean set(final String key, final String value) {
// execute, 实现对于redis数据的操作
boolean result = redisTemplate.execute(new RedisCallback() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
//StringRedisSerializer来做序列化,不过这个方式的泛型指定的是String
// 只能传String进来。所以项目中采用json字符串做redis的交互。
RedisSerializer serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
@Override
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
@Override
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public boolean remove(final String key) {
boolean result = redisTemplate.execute(new RedisCallback() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer serializer = redisTemplate.getStringSerializer();
connection.del(key.getBytes());
return true;
}
});
return result;
}
}
execute()方法,这样能实现对于redis数据的操作。
redis保存的数据会在内存和硬盘上存储,所以需要做序列化;这个里面使用的StringRedisSerializer来做序列化,不过这个方式的泛型指定的是String,只能传String进来。所以采用json字符串做redis的交互。
class Person {
private String name;
private String sex;
public Person() {
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
/**
* 测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class VkeIocRedisApplicationTests {
//使用json字符串进行交互,引入fastjson的JSONObject类
private JSONObject json = new JSONObject();
// @Autowired注解把redisService注入进来
@Autowired
private RedisService redisService;
@Test
public void contextLoads() throws Exception {
}
/**
* 插入字符串
*/
@Test
public void setString() {
redisService.set("redis_string_test", "springboot redis test");
}
/**
* 获取字符串
*/
@Test
public void getString() {
String result = redisService.get("redis_string_test");
System.out.println(result);
}
/**
* 插入对象
*/
@Test
public void setObject() {
Person person = new Person("person", "male");
redisService.set("redis_obj_test", json.toJSONString(person));
}
/**
* 获取对象
*/
@Test
public void getObject() {
String result = redisService.get("redis_obj_test");
Person person = json.parseObject(result, Person.class);
System.out.println(json.toJSONString(person));
}
/**
* 插入对象List
*/
@Test
public void setList() {
Person person1 = new Person("person1", "male");
Person person2 = new Person("person2", "female");
Person person3 = new Person("person3", "male");
List list = new ArrayList<>();
list.add(person1);
list.add(person2);
list.add(person3);
redisService.set("redis_list_test", json.toJSONString(list));
}
/**
* 获取list
*/
@Test
public void getList() {
String result = redisService.get("redis_list_test");
List list = json.parseArray(result, String.class);
System.out.println(list);
}
@Test
public void remove() {
redisService.remove("redis_test");
}
}
使用json字符串进行交互,所以引入fastjson的JSONObject类
然后执行测试类,结果如下:
在redis可视化管理工具查看时间
································注意:执行测试的时候,先要启动redis服务-----------------------------------------------------------------
定位到 redis的目录下,如下,就是启动服务。
回车键
但是不要关闭窗口,关闭窗口就相当于关闭服务。
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
解决办法:https://blog.csdn.net/WYpersist/article/details/81233348
ok啦~~~~~~~~·溜溜了