JPA简介

Spring Data JPA是Spring Data大家族的一部分,它可以轻松实现基于JPA的存储库。该模块用于增强支持基于JPA的数据访问层,它使我们可以更加容易地构建使用数据访问技术的Spring驱动的应用程序。

对于普通的开发者而言,自己实现应用程序的数据访问层是一件很麻烦的时间,开发者必须编写大量样板代码来执行简单的查询以及执行分页和统计,Spring Data JPA旨在通过将工作量减少到实际需要的程度来显著改进数据访问层的实现。作为开发人员,我们只需要编写存储库接口(Repository接口),包括自定义查询方法,其余的工作,Spring将自动帮我们完成。

JPA特性

  • 对基于Spring和JPA的存储库构建的完善支持。
  • 支持Querydsl查询框架,从而支持类型安全的JPA查询。
  • 域类的透明审计。
  • 具备分页支持、动态查询执行、集成自定义数据访问代码的能力。
  • 在启动时验证带@Query注解的查询。
  • 支持基于XML的实体映射。
  • 通过引入@EnableJpaRepositories注解来实现基于JavaConfig的存储库配置。

SpringBoot整合JPA

(1)添加Spring Data JPA依赖启动器

引入这两个依赖器创建项目,在项目pom.xml文件会出现以下依赖:

(2)编写ORM实体类

package com.hardy.springbootdatajpa.entity;

import javax.persistence.*;

/**
* @Author: HardyYao
* @Date: 2021/6/13
*/
@Entity(name = "t_comment") // 设置ORM实体类,并指定映射的表名
public class Comment { @Id // 映射对应的主键id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 设置主键自增策略
private Integer id; private String content; private String author; @Column(name = "a_id") // 指定映射的表字段名
private Integer aId; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public Integer getaId() {
return aId;
} public void setaId(Integer aId) {
this.aId = aId;
} @Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
}

(3)编写Repository接口

package com.hardy.springbootdatajpa.repository;

import com.hardy.springbootdatajpa.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository; /**
* @Author: HardyYao
* @Date: 2021/6/13
*/
public interface CommentRepository extends JpaRepository<Comment,Integer> { }

(4)编写配置文件

# MySQL数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

(5)测试

编写测试方法:

package com.hardy.springbootdatajpa;

import com.hardy.springbootdatajpa.entity.Comment;
import com.hardy.springbootdatajpa.repository.CommentRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import java.util.Optional; @SpringBootTest
class SpringbootdataJpaApplicationTests { @Autowired
private CommentRepository repository; @Test
public void selectComment() {
Optional<Comment> optional = repository.findById(1);
if (optional.isPresent()) {
System.out.println(optional.get());
}
System.out.println();
} }

打印测试结果:

SpringBoot数据访问(二) SpringBoot整合JPA的更多相关文章

  1. SpringBoot数据访问之整合mybatis注解版

    SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...

  2. SpringBoot数据访问之Druid启动器的使用

    数据访问之Druid启动器的使用 承接上文:SpringBoot数据访问之Druid数据源的自定义使用 官方文档: Druid Spring Boot Starter 首先在在 Spring Boot ...

  3. SpringBoot的学习二:整合Redis,JPA,Mybatis

    Redis介绍: 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API 特性: Redis 与其他 key - value 缓 ...

  4. SpringBoot数据访问(一) SpringBoot整合Mybatis

    前言 SpringData是Spring提供的一个用于简化数据库访问.支持云服务的开源框架.它是一个伞形项目,包含了大量关系型数据库及非关系型数据库的数据访问解决方案,其设计目的是为了使我们可以快速且 ...

  5. SpringBoot数据访问(三) SpringBoot整合Redis

    前言 除了对关系型数据库的整合支持外,SpringBoot对非关系型数据库也提供了非常好的支持,比如,对Redis的支持. Redis(Remote Dictionary Server,即远程字典服务 ...

  6. Springboot数据访问,棒棒哒!

    Springboot对数据访问部分提供了非常强大的集成,支持mysql,oracle等传统数据库的同时,也支持Redis,MongoDB等非关系型数据库,极大的简化了DAO的代码,尤其是Spring ...

  7. springboot 数据访问【转】【补】

    六.SpringBoot与数据访问 1.JDBC pom.xml配置 <dependencies> <dependency> <groupId>org.spring ...

  8. springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件

    整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的 ...

  9. SpringBoot数据访问之Druid数据源的使用

    数据访问之Druid数据源的使用 说明:该数据源Druid,使用自定义方式实现,后面文章使用start启动器实现,学习思路为主. 为什么要使用数据源: ​ 数据源是提高数据库连接性能的常规手段,数据源 ...

随机推荐

  1. L SERVER 数据库被标记为“可疑”的解决办法

    问题背景: 日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停 ...

  2. 网络请求axios

    axios的定义 axios是一个基于Promise,用于浏览器和node的HTTP客户端 axios的功能特点 在浏览器中发送 XMLHttpRsquests 请求 在node.js中发送http请 ...

  3. Java中浮点数的坑

    基本数据类型 浮点数存在误差 浮点数有一个需要特别注意的点就是浮点数是有误差的,比如以下这段代码你觉得输出的什么结果: public class Demo { public static void m ...

  4. [刷题] 75 Sort Colors

    要求 给只有0 1 2三个元素的数组排序 思路 方法1:遍历数组,利用辅助数组保存三个元素的个数,再写入(遍历两遍) 辅助数组有三个元素,对应0 1 2的个数 方法2:模拟三路快排,遍历一遍完成排序 ...

  5. CentOS 7配置静态IP地址的两种方法 来自:互联网

    CentOS 7配置静态IP地址的两种方法 来自:互联网 时间:2021-01-12 阅读:4 如果你想要为CentOS 7中的某个网络接口设置静态IP地址,有几种不同的方法,这取决于你是否想要使用网 ...

  6. 【转载】打造基于 Centos 7.X 的 spice 服务器

    [转载]打造基于 Centos 7.X 的 spice 服务器 https://segmentfault.com/a/1190000011991047

  7. Https实践

    https实践 常用端口 ssh 22 telnet 23 ftp 21 rsync 873 http 80 mysql 3306 redis 6379 https 443 dns 53 php 90 ...

  8. Linux进阶之正则,shell三剑客(grep,awk,sed),cut,sort,uniq

    一.正则表达式:Regular Expression 正则表达式:正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索.替换那些符合某个模式 ...

  9. 记一次MySQL(5.7版本)数据库的主从同步和备份

    我遇到的问题 我先后在BAT三大云服务器商购买了学生机,配置如下 百度云2核/4G 阿里云1核/2G 腾讯云1核/2G 我的解决方案 由于我不知道百度云的续费规则,导致买了2核/4G的服务器之后以为像 ...

  10. week-03

    1.简述HTTP交互原理 1.浏览器分析输入访问的地址 域名(IP)+$uri 2.读取浏览器缓存 3.请求DNS服务器,解析域名,返回IP 4.建立TCP连接,三次握手 5.发送请求 6.接收返回请 ...