创建项目时需要导入的包
在application.yml 配置文件中配置需要的
spring:datasource:url: jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=Asia/Shanghaiusername: 用户名password: 密码jpa:show-sql: truehibernate:ddl-auto: none
#redis 可以不配,默认就是locatihostredis:host: localhostport: 6379cache:type: redis
创建一个实体类
package com.lanou3g.redisdemo.entity;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 创建人: 武奇* 创建事件: 2019/6/25*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Emp {private Long uuid;private String username;private String email;}
在test 测试类中与redis交互
package com.lanou3g.redisdemo;import com.lanou3g.redisdemo.entity.Emp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;
import java.util.Collections;@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisDemoApplicationTests {// 如果没有Spring, 并且还想使用Java操作Redis// 那么需要引入Jedis框架@Resourceprivate RedisTemplate<String, Object> redisTemplate;@Testpublic void contextLoads() {// RedisTemplate里面的方法都是全局指令// 这些指令是与数据类型无关的// 比如: 删除key, 设置过期时间, 判断key是否存在等等this.redisTemplate.keys("school").forEach(System.out::println);//判断key是否存在Boolean boo = this.redisTemplate.hasKey("school");System.out.println(boo ? "key存在" : "key不存在");//删除Boolean b = this.redisTemplate.delete("school");System.out.println(b ? "删除成功" : "删除失败");}@Testpublic void testString() {// 操作String类型的数据
// 添加 ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();ops.set("ppppp111888", "蓝鸥3G");
// 序列化转换RedisSerializer<?> keySerializer = this.redisTemplate.getKeySerializer();System.out.println(keySerializer.getClass());}@Testpublic void saveObj() {Emp emp = Emp.builder().uuid(1001L).username("zhangsan").email("zhang3@qq.com").build();
//存储对象时必须使这个对象序列化,我们要配置beanValueOperations<String, Object> ops = this.redisTemplate.opsForValue();ops.set("emp", emp);}@Testpublic void readObj() {ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();Emp emp = (Emp) ops.get("emp");System.out.println(emp);}}
配置bean
package com.lanou3g.redisdemo.config;import org.springframework.cache.annotation.EnableCaching;
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.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import javax.annotation.PostConstruct;
import javax.annotation.Resource;@Configuration
@EnableCaching// 启用缓存
public class RedisConfig {// 配置key的序列化@Beanpublic RedisSerializer keySerializer() {return new StringRedisSerializer();}// 配置value的序列化@Beanpublic RedisSerializer valueSerializer() {// 当向Redis中存储一个对象时候,// 会把对象转换为json数据存储到Redis中return new GenericJackson2JsonRedisSerializer();}//@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(keySerializer());redisTemplate.setHashKeySerializer(keySerializer());// 如果不配置JSON的序列化, 还想保存对象,// 那么前提是这个对象是可以被序列化的, 也就是说// 对应的类必须是实现Serializable接口的// 如果需要使用JSON的序列化, 被保存的对象,// 必须得有默认的构造方法, 否则对象能被存上, 但是读不出来redisTemplate.setValueSerializer(valueSerializer());redisTemplate.setHashValueSerializer(valueSerializer());return redisTemplate;}//实体类配置Bean 与配置文件配置没有任何差异/*<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/><bean id="redisTemplate" class="com.xxx.RedisTemplate"><property key="connectionFactory" ref="redisConnectionFactory"/><property key="keySerializer" ref="keySerializer" /><property key="hashKeySerializer" ref="keySerializer" /></bean>*/}