Spring Data JPA 学习笔记

本文是关于Spring Data JPA的学习笔记,涵盖了从添加JPA支持到多表查询的全过程。文章介绍了如何配置JPA,定义数据库实体对象,以及创建数据库访问接口,包括自定义方法查询、SQL查询、排序、分页等功能。同时,还讨论了存储过程、数据库事务和多数据源配置等高级话题。
摘要由CSDN通过智能技术生成

Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.

Spring Data JPA 官方文档 / Java示例

下文是我学习过程中的笔记.

为工程添加JPA支持

使用spring data jpa需要在pom.xml增加依赖(以mysql为例)

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>6.0.6</version>
    </dependency>

然后再配置文件中增加如下配置

spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  datasource:
    url: jdbc:mysql://39.106.xxx.xxx:3306/eblog?useSSL=false&useUnicode=yes&characterEncoding=UTF-8
    username: root
    password: 123456xxxx
    driver-class-name: com.mysql.cj.jdbc.Driver

其中ddl-auto可以是以下5个值之一:

  • none : disable DDL handling (不根据实例定义生成数据库对象)
  • validate : validate the shcema, make no changes to database (检验,但不对数据库做任何改动)
  • update : update the schema if necessary (更新数据库对象)
  • create : create schema and destroy provious data (启动时销毁原先的数据对象,并创建新的)
  • create-drop : create and destroy the schema at the end of the session (创建并在应用停止时销毁,一般用于单元测试)

一个标准的spring boot工程结构如下采用这种目录结构符合spring boot的约定,可以节省配置 :

│  pom.xml
├─src
│  ├─main
│  │  ├─java
│  │  │  └─top
│  │  │      └─javawa
│  │  │          └─message
│  │  │              │      Application.java
│  │  │              │
│  │  │              ├─domain
│  │  │              │      AccessToken.java
│  │  │              │      AccessTokenRepository.java
│  │  │              │
│  │  │              ├─service
│  │  │              └─web
│  │  │                     TokenController.java
│  │  │
│  │  └─resources
│  │      │  bootstrap.yml
│  │      │  logback-spring.xml
│  │      │
│  │      ├─static
│  │      └─templates
│  └─test
│      ...

定义数据库实体对象

在domain包下创建POJO AccessToken

package top.javawa.message.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;

@Data
@Entity
public class AccessToken {
   
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;
  @Column(length = 32, unique = true, nullable = false)
  private String appId;
  @Column(length = 512)
  private String token;
  private long expiresIn;
}
  • @Data 是lombok的注解,可以自动生成getter/setter/toString… 等方法,简化java代码
  • @Entity 表示这是一个数据库表对象
  • @Id 表示主键
  • @GeneratedValue 表示这是一个生成列
    GenerationType有四个枚举值
    • TABLE 使用数据库表来生成唯一编号
    • SEQUENCE 使用数据库序列
    • IDENTITY 使用identity column(标识列)
    • AUTO 自动判断数据库支持那种方式,就以哪种方式来实现

注:我发现配置为AUTO 时,程序运行后产生了下表:

CREATE TABLE `hibernate_sequence` (
  `next_val` bigint(20) DEFAULT NULL
)

我猜测JPA使用了原子性的方法对该表的列进行自增来获取序号。

下面我将修改配置 ddl-auto: create 启动程序观察日志

 Hibernate: drop table if exists access_token
 Hibernate: drop table if exists hibernate_sequence
 Hibernate: create table access_token (id integer not null, app_id varchar(255), expires_in bigint not null, token varchar(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值