原创 | 使用JPA实现DDD持久化-通过Spring Data JPA访问数据
通过Spring Data JPA访问数据
上节内容介绍了如何通过原生访问数据库。从范例代码中可以看到,这个过程还是比较繁琐的,例如:
需要创建和调用;
需要使用编写查询语句;
需要自己管理事务。
实际上大多数时候我们的查询都只是根据属性值查询实体对象而已。我们不希望为这样简单的目的编写一堆样板代码。
的出现大大简化了查询的实现。它使得我们只需要通过创建一个接口,并且在其中根据我们的意图构造一个方法名称,在运行时会自动使用实现这个接口,查询数据库并返回正确的数据。
本项目的模块,就是使用实现模块中定义的仓储接口。
引入依赖
为了使用,我们只需要在项目的中引入依赖。当然还需要一个的实现框架,以及相关数据库的驱动:
yang.yu.tmall
tmall-domain
${project.version}
javax.transaction
javax.transaction-api
1.3
org.hibernate
hibernate-core
runtime
org.jboss.spec.javax.transaction
jboss-transaction-api_1.2_spec
org.springframework.data
spring-data-jpa
com.h2database
h2
1.4.200
runtime
com.mchange
c3p0
0.9.5.5
Spring配置
需要类或形式定义配置,创建和相关的基础设施。此处采用配置类形式的配置:
@Configuration
@ComponentScan("yang.yu.tmall")
@EnableJpaRepositories(basePackages = {"yang.yu.tmall.repository"})
@EnableTransactionManagement
@PropertySource("/jdbc.properties")
public class JpaSpringConfig {
private Environment env;
public JpaSpringConfig(Environment env) {
this.env = env;
}
@Bean(destroyMethod = "close")
public ComboPooledDataSource dataSource() throws Exception {
ComboPooledDataSource result = new ComboPooledDataSource();
result.setDriverClass(env.getProperty("jdbc.driverClassName"));
result.setJdbcUrl(env.getProperty("jdbc.url"));
result.setUser(env.getProperty("jdbc.username"));
result.setPassword(env.getProperty("jdbc.password", ""));
return result;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter result = new HibernateJpaVendorAdapter();
result.setDatabase(Database.H2);
result.setDatabasePlatform(env.getProperty("hibernate.dialect"));
result.setGenerateDdl(true);
result.setShowSql(true);
return result;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter adapter) {
LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean();
result.setDataSource(dataSource);
result.setJpaVendorAdapter(adapter);
result.setPackagesToScan("yang.yu.tmall.domain");
result.setJpaPropertyMap(hibernateProperties());
return result;
}
private Map hibernateProperties() {
Map props = new HashMap<>();
props.put("hibernate.implicit_naming_strategy", "jpa");
return props;
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
定义JpaRepository接口的子类,同时实现实体仓储接口
在模块中,我们定义了一个定价仓储接口:
/**
* 定价仓储接口
*/
public interface Pricings {
/**
* 保存定价信息
* @param pricing 要保存的定价信息
* @return 持久化后的定价信息
*/
Pricing save(Pricing pricing);
/**
* 获得指定产品在指定时间的定价信息
* @param product 产品
* @param time 时间
* @return 当时的产品价格
*/
Optional getPricingAt(Product product, LocalDateTime time);
}
定价实体记录产品每次调价的信息。其定义如下:
@Entity
@Table(name = "pricings")
public class Pricing extends BaseEntity {
@ManyToOne
private Product product; //产品
private Money unitPrice; //单价
@Column(name = "effective_time")
private LocalDateTime effectiveTime; //生效时间
public Pricing() {
}
public Pricing(Product product, Money unitPrice, LocalDateTime effectiveTime) {
this.product = product;
this.unitPrice = unitPrice;
this.effectiveTime = effectiveTime;
}
public Pricing(Product product, Money unitPrice) {
this(product, unitPrice, LocalDateTime.now());
}
public Product getProduct() {
return product;
}
public Money getUnitPrice() {
return unitPrice;
}
public LocalDateTime getEffectiveTime() {
return effectiveTime;
}
}
我们定义一个接口(注意是接口而不是实现类),同时扩展了和:
@Named
public interface PricingRepository extends Pricings, JpaRepository {
@Override
default Optional getPricingAt(Product product, LocalDateTime time) {
return findFirstByProductAndEffectiveTimeIsLessThanEqualOrderByEffectiveTimeDesc(product, time);
}
Optional findFirstByProductAndEffectiveTimeIsLessThanEqualOrderByEffectiveTimeDesc(Product product, LocalDateTime time);
}
说明如下:
这个接口扩展了和。后者提供了很多预定义的方法。
提供的已经提供了接口中定义的方法。因此不再需要提供这个方法。
由于接口的方法不符合的方法命名规范,不能由在运行时自动提供实现,因此我做了一个转换。我在中定义了一个符合规范的方法名,同时利用引进的默认方法特性定义了接口的同名默认方法,它直接调用方法。
方法由在运行时自动生成实现类。它查询数据库,返回关联指定产品的、生效时间不晚于指定时间的、按生效时间降序排列的定价实体的列表中的第一个定价实体(如果有的话)。
通过给类标注注解,将它定义为一个由容器管理的。可以把它注入到它的客户类中,供后者使用。注解由依赖注入规范定义。为什么不使用中等价的注解?首先是因为是依赖注入的规范,规范优先于实现;其次是因为只能注解类而不能注解接口。
集成测试
我们用集成测试作为仓储接口的客户类,来测试是否正确实现:
@SpringJUnitConfig(classes = JpaSpringConfig.class)
@Transactional
public class PricingRepositoryTest implements WithAssertions {
@Inject
private Pricings pricings;
@Inject
private EntityManager entityManager;
private Product product1, product2;
private Pricing pricing1, pricing2, pricing3, pricing4;
@BeforeEach
void beforeEach() {
product1 = entityManager.merge(new Product("电冰箱", null));
product2 = entityManager.merge(new Product("电视机", null));
pricing1 = entityManager.merge(new Pricing(product1, Money.valueOf(500), LocalDate.of(2020,10, 1).atStartOfDay()));
pricing2 = entityManager.merge(new Pricing(product1, Money.valueOf(600), LocalDate.of(2020,2, 15).atStartOfDay()));
pricing3 = entityManager.merge(new Pricing(product2, Money.valueOf(7000), LocalDate.of(2020,7, 14).atStartOfDay()));
pricing4 = entityManager.merge(new Pricing(product2, Money.valueOf(7100), LocalDate.of(2020,2, 15).atStartOfDay()));
}
@AfterEach
void afterEach() {
Arrays.asList(product1, product2, pricing1, pricing2, pricing3, pricing4)
.forEach(entityManager::remove);
}
@Test
void getPriceAt() {
LocalDateTime time2002_02_15 = LocalDate.of(2020, 2, 15).atStartOfDay();
LocalDateTime time2002_02_16 = LocalDate.of(2020, 2, 16).atStartOfDay();
LocalDateTime time2002_10_01 = LocalDate.of(2020, 10, 1).atStartOfDay();
assertThat(pricings.getPricingAt(product1, time2002_02_15))
.map(Pricing::getUnitPrice)
.contains(Money.valueOf(600));
assertThat(pricings.getPricingAt(product1, time2002_02_16))
.map(Pricing::getUnitPrice)
.contains(Money.valueOf(600));
assertThat(pricings.getPricingAt(product1, time2002_10_01))
.map(Pricing::getUnitPrice)
.contains(Money.valueOf(500));
}
}
这个集成测试类使用了,因此可以实现依赖注入(通过注解)和事务管理(通过注解)。
无法自动实现的方法
并非所有的查询方法都能通过来自动生成实现类。这个时候也有解决方法。下面是一个例子:
订单仓储接口中有一个方法,用于查找买家类型为机构买家的所有订单:
public interface Orders {
...
Stream findByBuyer(Buyer buyer);
Stream findByOrgBuyers();
...
}
接口中的方法符合规范,可由自动生成实现;而方法无法自动生成实现。
这时可以定义一个接口,此处命名为,它定义了同样的方法:
public interface OrderOperations {
Stream findByOrgBuyers();
}
同时提供它的实现类,命名为接口名加后缀:
public class OrderOperationsImpl implements OrderOperations {
private EntityManager entityManager;
public OrderOperationsImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Stream findByOrgBuyers() {
String jpql = "select o from Order o join o.buyer b where TYPE(b) = OrgBuyer";
return entityManager.createQuery(jpql, Order.class)
.getResultStream();
}
}
它可以依赖注入管理的任何。此处通过构造函数注入一个,由实现方法。
我们的实现的仓储,除了扩展和之外,还要扩展:
@Named
public interface OrderRepository extends Orders, JpaRepository, OrderOperations {
Stream findByBuyer(Buyer buyer);
}
在运行时,会自动为标准方法生成实现,并且将对的方法调用转发给的方法去实现。
总之,使用可以:
对于基于属性的简单查询和排序,可以在运行时自动生成实现;
对于复杂查询,可以自定义实现方法,由调用。
总结
本章的实例覆盖了通过实现的主要内容。
总体而言,映射元数据既可以按照类级/属性级映射划分,又可以按物理/逻辑映射划分;
对于属性级映射,既可以按值/关联映射划分,又可以按单值/多值映射划分。
后面的章节将对映射、连接、访问三大部分分门别类进行详细论述。其他种种如缓存、事务、事件机制等等细节也都会一一说明。
详细内容请戳这里↓↓↓
这一节就讲到这里,下一节我们讲全新章节"映射"。
如果觉得有收获,点个【赞】鼓励一下呗!
