数据库
首页 > 数据库> > springboot整合缓存Redis,java基础教程完整版pdf

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;

/**

*/

@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;

/**

*/

public class JsonUtils {

// 定义jackson对象

private static final ObjectMapper MAPPER = new ObjectMapper();

/**

*/

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;

}

/**

*/

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

try {

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

return t;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

*/

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();

标签:TUser,return,springboot,java,json,import,完整版,com,public
来源: https://blog.csdn.net/m0_64383449/article/details/121573952