源码地址: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. 带权值的图 BFS

    用bfs遍历最图求最短路径时通常借用优先队列即优先考虑最大的或者最小的权值 方法1 优先队列:(内置函数,优先考虑较小的权值) #include<iostream> #include< ...

  2. Reward 杭电 2647

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  3. springboot集成JdbcTemplate+druid

    application.yml datasource: username: root password: root url: jdbc:mysql://localhost:3306/early_war ...

  4. 面试官:兄弟,说说Java到底是值传递还是引用传递

    二哥,好久没更新面试官系列的文章了啊,真的是把我等着急了,所以特意过来催催.我最近一段时间在找工作,能从二哥的文章中学到一点就多一点信心啊! 说句实在话,离读者 trust you 发给我这段信息已经 ...

  5. [html]浏览器标签小图标LOGO简单设置

    方式一:如果是一个项目一个LOGO 的话,直接接把图片像素设置成16x16像素,然后改名favicon.ico放在项目根部目录就可以,自动识别的! 方式二:简单设置! 首先找一个图片把像素设置成16x ...

  6. golang方法详解

    Go 语言 类型方法是一种对类型行为的封装 .Go 语言的方法非常纯粹, 可以看作特殊类型的函数,其显式地将对象实例或指针作为函数的第一个参数,并且参数可以自己指定,而不强制要求一定是 this或se ...

  7. py安装教程

    https://www.runoob.com/w3cnote/pycharm-windows-install.html

  8. 5、flink常见函数使用及自定义转换函数

    代码地址:https://gitee.com/nltxwz_xxd/abc_bigdata 一.flink编程方法 获取执行环境(execution environment) 加载/创建初始数据集 对 ...

  9. linux--配置开发环境 --Apache篇

    现在我的的linux服务器上一般都是使用:Apache 和  Nginx 这两种配置. 你现在安装好了,启动了,也无法通过你服务器绑定的网址访问你的网站. 这是你可以通过这个命令查看一下你的80端口: ...

  10. python学习05条件分支

    '''if '''a=1b=2if a==b: print(a)print(b) '''与C语言不同,python语言的if格式必须为if 布尔表达式:(冒号不能省略)其二,python有严格的缩进格 ...