springboot整个缓存_SpringBoot整合Redis(二 ) springboot整合缓存redis

从上一篇文章 如何在Windows系统下安装redis服务中我们不难看出,redis是通过键值对的方式读取数据的,通过key找到对应的value值,此次springboot整合redis,讲的是如何通过key存值,如何通过key取值。

一、引入 springboot整合redis依赖配置。。一些多余的依赖配置,不需要的可以删,我这里是用来测试其他的功能,就不一一去删了

4.0.0

com.jspgou

springboot-redis

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

2.0.3.RELEASE

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

5.1.39

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

org.springframework.boot

spring-boot-starter-freemarker

org.springframework.boot

spring-boot-starter-log4j

1.3.8.RELEASE

org.springframework.boot

spring-boot-starter-aop

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.5

org.apache.commons

commons-lang3

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-mail

org.springframework.boot

spring-boot-starter-data-redis

二、设置redis连接信息,我这里的password获取不到,所以暂时在application.properties中配置了一个password

redis.yml

spring:

redis:

database: 1 #Redis数据库索引(默认为0)

host: 192.168.0.0.1 #Redis服务器地址

#Redis服务器连接端口

port: 6379

#Redis服务器连接密码

password: 12345

jedis:

pool:

max-active: 1000 #连接池最大连接数(只用负数值表示没有限制)

max-wait: -1 #连接池最大阻塞等待时间(只用负数值表示没有限制)

max-idle: 10 #连接池中的最大空闲连接

min-idle: 2 #连接池中的最小空闲连接

#连接超时时间(毫秒)

timeout: 0

application.properties

###JDBC

spring.datasource.url=jdbc:mysql://localhost:3306/springboot_wechat

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

logging.level.com.example.demo.dao=DEBUG

pagehelper.helperDialect=mysql

pagehelper.reasonable=true

pagehelper.supportMethodsArguments=true

pagehelper.params=count=countsql

pagehelper.page-size-zero=true

#redis密码

spring.redis.password=12345

三、工具类(将对象与json互相转换)

package redis.demo.util;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.JavaType;

import com.fasterxml.jackson.databind.ObjectMapper;

/**

* JSON工具类

* @author wulongwei

*

*/

public class JsonUtils {

// 定义jackson对象

private static final ObjectMapper MAPPER = new ObjectMapper();

/**

* 将对象转换成json字符串。

*

* @param data

* @return

*/

public static String objectToJson(Object data) {

try {

String string = MAPPER.writeValueAsString(data);

return string;

} catch (JsonProcessingException e) {

e.printStackTrace();

}

return null;

}

/**

* 将json结果集转化为对象

*

* @param jsonData

* @param beanType

* @return

*/

public static T jsonToPojo(String jsonData, ClassbeanType) {

try {

T t = MAPPER.readValue(jsonData, beanType);

return t;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 将json数据转换成pojo对象list

*

* @param jsonData

* @param beanType

* @return

*/

public static ListjsonToList(String jsonData, ClassbeanType) {

JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);

try {

Listlist = MAPPER.readValue(jsonData, javaType);

return list;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

用户信息实体类

package redis.demo.entity;

import lombok.Data;

@Data

public class User {

private String userName;

private Integer userAge;

private Short userSex;

private String city;

}

控制器层

package redis.demo.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import redis.demo.entity.User;

import redis.demo.util.JsonUtils;

@RestController

@RequestMapping("redis")

public class RedisController {

@Value("${spring.redis.password}")

private Integer password;

/**

* 向redis存入单个字符串

* @return

*/

@GetMapping("/test")

public String redisJsonResult() {

System.out.println(password);

redisTemplate.opsForValue().set("user-cache", "wulongwei");

return redisTemplate.opsForValue().get("user-cache");

}

/**

* 向redis存入一个对象

* @return

* @throws IOException

*/

//@PostMapping("/saveUser")

@GetMapping("/getUser")

public User saveUser() throws IOException {

User user = new User();

user.setCity("南昌");

user.setUserAge(22);

user.setUserSex((short)1);

user.setUserName("张三");

redisTemplate.opsForValue().set("json:user", JsonUtils.objectToJson(user));

return JsonUtils.jsonToPojo(redisTemplate.opsForValue().get("json:user"), User.class);

}

@Autowired

private StringRedisTemplate redisTemplate;

}

最后一个启动类

package redis.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

public class app {

public static void main(String[] args) {

SpringApplication.run(app.class, args);

}

}

结果:

测试接口一: 向redis存入单个字符串  (调用接口后刷新 redis)

测试接口二 : 向redis存入一个对象 (调用接口后刷新 redis)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值