首先要明白的是JPA不是产品,它是一个规范。

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

它的出现主要是为了简化现有的持久化开发工作和整合 ORM 技术,结束现在 Hibernate,TopLink,JDO 等 ORM 框架各自为营的局面

在pom.xml中添加引用:jpa和mysql

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在配置文件中添加数据库链接配置
链接字符串要指定时区和编码格式不然会导致中文乱码和报错

然后声明一个实体类,添加@Table注解制定表名,@Entity表明它是一个实体类Bean

@Id标明主键,@GeneratedValue标明主键类型,下图是mysql自增主键的例子

@Column指定映射的数据库列名,同名的话可以不写

创建一个接口继承JpaRepository,然后我们就可以使用框架为我们封装好的CURD了

调用接口看一下返回效果

剩下的删除修改就不贴了都是一样的。

接下来是JPA的查询,JPA会根据方法的名字来生成对应的查询

到控制器层试一下

调用一下接口,完美返回

删除跟新什么的都是这个套路,下面是一张对照表

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection age) … where x.age not in ?1
TRUE findByActiveTrue() … where x.active = true
FALSE findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

接下来是JPA的分页,JPA中自带了分页可以让我们直接使用

调用接口返回

开箱即用

下一章,我们讲集成Mybaits

SpringBoot系列:四、SpringBoot集成JPA的更多相关文章

  1. SpringBoot系列之Spring Data Jpa集成教程

    SpringBoot系列之Spring Data Jpa集成教程 Spring Data Jpa是属于Spring Data的一个子项目,Spring data项目是一款集成了很多数据操作的项目,其下 ...

  2. SpringBoot系列之i18n集成教程

    目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...

  3. springboot系列四:springboot整合mybatis jsp

    一.用IDEA 创建maven项目 项目目录结构 1.添加pom jar依赖 <?xml version="1.0" encoding="UTF-8"?& ...

  4. springboot系列四、配置模板引擎、配置热部署

    一.配置模板引擎 在之前所见到的信息显示发现都是以 Rest 风格进行显示,但是很明显在实际的开发之中,所有数据的显示最终都应该交由页面完成,但是这个页面并不是*.jsp 页面,而是普通的*.html ...

  5. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  6. SpringBoot和druid数据源集成Jpa

    1.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  7. Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)

    前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...

  8. SpringBoot学习(四)-->SpringBoot快速入门,开山篇

    Spring Boot简介 Spring Boot的目的在于创建和启动新的基于Spring框架的项目.Spring Boot会选择最适合的Spring子项目和第三方开源库进行整合.大部分Spring ...

  9. SPRING-BOOT系列之SpringBoot快速入门

    今天 , 正式来介绍SpringBoot快速入门 : 可以去如类似 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/refer ...

  10. SPRING-BOOT系列之SpringBoot的诞生及其和微服务的关系

    转载自 : https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法 ...

随机推荐

  1. Zookeeper — 应用场景

    大致来说,zookeeper 的使用场景如下,我就举几个简单的,大家能说几个就好了: 分布式协调 分布式锁 元数据/配置信息管理 HA高可用性 分布式协调 这个其实是 zookeeper 很经典的一个 ...

  2. wordpress开发的一些积累

    wordpress 攒知识点 记录开发 wordpress 的一些技能点,以备不时之需 短代码 Shortcode 虽然很多插件都是提供,直接在代码中插入类似[Shortcode] 便可以生效,但是很 ...

  3. Android--ViewPager点击按钮切换下一页

    不再重写一遍了,看csdn: https://blog.csdn.net/qq_42866164/article/details/101346058

  4. 一、.net Core bundleconfig.json

    一.bundleconfig.json [ { "outputFileName": "wwwroot/css/site.min.css", "inpu ...

  5. 012-linux系统管理——进程管理与工作管理

    linux系统管理——进程管理 top 命令是使用 top - :: up :, user, load average: 0.06, 0.60, 0.48 #五分钟钱,十分钟前,十五分钟前负载的值根据 ...

  6. PAT Advanced 1036 Boys vs Girls (25 分)

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  7. 使用pycharm编写自动化脚本

    1.:导入本地Python环境 2:安装Selenium 3:下来需要根据安装的浏览器版本,下载合适的chromedriver驱动程序. 4:接着需要将下载的chromedriver进行解压,并将文件 ...

  8. Codeforces 960 二进制构造子序列 完全二叉树shift模拟 主席树/MAP DP

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  9. 微信公众号接口类(PHP版本)

    [项目需求] 通过微信提供的接口,实现微信公众号与后端的应用程序数据交互.消息响应等功能. [项目疑难点] 理解接口工作方式,统一接口API,响应速度.安全性等   [代码举例]   WeixinAp ...

  10. Rails3:使用bundler管理gems

    Rails3:使用bundler管理gems bundler 是一套为了 Rails3 所打造的全新 Gem dependencies 管理工具:一套基于 Rubygems 的更高阶套件管理工具,适合 ...