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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值