栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot-学习笔记05【SpringBoot集成】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot-学习笔记05【SpringBoot集成】

  • Java后端 学习路线 笔记汇总表【黑马程序员】
  1. SpringBoot-学习笔记01【SpringBoot介绍】
  2. SpringBoot-学习笔记02【SpringBoot入门】
  3. SpringBoot-学习笔记03【SpringBoot原理分析】
  4. SpringBoot-学习笔记04【SpringBoot配置文件】
  5. SpringBoot-学习笔记05【SpringBoot集成】

目录

23-SpringBoot集成其他技术-集成Mybatis1

24-SpringBoot集成其他技术-集成Mybatis2

25-SpringBoot集成其他技术-集成Junit

26-SpringBoot集成其他技术-集成SpringDataJPA

27-SpringBoot集成其他技术-集成Redis


23-SpringBoot集成其他技术-集成Mybatis1

24-SpringBoot集成其他技术-集成Mybatis2

25-SpringBoot集成其他技术-集成Junit

26-SpringBoot集成其他技术-集成SpringDataJPA

27-SpringBoot集成其他技术-集成Redis

package com.itheima;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaApplication.class)
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserRepository userRepository;

    @Test
    public void test() throws JsonProcessingException {
        //1、从redis中获得数据,数据的形式json字符串
        String userListJson = redisTemplate.boundValueOps("user.findAll").get();
        //2、判断redis中是否存在数据
        if (null == userListJson) {
            //3、不存在数据 从数据库查询
            List all = userRepository.findAll();
            //4、将查询出的数据存储到redis缓存中
            //向将list集合转换成json格式的字符串  使用jackson进行转换
            ObjectMapper objectMapper = new ObjectMapper();
            userListJson = objectMapper.writeValueAsString(all);
            redisTemplate.boundValueOps("user.findAll").set(userListJson);
            System.out.println("=======从数据库中获得user的数据======");
        } else {
            System.out.println("=======从redis缓存中获得user的数据======");
        }
        //4、将数据在控制台打印
        System.out.println(userListJson);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880459.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号