专业的编程技术博客社区

网站首页 > 博客文章 正文

简单持久层框架 jpa(java持久层框架对比)

baijin 2024-08-10 13:33:38 博客文章 30 ℃ 0 评论

实施应用程序的数据访问层已经很麻烦了很久。必须编写太多的样板代码来执行简单查询,以及执行分页和审核。Spring数据JPA旨在通过减少实际需要的数量来显着提高数据访问层的实现。作为开发人员,您编写存储库接口(包括自定义查找器方法),Spring将自动提供该实现。

特征

  • 基于Spring和JPA构建存储库的复杂支持

  • 支持Querydsl谓词,从而支持类型安全的JPA查询

  • 域类的透明审计

  • 分页支持,动态查询执行,整合自定义数据访问代码的能力

  • @Query在引导时间验证注释查询

  • 支持基于XML的实体映射

  • 通过介绍基于JavaConfig的存储库配置@EnableJpaRepositories

package hello;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

@Entity

public class Customer {

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

private Long id;

private String firstName;

private String lastName;

protected Customer() {}

public Customer(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

@Override

public String toString() {

return String.format(

"Customer[id=%d, firstName='%s', lastName='%s']",

id, firstName, lastName);

}

}

这里有一个Customer有三个属性的类id,即firstName,和,lastName。你也有两个构造函数。默认的构造函数仅为了JPA而存在。你不会直接使用它,所以它被指定为protected。另一个构造函数是用于创建Customer要保存到数据库的实例的构造函数。

在本指南中,为了简洁起见,典型的吸气剂和搅拌器已被忽略。

Customer类都被注解@Entity,表明它是一个JPA实体。由于缺少@Table注释,假设该实体将映射到一个名为的表Customer

Customerid财产都被注解@Id,使JPA将其识别为对象的ID。该id属性还注释@GeneratedValue为指示应自动生成ID。

另外两个属性,firstNamelastName留下未注释。假设它们将映射到与属性本身具有相同名称的列。

方便的toString()方法将打印客户的属性。

创建简单查询

Spring数据JPA专注于使用JPA将数据存储在关系数据库中。其最引人注目的功能是能够在运行时从存储库接口自动创建存储库实现。

要了解它是如何工作的,请创建一个与Customer实体一起使用的存储库接口:

src/main/java/hello/CustomerRepository.java

package hello;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {

List<Customer> findByLastName(String lastName);

}

CustomerRepository扩展CrudRepository界面。实体和ID的类型,CustomerLong在通用参数中指定CrudRepository。通过扩展CrudRepositoryCustomerRepository继承了使用Customer持久性的几种方法,包括保存,删除和查找Customer实体的方法。

Spring数据JPA还允许您通过简单地声明其方法签名来定义其他查询方法。在这种情况下CustomerRepository,用findByLastName()方法显示。

在典型的Java应用程序中,您希望编写一个实现的类CustomerRepository。但是,这就是Spring Data JPA如此强大:您不必编写存储库接口的实现。Spring Data JPA在运行应用程序时即时创建一个实现。

让我们来看看它的样子吧!

创建一个应用程序类

在这里,您创建一个包含所有组件的Application类。

package hello;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class Application {

private static final Logger log = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {

SpringApplication.run(Application.class);

}

@Bean

public CommandLineRunner demo(CustomerRepository repository) {

return (args) -> {

// save a couple of customers

repository.save(new Customer("Jack", "Bauer"));

repository.save(new Customer("Chloe", "O'Brian"));

repository.save(new Customer("Kim", "Bauer"));

repository.save(new Customer("David", "Palmer"));

repository.save(new Customer("Michelle", "Dessler"));

// fetch all customers

log.info("Customers found with findAll():");

log.info("-------------------------------");

for (Customer customer : repository.findAll()) {

log.info(customer.toString());

}

log.info("");

// fetch an individual customer by ID

Customer customer = repository.findOne(1L);

log.info("Customer found with findOne(1L):");

log.info("--------------------------------");

log.info(customer.toString());

log.info("");

// fetch customers by last name

log.info("Customer found with findByLastName('Bauer'):");

log.info("--------------------------------------------");

for (Customer bauer : repository.findByLastName("Bauer")) {

log.info(bauer.toString());

}

log.info("");

};

}

}

@SpringBootApplication 是一个方便的注释,它添加以下所有内容:

  • @Configuration 将类标记为应用程序上下文的bean定义的来源。

  • @EnableAutoConfiguration 告诉Spring Boot根据类路径设置,其他bean和各种属性设置开始添加bean。

  • 通常,您将添加@EnableWebMvc一个Spring MVC应用程序,但Spring Boot 在类路径中看到spring-webmvc时会自动添加它。这将应用程序标记为Web应用程序,并激活诸如设置a的关键行为DispatcherServlet

  • @ComponentScan告诉Spring在hello程序包中查找其他组件,配置和服务,以便找到控制器。

main()方法使用Spring Boot的SpringApplication.run()方法启动应用程序。你注意到没有一行XML吗?没有web.xml文件。此Web应用程序是100%纯Java,您无需处理配置任何管道或基础设施。

Application包括main()这使该方法CustomerRepository通过几个测试。首先,它CustomerRepository从Spring应用程序上下文中获取。然后它保存了少量Customer对象,演示了该save()方法并设置了一些可以使用的数据。接下来,它调用从数据库中findAll()获取所有Customer对象。然后它调用通过其ID findOne()获取单个Customer。最后,它呼吁findByLastName()找到所有名字叫做“鲍尔”的客户。

默认情况下,Spring Boot将启用JPA存储库支持,并查看所在的包(及其子包)@SpringBootApplication。如果您的配置中包含位于包中的JPA存储库接口定义不可见,则可以使用@EnableJpaRepositories它及其类型安全basePackageClasses=MyRepository.class参数指出备用软件包。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表