springboot整合Redis 和Redis简单安装使用

安装Redis

https://redis.io/ 官网下载解压到指定目录就行
启动redis redis-server.exe redis.windows.conf
在这里插入图片描述
菜鸟教程网址 https://www.runoob.com/redis/redis-install.html

整合Redis

1.x版本修改配置和2.x版本十分不一样 1.就不记载了
2.x版本修改redis的配置类 比如序列化自定义http://shangshiwendao.com/article/88
配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=abcdef
mybatis.configuration.mapUnderscoreToCamelCase=true  #mybtis是否开启驼峰命名法


logging.level.mappers=debug


spring.redis.host=  127.0.0.1    #这个是redis地址
server.port=8080

示例使用

@Autowired
EmployeeMapper employeeMapper;


@Autowired
   StringRedisTemplate stringRedisTemplate;//因为reids经常处理spring所以产生的这个类  操作的k -v 是字符串


@Autowired
   RedisTemplate redisTemplate;  //redis操作类  操作的 k-v是 都是对象
   @Autowired
RedisTemplate<Object, Employee> empredisTemplate;//自定义的cache
/*
* redis 常见的五大类型
* string(字符串).  list(列表).   set(集合)   hash(散列).   zset(有序集合)
*
* */




void contextLoads() {
   //查出一个srping已拥有的key
   String mykey = stringRedisTemplate.opsForValue().get("mykey");
   System.out.println(mykey);
   //存入一个数据
   stringRedisTemplate.opsForValue().append("msg","hellow");
   //存入一个列表
   stringRedisTemplate.opsForList().leftPush("list","1");
   stringRedisTemplate.opsForList().leftPush("list","2");
   //查询一个列表
   BoundListOperations<String, String> list = stringRedisTemplate.opsForList().getOperations().boundListOps("list");
   System.out.println(list.toString());

}
@Test
void contextLoads2(){
   Employee employee = employeeMapper.getEmpById(1);
    //默认如果保存对象 我们回使用jdk默认序列化保存到redis
   //redisTemplate.opsForValue().set("employee",employee);
   //将数据以json的方式保存到redis
   //自己将对象保存为 json
   redisTemplate.opsForValue().set("emp01",employee);
}

Guess you like

Origin blog.csdn.net/weixin_43979902/article/details/119849865