Spring Boot整合redis

一、添加依赖

1<!--SpringBoot整合redis的依赖--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-redis</artifactId> 5 </dependency>

二、在配置文件中添加redis配置

1spring: 2 datasource: 3 driver-class-name: com.mysql.jdbc.Driver 4 url: jdbc:mysql://127.0.0.1:3306/springboot 5 username: root 6 password: ROOT 7 8 redis: 9 database: 0 10 host: 192.168.81.10 11 password: offcn 12 port: 6379 13 14 jpa: 15 database: mysql 16 show-sql: true 17 generate-ddl: true

三、编写测试类

1package com.offcn.boot; 2import com.offcn.HelloApplication; 3import com.offcn.pojo.MUser; 4import org.junit.Test; 5import org.junit.runner.RunWith; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.boot.test.context.SpringBootTest; 8import org.springframework.data.redis.core.RedisTemplate; 9import org.springframework.test.context.junit4.SpringRunner; 10 11import java.util.List; 12 13@RunWith(SpringRunner.class) 14@SpringBootTest(classes = HelloApplication.class) 15public class TestApp { 16 17 @Autowired 18 private RedisTemplate redisTemplate; 19 20 @Test 21 public void test02(){ 22 redisTemplate.opsForValue().set("user","xiaoming"); 23 String s = (String)redisTemplate.opsForValue().get("user"); 24 System.out.println(s); 25 26 MUser mUser = new MUser(); 27 mUser.setId(5); 28 mUser.setUsername("xiaohong"); 29 mUser.setAge(22); 30 redisTemplate.opsForValue().set("muser",mUser); 31 MUser muser = (MUser)redisTemplate.opsForValue().get("muser"); 32 System.out.println(muser); 33 34 } 35}

四、错误

通过RedisTemplate往redis中存入对象时,对象必须要实现序列化接口

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

SpringBoot持久层技术

一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId

SpringBoot持久层技术

一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId

Spring Boot整合redis - HelloWorld