springboot整合jpa_springboot 快速整合 jpa

c16b4d791c35bf168f32ab30ec2429b9.png

spring data jpa 官网的介绍

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.

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

spring data jpa 是什么

它是Spring基于ORM框架、JPA规范封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展。学习并使用Spring Data JPA可以极大提高开发效率。

新建项目,增加依赖

在 Intellij IDEA 里面新建一个 SpringBoot 项目后,添加如下依赖

org.springframework.boot

spring-boot-starter-data-jpa

mysql

mysql-connector-java

5.1.6

项目配置

在配置文件application.properties中增加如下配置

#通用数据源配置spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://{ip}:3306/{db}?charset=utf8mb4&useSSL=falsespring.datasource.username=springbootspring.datasource.password=springboot# Hikari 数据源专用配置spring.datasource.hikari.maximum-pool-size=20spring.datasource.hikari.minimum-idle=5# JPA 相关配置spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialectspring.jpa.database=mysqlspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=create

spring.jpa.show-sql=true 配置在日志中打印出执行的 SQL 语句信息。

spring.jpa.hibernate.ddl-auto=create 配置指明在程序启动的时候要删除并且创建实体类对应的表。这个参数很危险,因为他会把对应的表删除掉然后重建。所以千万不要在生成环境中使用。只有在测试环境中,一开始初始化数据库结构的时候才能使用一次。

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect 。在 SrpingBoot 2.0 版本中,Hibernate 创建数据表的时候,默认的数据库存储引擎选择的是 MyISAM (之前好像是 InnoDB,这点比较诡异)。这个参数是在建表的时候,将默认的存储引擎切换为 InnoDB 用的。

建立一个数据实体类user

d17e6410d1a3ce7dfc6346eed54f88fc.png

实现持久层服务UserDao

6f3752974a67ed0096fd23a77383ce5e.png



UserDao支持的方法如下:

c447b3624411be73ab2fbd9bbd3dc589.png

新建单元测试方法

9ba831ed53c3594c8ca61ac50f6606dd.png



可以看到,我们所做的全部事情仅仅是在 SpingBoot 工程里面增加数据库配置信息,声明一个 UserDO 的数据库实体对象,然后声明了一个持久层的接口,改接口继承自 org.springframework.data.jpa.repository.JpaRepository 接口。然后,系统就自动拥有了丰富的增加、删除、修改、查询功能。查询功能甚至还拥有了排序和分页的功能。

运行单元测试方法

36eaefc3243eb265482d32d1be35f4bf.png

通过控制台日志发现 ,已经成功新建了user表并且插入了一条user数据

结语

这个样例基本上讲述了 JPA 使用过程中的一些细节。我们可以看出。使用 JPA 来完成关于关系数据库增删改查的功能是非常的方便快捷的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用PostgreSQL数据库并结合JPA进行开发,可以按照以下步骤进行: 1. 添加依赖 在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 配置数据源 在`application.properties`文件中添加以下配置: ```properties spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=myuser spring.datasource.password=mypassword spring.datasource.driver-class-name=org.postgresql.Driver ``` 3. 创建实体类 创建一个实体类,用于映射数据库表的字段,例如: ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // getters and setters } ``` 4. 创建JpaRepository 创建一个继承自`JpaRepository`的接口,用于进行数据库操作,例如: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 5. 编写业务逻辑代码 在业务逻辑代码中注入`UserRepository`,并使用其提供的方法进行数据库操作,例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getAllUsers() { return userRepository.findAll(); } public void saveUser(User user) { userRepository.save(user); } // other methods } ``` 至此,Spring Boot整合PostgreSQL并结合JPA进行开发的基本步骤就介绍完了。当然,在实际开发中,还有很多需要注意的地方,例如事务管理、异常处理等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值