springboot整合缓存Redis,java基础教程完整版pdf

spring.redis.pool.min-idle=2

连接超时时间(毫秒)

spring.redis.timeout=0

Controller

package com.yh.controller;

import java.util.ArrayList;

import java.util.List;

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

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

import org.springframework.stereotype.Controller;

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

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

import com.yh.pojo.TUser;

import com.yh.utils.JsonUtils;

import com.yh.utils.RedisOperator;

@Controller

@RequestMapping(value=“redis”)

public class RedisController {

@Autowired

private StringRedisTemplate stringRedisTemplate;

@Autowired

private RedisOperator reactor;

/**

  • redis

  • Title: getRedis

  • Description:

  • @return

*/

@RequestMapping(“getRedis”)

@ResponseBody

public TUser getRedis() {

/*

stringRedisTemplate.opsForValue().set(“hlv-y”, “hello yy”);

*/

/String redisRet =stringRedisTemplate.opsForValue().get(“hlv-y”);/

TUser tUser = new TUser( “heng”, “666666”, “1111”);

stringRedisTemplate.opsForValue().set(“json:tuser:list”,JsonUtils.objectToJson(tUser));

TUser tu =JsonUtils.jsonToPojo(stringRedisTemplate.opsForValue().get(“json:tuser:list”), TUser.class);

return tu;

}

}

JsonUtils

package com.yh.utils;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.JavaType;

import com.fasterxml.jackson.databind.ObjectMapper;

/**

  • @Title: JsonUtils.java

  • @Package com.lee.utils

  • @Description: 自定义响应结构, 转换类

*/

public class JsonUtils {

// 定义jackson对象

private static final ObjectMapper MAPPER = new ObjectMapper();

/**

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

  • Title: pojoToJson

  • Description:

  • @param data

  • @return

*/

public static String objectToJson(Object data) {

try {

String string = MAPPER.writeValueAsString(data);

return string;

} catch (JsonPr

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ocessingException e) {

e.printStackTrace();

}

return null;

}

/**

  • 将json结果集转化为对象

  • @param jsonData json数据

  • @param clazz 对象中的object类型

  • @return

*/

public static T jsonToPojo(String jsonData, Class beanType) {

try {

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

return t;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

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

  • Title: jsonToList

  • Description:

  • @param jsonData

  • @param beanType

  • @return

*/

public static List jsonToList(String jsonData, Class beanType) {

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

try {

List list = MAPPER.readValue(jsonData, javaType);

return list;

} catch (Exception e) {

e.printStackTrace();

上一篇:c – 如果我将片段着色器附加到程序,则出现错误1282


下一篇:MySQL笔记1