- Spring Boot ORM 教程
- Spring Boot ORM - 主页
- Spring Boot ORM - 概述
- 环境设置
- Spring Boot ORM 和 Spring Data JPA
- Spring Boot ORM - 创建项目
- 应用程序属性
- Spring Boot ORM - 更新项目
- Spring Boot ORM - 测试 Hibernate
- Spring Boot ORM 和 EclipseLink
- Maven EclipseLink
- 更新项目 EclipseLink
- Spring Boot ORM - 测试 EclipseLink
- Spring Boot ORM 有用资源
- Spring Boot ORM - 快速指南
- Spring Boot ORM - 有用的资源
- Spring Boot ORM - 讨论
Spring Boot ORM - 快速指南
Spring Boot ORM - 概述
Spring 框架与 Hibernate、Java Persistence API (JPA)、Java Data Objects (JDO) 和 iBATIS SQL Maps 等 ORM 框架很好地集成。Spring 提供资源管理、数据访问对象(DAO)实现和事务策略。Spring 允许通过依赖管理来配置 ORM 库功能。Spring 为其支持的所有 ORM 库维护统一的 DAO 异常层次结构和通用事务管理。
Spring IoC 容器有利于 ORM 配置和轻松部署。以下是使用 Spring 框架创建 ORM DAO 的主要好处。
易于测试- 使用 spring IoC,可以轻松配置 ORM 实现。每个持久性单元都可以单独测试。
常见数据访问异常- Spring 将 ORM 工具异常包装为常见运行时异常,作为 DataAccessException。此方法有助于在适当的层中处理大多数持久性异常(不可恢复)。无需处理 ORM 特定的样板 catch/throws/异常声明。
常规资源管理- Spring应用程序上下文可以轻松管理持久性对象及其配置。例如,Hibernate SessionFactory 实例、JPA EntityManagerFactory 实例、JDBC DataSource 实例、iBatis SQL Maps 配置对象和其他相关对象。Spring 自行处理本地事务以及 JTA 事务管理。
集成事务管理- Spring AOP 可用于使用声明性 AOP 样式拦截器包装 ORM 代码,可以使用 @Transaction 注释或通过在 XML 配置文件中指定事务 AOP 建议。Spring处理事务语义、异常处理、回滚等。Spring 允许交换事务管理器而不影响 ORM 代码。
Spring Boot ORM - 环境设置
本章将指导您如何准备开发环境以开始使用 Spring Boot 框架。它还将教您如何在设置 Spring Boot 框架之前在您的计算机上设置 JDK、Eclipse -
第 1 步 - 设置 Java 开发工具包 (JDK)
Java SE 可供免费下载。要下载,请单击此处,请下载与您的操作系统兼容的版本。
按照说明下载 Java,然后运行.exe以在您的计算机上安装 Java。在计算机上安装 Java 后,您需要设置环境变量以指向正确的安装目录。
设置 Windows 2000/XP 的路径
假设您已将 Java 安装在 c:\Program Files\java\jdk 目录中 -
右键单击“我的电脑”并选择“属性”。
单击“高级”选项卡下的“环境变量”按钮。
现在,编辑“Path”变量并在其末尾添加 Java 可执行文件目录的路径。例如,如果路径当前设置为 C:\Windows\System32,则按以下方式编辑
C:\Windows\System32;c:\Program Files\java\jdk\bin。
设置Windows 95/98/ME的路径
假设您已将 Java 安装在 c:\Program Files\java\jdk 目录中 -
编辑“C:\autoexec.bat”文件并在末尾添加以下行 -
设置路径=%PATH%;C:\Program Files\java\jdk\bin
设置 Linux、UNIX、Solaris、FreeBSD 的路径
应将环境变量 PATH 设置为指向 Java 二进制文件的安装位置。如果您在执行此操作时遇到问题,请参阅您的 shell 文档。
例如,如果您使用 bash 作为 shell,那么您可以在.bashrc末尾添加以下行-
- 导出 PATH=/path/to/java:$PATH'
或者,如果您使用集成开发环境 (IDE),如 Borland JBuilder、Eclipse、IntelliJ IDEA 或 Sun ONE Studio,则必须编译并运行一个简单的程序来确认 IDE 知道您安装了 Java 的位置。否则,您将必须按照 IDE 文档中的规定进行正确的设置。
第 2 步 - 设置 Eclipse IDE
本教程中的所有示例都是使用 Eclipse IDE 编写的。因此,我们建议您应该在计算机上安装最新版本的 Eclipse。
要安装 Eclipse IDE,请从https://www.eclipse.org/downloads/下载最新的 Eclipse 二进制文件。下载安装后,将二进制发行版解压到一个方便的位置。例如,在 Windows 上的 C:\eclipse 中,或在 Linux/Unix 上的 /usr/local/eclipse 中,最后适当地设置 PATH 变量。
Eclipse 可以通过在 Windows 机器上执行以下命令来启动,或者只需双击 eclipse.exe
%C:\eclipse\eclipse.exe
可以通过在 Unix(Solaris、Linux 等)机器上执行以下命令来启动 Eclipse -
$/usr/local/eclipse/eclipse
成功启动后,如果一切正常,那么它应该显示以下结果 -
第 3 步 - 设置 m2eclipse
M2Eclipse 是 Eclipse 插件,它将 Apache Maven 集成到 Eclipse IDE 中非常有用。我们在本教程中使用 Maven 来构建 Spring Boot 项目,示例使用 m2eclipse 在 Eclipse 中运行。
使用 Eclipse IDE 中的“安装新软件”对话框安装最新的 M2Eclipse 版本,并将其指向此 p2 存储库:https ://download.eclipse.org/technology/m2e/releases/latest/
第 3 步 - 设置 Spring Boot 项目
现在,如果一切正常,那么您可以继续设置 Spring Boot。以下是在您的计算机上下载并安装 Spring Boot 项目的简单步骤。
转到 spring Initializr 链接来创建 Spring Boot 项目,https://start.spring.io/。
选择项目作为Maven Project。
选择语言为Java。
选择 Spring Boot 版本为2.5.5。
设置项目元数据 - 组为com.tutorialspoint,工件为springbootorm,名称为springbootorm,描述为Spring Boot ORM 的演示项目,包名称为com.tutorialspoint.springbootorm。
选择包装为Jar。
选择 java 作为11。
添加Spring Web、Spring Data JPA、MySQL Driver 和 Spring Boot DevTools等依赖项。
现在单击“生成”按钮生成项目结构。
下载基于maven的spring boot项目后,将maven项目导入到eclipse中,其余的eclipse将处理。它将下载 Maven 依赖项并构建项目,为进一步开发做好准备。
步骤 4 - 用于 REST API 测试的 POSTMAN
POSTMAN 是测试基于 REST 的 API 的有用工具。要安装 POSTMAN,请从https://www.postman.com/downloads/下载最新的 POSTMAN 二进制文件。下载可安装程序后,请按照说明进行安装和使用。
Spring Boot ORM - 创建项目
使用 eclipse,选择File -> Import -> Existing Maven Project,然后单击 Next。
选择环境搭建时从springinitializr下载并解压的项目,如下图:
单击完成按钮,将创建一个新项目。
现在我们已经准备好了项目,让我们检查 pom.xml 中的以下依赖项。
Spring Boot 入门框架库
MySQL 连接器
其他相关依赖。
当我们使用 Spring Boot 时,它的启动项目会自动处理大部分依赖项。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tutorialspoint</groupId>
<artifactId>springbootorm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot ORM</name>
<description>Demo project for Spring Boot ORM</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Spring Boot ORM - Application.properties
Spring Boot 从 application.properties 读取应用程序以及与持久性相关的属性。在这里我们还可以配置 hibernate 或任何其他 ORM 框架特定的属性。我们使用通用属性,以便我们可以在 ORM 之间切换,而无需更改太多代码。默认情况下,如果POM.xml中没有指定其他 ORM 库,则 spring boot 将 hibernate 配置为 ORM 提供者。
在src -> main -> resources目录下创建application.properties并更新,如下所示。
应用程序属性
#datasource configurations spring.datasource.url=jdbc:mysql://localhost:3306/tutorialspoint?useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=root@123 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # show SQL spring.jpa.show-sql: true # DDL generation spring.jpa.generate-ddl=true
下面是application.properties的关键属性的描述。
spring.datasource - 数据库特定属性,如连接 url、用户名、密码、驱动程序类等。
spring.jpa - jpa 特定属性,例如显示 sql、允许创建表等。
Spring Boot ORM - 更新项目
现在让我们向 Spring 应用程序添加一个 REST API,它可以添加、编辑、删除和显示员工。
实体 - Employee.java
package com.tutorialspoint.springbootorm.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int age;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
存储库 - EmployeeRepository.java
package com.tutorialspoint.springbootorm.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.springbootorm.entity.Employee;
@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
}
服务 - EmployeeService.java
package com.tutorialspoint.springbootorm.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tutorialspoint.springbootorm.entity.Employee;
import com.tutorialspoint.springbootorm.repository.EmployeeRepository;
@Service
public class EmployeeService {
@Autowired
EmployeeRepository repository;
public Employee getEmployeeById(int id) {
return repository.findById(id).get();
}
public List<Employee> getAllEmployees(){
List<Employee> employees = new ArrayList<Employee>();
repository.findAll().forEach(employee -> employees.add(employee));
return employees;
}
public void saveOrUpdate(Employee employee) {
repository.save(employee);
}
public void deleteEmployeeById(int id) {
repository.deleteById(id);
}
}
服务 - EmployeeController.java
package com.tutorialspoint.springbootorm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tutorialspoint.springbootorm.entity.Employee;
import com.tutorialspoint.springbootorm.service.EmployeeService;
@RestController
@RequestMapping(path = "/emp")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeService.getAllEmployees();
}
@GetMapping("/employee/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
return employeeService.getEmployeeById(id);
}
@DeleteMapping("/employee/{id}")
public void deleteEmployee(@PathVariable("id") int id) {
employeeService.deleteEmployeeById(id);
}
@PostMapping("/employee")
public void addEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
@PutMapping("/employee")
public void updateEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
}
主应用程序 - SpringBootOrmApplication.java
package com.tutorialspoint.springbootorm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootOrmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootOrmApplication.class, args);
}
}
Spring Boot ORM - 测试 Hibernate
现在在 Eclipse 中,右键单击 SpringBootOrmApplication.java,选择Run As上下文菜单,然后选择Java Application。检查 eclipse 中的控制台日志。您可以看到以下日志:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.5) 2021-10-05 09:40:27.442 INFO 8704 --- [ restartedMain] c.t.s.SpringBootOrmApplication : Starting SpringBootOrmApplication using Java 11.0.11 on ... 2021-10-05 09:40:34.775 INFO 8704 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2021-10-05 09:40:34.858 INFO 8704 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2021-10-05 09:40:34.880 INFO 8704 --- [ restartedMain] c.t.s.SpringBootOrmApplication : Started SpringBootOrmApplication in 8.222 seconds (JVM running for 9.564) 2021-10-05 09:41:08.718 INFO 8704 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2021-10-05 09:41:08.718 INFO 8704 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2021-10-05 09:41:08.722 INFO 8704 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
服务器启动并运行后,首先使用 Postman 发出 POST 请求来添加记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - POST
URL - http://localhost:8080/emp/employee
BODY -员工 JSON
{
"age": "35",
"name": "Julie",
"email": "julie@gmail.com"
}
单击发送按钮并检查响应状态是否正常。现在发出 GET 请求来获取所有记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - GET
URL - http://localhost:8080/emp/employees
单击发送按钮并验证响应。
[{
"id": 1,
"age": 35,
"name": "Julie",
"email": "julie@gmail.com"
}]
Spring Boot ORM - Maven EclipseLink
Spring Boot 默认使用 Hibernate 作为 ORM 实现。为了使用EclipseLink,我们首先需要从pom.xml中的spring-data-jpa依赖中排除hibernate依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
现在在pom.xml中包含 eclipse-link 依赖项。
<dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> <version>2.7.8</version> </dependency>
以下是完整的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tutorialspoint</groupId>
<artifactId>springbootorm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot ORM</name>
<description>Demo project for Spring Boot ORM</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
保存文件,eclipse将自动更新依赖项。
Spring Boot ORM - 更新项目 EclipseLink
Spring Boot 使用HibernateJpaAutoConfiguration默认配置 hibernate 实现。为了切换到 EclipseLink,我们需要创建一个自定义配置类,它将扩展 JpaBaseConfiguration 类。JpaBaseConfiguration 是用于为任何 ORM 实现扩展和配置 JPA 的基类。以下是EclipsLinkJpaConfiguration的代码。
EclipsLinkJpaConfiguration.java
package com.tutorialspoint.springbootorm;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.logging.SessionLog;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;
@Configuration
public class EclipsLinkJpaConfiguration extends JpaBaseConfiguration {
protected EclipsLinkJpaConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
super(dataSource, properties, jtaTransactionManager);
}
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
@Override
protected Map<String, Object> getVendorProperties() {
Map<String, Object> map = new HashMap<>();
map.put(PersistenceUnitProperties.WEAVING, "false");
map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
map.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
return map;
}
@Bean
public static DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/tutorialspoint?useSSL=false&allowPublicKeyRetrieval=true");
dataSource.setUsername("root");
dataSource.setPassword("root@123");
return dataSource;
}
}
我们分别使用 createJpaVendorAdapter()、dataSource() 和 getVendorProperties() 方法添加了 Adapter、DataSource 和属性。
更新实体以使用 Integer 而不是 int。
package com.tutorialspoint.springbootorm.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Spring Boot ORM - 测试 EclipseLink
现在在 Eclipse 中,右键单击 SpringBootOrmApplication.java,选择Run As上下文菜单,然后选择Java Application。检查 eclipse 中的控制台日志。您可以看到以下日志 -
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.5) ... 2021-10-06 09:52:28.187 INFO 10048 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3111 ms ... [EL Info]: 2021-10-06 09:52:29.063--ServerSession(2026366076)--Thread(Thread[restartedMain,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.7.8.v20201217-ecdf3c32c4 ... [EL Config]: connection: 2021-10-06 09:52:29.424--ServerSession(2026366076)--Connection(460137428)--Thread(Thread[restartedMain,5,main])--Connected: jdbc:mysql://localhost:3306/tutorialspoint?useSSL=false&allowPublicKeyRetrieval=true User: root@localhost Database: MySQL Version: 8.0.23 Driver: MySQL Connector/J Version: mysql-connector-java-8.0.26 (Revision: 9aae1e450989d62c06616c1dcda3e404ef84df70) [EL Fine]: connection: 2021-10-06 09:52:29.471--ServerSession(2026366076)--Thread(Thread[restartedMain,5,main])--/file:/F:/tutorialspoint/springbootorm/target/classes/_default login successful [EL Finer]: metamodel: 2021-10-06 09:52:29.512--ServerSession(2026366076)--Thread(Thread[restartedMain,5,main])--Canonical Metamodel class [com.tutorialspoint.springbootorm.entity.Employee_] not found during initialization. 2021-10-06 09:52:29.796 WARN 10048 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2021-10-06 09:52:30.543 INFO 10048 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2021-10-06 09:52:30.603 INFO 10048 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2021-10-06 09:52:30.622 INFO 10048 --- [ restartedMain] c.t.s.SpringBootOrmApplication : Started SpringBootOrmApplication in 6.556 seconds (JVM running for 7.512) 2021-10-06 09:53:38.526 INFO 10048 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2021-10-06 09:53:38.527 INFO 10048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2021-10-06 09:53:38.532 INFO 10048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
服务器启动并运行后,使用 Postman 发出 GET 请求以获取所有记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - GET
URL - http://localhost:8080/emp/employees
单击发送按钮并验证响应。
[{
"id": 1,
"age": 35,
"name": "Julie",
"email": "julie@gmail.com"
}]