前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot整合Spring Data JPA

SpringBoot整合Spring Data JPA

作者头像
烂猪皮
发布2023-09-04 11:10:06
2340
发布2023-09-04 11:10:06
举报
文章被收录于专栏:JAVA烂猪皮JAVA烂猪皮

本篇要点

  • 简单介绍JPA。
  • 介绍快速SpringBoot快速整合JPA

JPA是啥?

The Java Persistence API is a standard technology that lets you “map” objects to relational databases. The spring-boot-starter-data-jpa POM provides a quick way to get started.

  • JPA是The Java Persistence API标准,Java持久层API,是一种能让对象能够快速映射到关系型数据库的技术规范。
  • JPA只是一种规范,它需要第三方自行实现其功能,在众多框架中Hibernate是最为强大的一个。

Spring Data JPA

Spring Data JPA 是采用基于JPA规范的Hibernate框架基础下提供了Repository层的实现。Spring Data Repository极大地简化了实现各种持久层的数据库访问而写的样板代码量,同时CrudRepository提供了丰富的CRUD功能去管理实体类。SpringBoot框架为Spring Data JPA提供了整合,spring-boot-starter-data-jpa能够让你快速使用这门技术,它提供了以下依赖。

  • Hibernate:最流行的JPA实现之一。
  • Spring Data JPA:帮助你去实现JPA-based repositories。
  • Spring ORM:Spring Framework提供的核心ORM支持。

快速SpringBoot快速整合JPA

引入依赖

代码语言:javascript
复制
        <!--SpringBoot对jpa的封装-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--mysql驱动,8.x版本-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

配置yml

代码语言:javascript
复制
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jpa?serverTimezone=GMT%2B8
    username: root
    password: 123456
    hikari:
      maximum-pool-size: 20
      minimum-idle: 5
  jpa:
    #在建表的时候,将默认的存储引擎切换为 InnoDB
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    # 配置在日志中打印出执行的 SQL 语句信息。
    show-sql: true
    # 配置指明在程序启动的时候要删除并且创建实体类对应的表。
    hibernate:
      ddl-auto: create #update

值得注意的是:spring.jpa.hibernate.ddl-auto第一建表的时候可以create,指明在程序启动的时候要删除并且创建实体类对应的表。后续使用就需要改为update。

ddl-auto的几种属性值
  • create:每次加载hibernate时都会删除上一次的生成的表,再重新根据model生成表,因此可能会导致数据丢失。
  • create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除
  • update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,原有数据不会清空,只会更新。
  • validate :每次加载hibernate时,会校验数据与数据库的字段类型是否相同,字段不同会报错

实体类

JPA规范定义在javax.persistence包下,注意导包的时候不要导错。

代码语言:javascript
复制
@Entity(name = "t_user")
@Data
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;
    private String password;


    @Transient
    private String email;

}
  • @Entity标注保证实体能够被SpringBoot扫描到,对应表名为t_user
  • @Id表明id。
  • @GeneratedValue中标注主键生成策略。
  • @Transient表示不需要映射的字段。
常见的主键生成策略
  • TABLE: 使用一个特定的数据库表格来保存主键
  • SEQUENCE: 根据底层数据库的序列来生成主键,条件是数据库支持序列。这个值要与generator一起使用,generator 指定生成主键使用的生成器(可能是orcale中自己编写的序列)。
  • IDENTITY: 主键由数据库自动生成(主要是支持自动增长的数据库,如mysql)
  • AUTO: 主键由程序控制,也是GenerationType的默认值。

启动项目,生成表

首先在数据库中创建jpa库,库名无所谓,和配置对应上就可以。

启动项目,你会发现控制台输出日志如下:

代码语言:javascript
复制
Hibernate: drop table if exists t_user
Hibernate: create table t_user 
    (id bigint not null auto_increment, password varchar(255), username varchar(255), primary key (id)) engine=InnoDB

此时我们配置的create效果已经显现,我们之后将它改为update,不然每次启动程序,数据表又得重建咯。

数据访问层

Working with Spring Data Repositories

Spring Data JPA repositories是你可以定义访问数据的接口,JPA查询是根据你的方法名称自动创建的。

这里我们编写一个接口,继承JpaRepository即可。User是对象名,不是表名,Long为主键的类型。

代码语言:javascript
复制
public interface UserDao extends JpaRepository<User, Long> {

    /**
     * 根据用户名和密码查询用户
     */
    User findByUsernameAndPassword(String username, String password);
}

JPA默认支持常见的增删改查,也支持findByUsernameAndPassword这种以字段命名的方法,对于更复杂的查询,您可以使用Spring Data的Query注解对方法进行注解。

命名规范与对应SQL

Keyword

Sample

JPQL snippet

Distinct

findDistinctByLastnameAndFirstname

select distinct … where x.lastname = ?1 and x.firstname = ?2

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is, Equals

findByFirstname,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, Null

findByAge(Is)Null

… 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 ages)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstname) = UPPER(?1)

测试JPA

代码语言:javascript
复制
@SpringBootTest
class SpringBootJpaApplicationTests {

    @Resource
    UserDao userDao;

    @Test
    void testJPA() {
        User user = userDao.save(new User(null, "summerday", "123456", "hangzhou"));
        System.out.println("添加用户: " + user);
        User u = userDao.findByUsernameAndPassword("summerday", "123456");
        System.out.println("根据用户名和密码查询用户: " + u);
        long count = userDao.count();
        System.out.println("当前用户数量: " + count);
        PageRequest page = PageRequest.of(0, 5, Sort.by(Sort.Order.desc("id")));
        Page<User> all = userDao.findAll(page);
        System.out.println("分页 + 根据id逆序 查询结果: " + all.getContent());
        if(userDao.existsById(u.getId())) {
            userDao.deleteById(u.getId());
            System.out.println("删除id为" + u.getId()+ "的用户成功");
        }
        long c = userDao.count();
        System.out.println("剩余用户数为: " + c);
    }
}

控制台输出如下:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-06-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JAVA烂猪皮 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • JPA是啥?
  • Spring Data JPA
  • 快速SpringBoot快速整合JPA
    • 引入依赖
      • 配置yml
        • ddl-auto的几种属性值
      • 实体类
        • 常见的主键生成策略
      • 启动项目,生成表
        • 数据访问层
          • 命名规范与对应SQL
        • 测试JPA
        相关产品与服务
        数据库
        云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档