Git Product home page Git Product logo

spring-data-jpa-demo's Introduction

Spring Data Jpa 常用功能演示

项目简介

框架介绍

Spring Data JPASpring Data 系列的一部分, 使得轻松实现基于JPA的存储库变得更容易. 该模块处理对基于JPA的数据访问层的增强支持. 它使得更容易构建使用数据访问技术的Spring-powered应用程序. Spring Data JPA 的目标是通过让一些必须的工作变得更简单, 来显著提高数据访问层的实现. 作为一个开发者, 你写你的仓库界面, 包括自定义查询方法, 而 Spring 给你提供自动实现.

特点

  • 对建立基于Spring 和 JPA 的库的支持.
  • 支持 Querydsl predicates和安全类型的 JPA 查询.
  • Transparent auditing of domain class.
  • 分页支持, 动态查询执行, 可以集成自定义数据访问代码.
  • @Query 注明查询引导时验证.
  • 支持基于XML的实体映射.
  • 引入 @EnableJpaRepositories , 实现基于JavaConfig based仓库配置.

正式开始

准备工作

在开始提到, 本项目采用SpringBoot做基础架构支撑, 项目中使用 spring-data-jpa 的推荐方法是使用依赖关系管理系统,也就是我们平时所说的包管理器、构建工具.下面两种工具对应的代码段可以复制并粘贴到您的项目中. 如需帮助, 请参阅我们使用 MavenGradle 构建的入门指南.

Maven
<dependencies>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    ...
</dependencies>
Gradle
dependencies {
    ...
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    ...
}

Jpa提供的接口

为了使用方便, spring-data-jpa已经提供了多个接口, 只需要继承其中一个, spring-data-jpa将会自动实现并提供相应的实现. 继承/实现关系如下:

 QueryDslJpaRepository
 ┣━ QueryDslPredicateExecutor
 ┗━ SimpleJpaRepository
     ┣━ JpaSpecificationExecutor
     ┗━ JpaRepository
         ┣━ QueryByExampleExecutor
         ┗━ PagingAndSortingRepository
             ┗━ CrudRepository
                 ┗━Repository

这些 Repository 都是 spring-data-commons 提供给我们的核心接口, spring-data-commonsSpring Data 的核心包. 这个接口中为我们提供了数据的分页方法, 以及排序方法. spring-data 让我们省了很多心了, 一切都按照这个规范进行构造, 就连业务系统中常用到的一些操作都为我们考虑到了, 而我们只需更用心的去关注业务逻辑层. spring-datarepository 的颗粒度划得很细.

  • Repository:最基本的接口, 不提供任何功能, 仅作为 SpringData 的一个标记, 并提供实现.这个接口只是一个空的接口, 目的是为了统一所有 Repository 的类型, 其接口类型使用了泛型, 泛型参数中T代表实体类型, ID则是实体中id的类型. 任何直接或间接继承此接口的类或接口, 均会被Spring扫描到.
  • CrudRepository:提供最基本的增删改查方法.此接口中的方法大多是我们在访问数据库中常用的一些方法, 如果我们要写自己的DAO的时候, 只需定义个接口来集成它便可使用了.
  • PagingAndSortingRepository:提供基本的分页及排序功能, 并同时提供CrudRepository接口的功能.
  • QueryByExampleExecutor:提供查询功能, 后期会具体讲解.
  • JpaRepository:这个接口继承自 PagingAndSortingRepository , 里面的方法都是一些简单的操作, 并未涉及到复杂的逻辑. 当你在处理一些简单的数据逻辑时, 便可继承此接口.
  • JpaSpecificationExecutor:提供 criteria 查询, 排序、支持分页, 此接口没有父类(不包括 Object ), 即没有上级接口.
  • SimpleJpaRepository:实现 JpaRepositoryJpaSpecificationExecutor 接口, 使用 hibernateEntityManager做持久化相关处理. 您也可以更换为其他 JPA 实现, 如:EclipseLinkTopLink 等.
  • QueryDslPredicateExecutor:提供 Querydsl 查询的接口.
  • QueryDslJpaRepository:继承 SimpleJpaRepository 类, 实现 QueryDslPredicateExecutor 接口.

附录A:Github 常用按钮说明

  • Watch:关注该项目, 作者有更新的时候, 会在你的 Github 主页有通知消息.
  • Star:收藏该项目, 在你的头像上有一个 "Your stars" 链接, 可以看到你的收藏列表, 以方便下次进来.
  • Fork:复制一份项目到自己的 Github 空间上, 你可以自己开发自己的这个地址项目, 然后 Pull Request 给项目原主人.

联系方式

结束语

  • 真心感谢这些志同道合的人, 这个真的很重要, 也希望你能一起参与!
  • 同时感谢那些通过私聊或其他方式指出一些错误地方的朋友, 使得该教程能得以更加完善, 真心感谢!

spring-data-jpa-demo's People

Contributors

keveon avatar

Watchers

James Cloos avatar TanZhiqiang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.