源码地址:https://gitee.com/ytfs-dtx/SpringBoot

Redis-Desktop-Manager2020地址:

https://ytsf.lanzous.com/b01bei1bc

密码:2qan

集成Spring Data JPA

添加Spring Data JPA的起步依赖

<!-- springBoot JPA的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

添加数据库驱动依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

在application.yml中配置数据库和JPA的相关属性

#DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
jpa:
database: mysql
show-sql: true
generate-ddl: false
hibernate:
ddl-auto: update

创建实体配置实体

package xyz.ytfs.entity;

import javax.persistence.*;

/**
* @author by 雨听风说
* @Classname User
* @Description TODO(用户的JavaBean)
* @Date 2020/5/13 17:09
*/ @Entity
@Table(name = "user")
public class User { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/*
@Column属性:当数据库的列明与实体类的属性名一致的时候可以不用配置
*/
@Column(name = "username")
private String username; @Column(name = "password")
private String password; @Column(name = "name")
private String name; //此处省略setter和getter方法... ...
}

编写测试类

package xyz.ytfs.test;

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.test.context.junit4.SpringRunner;
import xyz.ytfs.SpringBootJpaApplication;
import xyz.ytfs.repository.UserRepository; /**
* @author by 雨听风说
* @Classname JpaTest
* @Description TODO(JPA的测试)
* @Date 2020/5/13 17:18
*/ @RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootJpaApplication.class)
public class JpaTest { @Autowired
private UserRepository userRepository; @Test
public void testFindALl() {
this.userRepository.findAll().forEach(System.out::println);
} }

控制台打印信息

集成Redis--为了测试方便使用windows版的Redis

添加Redis起步依赖

<!-- 配置redis 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

启动redis服务器

配置Redis连接信息

#DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
jpa:
database: mysql
show-sql: true
generate-ddl: false
hibernate:
ddl-auto: update
#redis
redis:
port: 6399
host: 127.0.0.1

注入RedisTemplate测试redis

package xyz.ytfs.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
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 xyz.ytfs.SpringBootJpaApplication;
import xyz.ytfs.entity.User;
import xyz.ytfs.repository.UserRepository; import java.util.List; /**
* @author by 雨听风说
* @Classname RedisTest
* @Description TODO(Redis 测试)
* @Date 2020/5/13 20:55
*/ @RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootJpaApplication.class)
public class RedisTest {
/*
泛型代表的是key-value的类型
*/
@Autowired
private RedisTemplate<String, String> redisTemplate; @Autowired
private UserRepository repository; /*
jackson的序列化工具,springBoot自动集成redis
*/
private static final ObjectMapper MAPPER = new ObjectMapper(); @Test
public void test() throws JsonProcessingException { //1.从Redis中获取数据 数据格式json字符串
String usersJson = this.redisTemplate.boundValueOps("user.findAll").get(); //2.判断redis中书否存在这个数据
if (StringUtils.isBlank(usersJson)){ //3.不存在:数据库查询
List<User> users = this.repository.findAll(); //将查询出的数据存储到redis缓存中,先将users集合转换成json格式字符串 使用jackson进行转换
usersJson = MAPPER.writeValueAsString(users); //存储在redis中
this.redisTemplate.boundValueOps("user.findAll").set(usersJson);
System.out.println("---------------------------------------");
System.out.println("\"从数据库中查询数据\" = " + "从数据库中查询数据"); }else { System.out.println("\"从redis中获取数据\" = " + "从redis中获取数据");
} //4.存在:直接返回(测试直接在控制台打印)
System.out.println("usersJson = " + usersJson); } }

第一次执行

从数据库中获取数据

第二次执行

从redis中获取数据

打开redis可视化工具(redis desktop manager),发现键值已存在

springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows的更多相关文章

  1. springboot整合spring Data JPA

    今天敲代码,一连串的错误,我也是服气~果然,我们不是在出bug,就是在找bug的路上…… 今天完成的是springboot整合spring data JPA ,出了一连串的错,真是头大 java.sq ...

  2. springboot整合spring data jpa 动态查询

    Spring Data JPA虽然大大的简化了持久层的开发,但是在实际开发中,很多地方都需要高级动态查询,在实现动态查询时我们需要用到Criteria API,主要是以下三个: 1.Criteria ...

  3. spring-boot (三) spring data jpa

    学习文章来自:http://www.ityouknow.com/spring-boot.html spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence ...

  4. Spring Boot从入门到精通(九)整合Spring Data JPA应用框架

    JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...

  5. Spring Boot 整合Spring Data JPA

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...

  6. SpringBoot第九篇:整合Spring Data JPA

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10910059.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   前面几章, ...

  7. Spring Boot:整合Spring Data JPA

    综合概述 JPA是Java Persistence API的简称,是一套Sun官方提出的Java持久化规范.其设计目标主要是为了简化现有的持久化开发工作和整合ORM技术,它为Java开发人员提供了一种 ...

  8. springboot集成Spring Data JPA数据查询

    1.JPA介绍 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.它的出现主要是为 ...

  9. Spring Boot整合Spring Data JPA

    1.JPA 2.Spring Data JPA 3.导入依赖 4.连接数据库 5.实体类 6.Repository 7.测试 1.JPA JPA是Java Persistence API的简称,中文名 ...

  10. 整合Spring Data JPA与Spring MVC: 分页和排序

    之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...

随机推荐

  1. Android | 教你如何在安卓上实现二代身份证识别,一键实名认证

    @ 目录 前言 场景 开发前准备 android studio 安装 在项目级gradle里添加华为maven仓 在应用级的build.gradle里面加上SDK依赖 在AndroidManifest ...

  2. Spring Cloud 系列之 Sleuth 链路追踪(一)

    随着微服务架构的流行,服务按照不同的维度进行拆分,一次请求往往需要涉及到多个服务.互联网应用构建在不同的软件模块集上,这些软件模块,有可能是由不同的团队开发.可能使用不同的编程语言来实现.有可能布在了 ...

  3. python学习笔记(五)---函数与类

    函数 def为定义函数的一个标志 demo1: def greet_user(username): print("Hello, " + username.title() + &qu ...

  4. beego微信网页授权

    beego.Get("MP_verify_Rj3QAbcdU0MRsyS.txt", func(context *context.Context) { context.Respon ...

  5. python学习03字符串基本操作

    '''字符串可以用单引号,双引号,三引号表示 '''#1.读取str1='I am a student!'#每一个字符对应一个下标,可以利用下标的方式来读取字符串对应的值——索引print(str1[ ...

  6. Spring5参考指南:Environment

    文章目录 Profiles PropertySource 使用@PropertySource Spring的Environment接口有两个关键的作用:1. Profile, 2.properties ...

  7. Python2 和 Python3的区别

    Python2 和 Python3的区别: 1.python2的默认编码方式是ascii码:python3的默认编码是utf-8. 如果出现乱码或者编码错误,可以使用以下编码在文件头来指定编码: #- ...

  8. F查询,Q查询,事物,only与defer

    F查询 之前的单表查询多表查询筛选条件都是一个固定的值,那么如何用字段来筛选呢? 比如 :           查询卖出数大于库存数的商品 这个时候我们就可以用到django里面的F查询了 查询示例表 ...

  9. Java和php中的try-catch分析

    为什么80%的码农都做不了架构师?>>>   描述:对一个健壮的系统来讲,异常处理是必不可少的一部分,针对异常的管理,主要就是异常的捕获和处理操作,然而在php中使用try-catc ...

  10. webpack4.x下babel的安装、配置及使用

    前言 目前,ES6(ES2015)这样的语法已经得到很大规模的应用,它具有更加简洁.功能更加强大的特点,实际项目中很可能会使用采用了ES6语法的模块,但浏览器对于ES6语法的支持并不完善.为了实现兼容 ...