Spring Data JPA简介

用来简化创建 JPA 数据访问层和跨存储的持久层功能。

Spring Data JPA提供的接口

Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组件扫描的时候自动识别。

CrudRepository :是Repository的子接口,提供CRUD的功能。

PagingAndSortingRepository:是CrudRepository的子接口,添加分页和排序的功能。

JpaRepository:是PagingAndSortingRepository的子接口,增加了一些实用的功能,比如:批量操作等。

JpaSpecificationExecutor:用来做负责查询的接口。

Specification:是Spring Data JPA提供的一个查询规范,要做复杂的查询,只需围绕这个规范来设置查询条件即可。

Repository接口查询规则

关键字 @ 案例 @效果

 And @ findByLastnameAndFirstname @ … where x.lastname = ?1 and x.firstname = ?2Or @ findByLastnameOrFirstname @ … where x.lastname = ?1 or x.firstname = ?2
Is,Equals@findByFirstname,findByFirstnameIs,findByFirstnameEquals@… where x.firstname = ?1Between@findByStartDateBetween@… where x.startDate between ?1 and ?2LessThan@findByAgeLessThan@… where x.age < ?1LessThanEqual @findByAgeLessThanEqual@… where x.age <= ?1GreaterThan @findByAgeGreaterThan@… where x.age > ?1GreaterThanEqual @findByAgeGreaterThanEqual@… where x.age >= ?1After @findByStartDateAfter@… where x.startDate > ?1Before@findByStartDateBefore@… where x.startDate < ?1IsNull@findByAgeIsNull@… where x.age is nullIsNotNull,NotNull@findByAge(Is)NotNull@… where x.age not nullLike @findByFirstnameLike@… where x.firstname like ?1NotLike @findByFirstnameNotLike@… where x.firstname not like ?1StartingWith@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 descNot@findByLastnameNot@… where x.lastname <> ?1In @findByAgeIn(Collection<Age> ages)@… where x.age in ?1NotIn @findByAgeNotIn(Collection<Age> age)@… where x.age not in ?1TRUE@findByActiveTrue()@… where x.active = trueFALSE @findByActiveFalse()@… where x.active = falseIgnoreCase@findByFirstnameIgnoreCase@… where                    UPPER(x.firstame) = UPPER(?1)

项目图片

pom.xml

只需要在pom.xml引入需要的数据库配置,就会自动访问此数据库,如果需要配置其他数据库,可以在application.properties进行添加

默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jege.spring.boot</groupId><artifactId>spring-boot-data-jpa</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-boot-data-jpa</name><url>http://maven.apache.org</url><!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><!-- 持久层 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- h2内存数据库 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><finalName>spring-boot-data-jpa</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build>
</project>

模型对象User

package com.jege.spring.boot.data.jpa.entity;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;/*** @jpa模型对象*/
@Entity
@Table(name = "t_user")
public class User {@Id@GeneratedValueprivate Long id;private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name = name;this.age = age;}}

持久层UserRepository

package com.jege.spring.boot.data.jpa.repository;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import com.jege.spring.boot.data.jpa.entity.User;/***持久层接口,由spring自动生成其实现*/
public interface UserRepository extends JpaRepository<User, Long> {List<User> findByNameLike(String name);}

启动类Application

package com.jege.spring.boot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** spring boot 启动类*/@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

配置文件application.properties

## JPA Settings
spring.jpa.generate-ddl: true
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: create
spring.jpa.properties.hibernate.format_sql: false

测试类UserRepositoryTest

package com.jege.spring.boot.data.jpa;import static org.assertj.core.api.Assertions.assertThat;import org.junit.After;
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.SpringJUnit4ClassRunner;import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository;@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class UserRepositoryTest {@AutowiredUserRepository userRepository;// 打印出class com.sun.proxy.$Proxy66表示spring注入通过jdk动态代理获取接口的子类@Testpublic void proxy() throws Exception {System.out.println(userRepository.getClass());}@Testpublic void save() throws Exception {for (int i = 0; i < 10; i++) {User user = new User("jege" + i, 25 + i);userRepository.save(user);}}@Testpublic void all() throws Exception {save();assertThat(userRepository.findAll()).hasSize(10);}@Testpublic void findByName() throws Exception {save();assertThat(userRepository.findByNameLike("jege%")).hasSize(10);}@Afterpublic void destroy() throws Exception {userRepository.deleteAll();}}

扫一扫获取更多相关资讯哟!!!

Spring Boot Data JPA相关推荐

  1. spring boot 系列之四:spring boot 整合JPA

    上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...

  2. Spring Boot集成JPA的Column注解命名字段无效的问题

    偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...

  3. 使用PostgreSQL使用Spring Boot和JPA构建基本应用

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 每个不平 ...

  4. Spring Boot&JPA&Hibernate&Oracle

    在本教程中,我们将展示如何创建一个Spring Boot应用程序,该应用程序通过Hibernate与Oracle数据源进行通信. 先决条件: Eclipse IDE(最新版本) Maven的4 Jav ...

  5. Spring Boot基础学习笔记07:Spring Boot整合JPA

    文章目录 零.学习目标 1.熟悉Spring Data JPA基本语法和使用 2.掌握Spring Boot与JPA的整合使用 一.Spring Data JPA概述 1.Spring Data JP ...

  6. Spring boot整合jpa Jquery实现三级联动

    Spring boot 整合jpa JQuery 实现省,市,区, 三级联动效果 三级联动在很多项目都必不可少,尤其是在付款时,需要选取地址,为了更好的用户体验感,从而出现了三级联动. 实现三级联动的 ...

  7. Spring Boot整合JPA和人大金仓(Kingbase8)数据库

    Spring Boot整合JPA和人大金仓(Kingbase8)数据库 简介 在开发Java应用程序时,使用JPA(Java Persistence API)可以方便地进行数据库操作.而人大金仓(Ki ...

  8. Spring Boot整合Jpa多数据源

    Spring Boot整合Jpa多数据源 本文是Spring Boot整合数据持久化方案的最后一篇,主要和大伙来聊聊Spring Boot整合Jpa多数据源问题.在Spring Boot整合JbdcT ...

  9. Spring Boot入门——JPA

    JPA最大的特点就是可以根据@Entity自动创建你数据库表,用户只需要声明持久层的接口,不需要实现该接口 1.JPA概念 JPA全称Java Persistence API,JPA通过JDK5.0注 ...

最新文章

  1. 企业OKR实施失败一定要小心这三大杀手
  2. spring mvc学习(8):springmvc常用注解代码
  3. PHP注入漏洞(附代码,具体步骤)
  4. .Net MVC中设置默认启动为某区域的视图
  5. 苹果手机如何查看html代码,苹果手机怎么利用代号查看硬件信息
  6. 更新一波,特殊福利 !
  7. Study「Word2016」:论文公式编辑时,编号右对齐
  8. 在线更换背景网站(白色背景换为蓝色背景证件照)
  9. 二维码的生成细节和原理源码
  10. 【智能路由器】openwrt添加服务项
  11. 自动化测试遇到的难点_自动化测试不成功的原因和实施过程中存在的问题
  12. 锐捷睿易:配置SSH登录
  13. TS+M3U8+directshow流媒体播放器 简介
  14. Windows server 2012 服务器挂载NAS盘
  15. win10xp化折腾指南
  16. Unity Joystick手势操作
  17. 忘记各种电脑密码的解决办法
  18. uni-app 聊天对话滚动到最底部
  19. 服务器系统装QC软件,QC安装部署
  20. P3166 [CQOI2014]数三角形

热门文章

  1. java中的输入语句判断正负_在java中使用方法调用统计数组中正数的个数,将判断数据的正负功能定义成方法...
  2. win10系统预览体验计划错误代码0x800bfa19怎么办
  3. PP视频怎么查看云钻的兑换记录呢
  4. tomcat 配置https安全认证协议报错解决方案
  5. matlab程序求尖锐度,业务名称
  6. java面试题:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
  7. Java番外篇1——正则表达式
  8. mysql++多版本安装_MySQL多版本多实例安装启动
  9. html 输入框 相加,JS中,如何实现两个输入框中内容的数字相加?
  10. 方正高影仪安装方法_铝合金门窗是怎么安装的?