Spring 框架组件之 Spring Data
前言
本篇文章主要记录本人在学习使用 Spring 框架组件 Spring Data 的过程中一些总结及感悟,学习过程中通过新建项目边学边练来加深理解
Spring Data 概述
- Spring Data 是 Spring 的一个子项目。用于简化数据库访问,支持 NoSQL 和关系数据存储 。其主要目标是使数据库的访问变得方便快捷。
- Spring Data 项目所支持 NoSQL 存储:
-MongoDB (文档数据库) -Neo4j(图形数据库) -Redis(键 / 值存储) -Hbase(列族数据库)
- Spring Data 项目所支持的关系数据存储技术:
-JDBC -JPA (本次采用)
- JPA Spring Data 致力于减少数据访问层 (DAO) 的开发量。开发者唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!
- 框架怎么可能代替开发者实现业务逻辑呢?比如:当有一个 UserDao.findUserById () 这样一个方法声明,大致应该能判断出这是根据给定条件的 ID 查询出满足条件的 User 对象。Spring Data JPA 做的便是规范方法的名字,根据符合规范的名字来确定方法需要实现什么样的逻辑。
搭建环境
Spring Data 、JPA 进行持久层开发步骤
配置 Spring 整合 JPA
1 | <!-- 1. 配置数据源 --> |
在 Spring 配置文件中配置 Spring Data
1
2
3
4
5
6<!-- 5. 配置 SpringData -->
<!-- 加入 jpa 的命名空间 -->
<!-- base-package: 扫描 Repository Bean 所在的 package -->
<jpa:repositories base-package="com.syshlang"
entity-manager-factory-ref="entityManagerFactory">
</jpa:repositories>
声明持久层的接口
持久层的接口:1
2
3
4
5
6
7
8
9
10
11
12
13
14/*
* Copyright (c) 2018. GRGBanking
* @File: PersonRepsotory.java
* @Description:
* @Author: sunys
* @Date: 18-6-7 下午9:49
* @since:
*/
package com.syshlang.repository;
import com.syshlang.entity.Person;
import org.springframework.data.repository.Repository;
public interface PersonRepsotory extends Repository<Person,Integer>{
}
实体类:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65/*
* Copyright (c) 2018. GRGBanking
* @File: Person.java
* @Description:
* @Author: sunys
* @Date: 18-6-7 下午9:54
* @since:
*/
package com.syshlang.entity;
import javax.persistence.*;
import java.util.Date;
public class Person {
private Integer id;
private String lastName;
private String email;
private Date birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String toString() {
return "Person [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", brith=" + birth + "]";
}
}
- Repository 是一个空接口。即是一个标记接口;
- 若我们定义的接口继承了 Repository, 则该接口会被 IOC 容器识别为一个 Repository Bean. 纳入到 IOC 容器中。进而可以在该接口中定义满足一定规范的方法;
- 实际上,也可以通过 @RepositoryDefinition 注解来替代继承 Repository 接口。
关于 Repository 接口相关概述参照《SpringData 组件之 Repository 接口概述》。
在接口中声明需要的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/*
* Copyright (c) 2018. GRGBanking
* @File: PersonRepsotory.java
* @Description:
* @Author: sunys
* @Date: 18-6-7 下午9:49
* @since:
*/
package com.syshlang.repository;
import com.syshlang.entity.Person;
import org.springframework.data.repository.Repository;
public interface PersonRepsotory extends Repository<Person,Integer>{
//根据 lastName 来获取对应的 Person
Person getByLastName(String lastName);
}
测试:1
2
3
4
5
6
7
public void testSpringData(){
PersonRepsotory personRepsotory = ctx.getBean(PersonRepsotory.class);
Person person = personRepsotory.getByLastName("AA");
System.out.println(person);
}
测试结果:1
2
3
4
5
6
7
8
9
10
11Hibernate:
select
person0_.id as id1,
person0_.birth as birth2,
person0_.email as email3,
person0_.last_name as last_nam4
from
jpa_persons person0_
where
person0_.last_name=?
Person [id=1, lastName=AA, email=syshlang@163.com, brith=2018-06-07 13:42:02.0]
在 Repository 子接口中声明方法:
- 不是随便声明的。而需要符合一定的规范;
- 查询方法以 find | read | get 开头;
- 涉及条件查询时,条件的属性用条件关键字连接;
- 要注意的是:条件属性以首字母大写;
- 支持属性的级联查询。若当前类有符合条件的属性,则优先使用,而不使用级联属性;
- 若需要使用级联属性,则属性之间使用 _ 进行连接。
关于 Repository 子接口中声明方法参照《SpringData 组件之 Repository 接口方法定义规范》。
附:配置清单1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.syshlang"></context:component-scan>
<!-- 1. 配置数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<!-- 配置其他属性 -->
</bean>
<!-- 2. 配置 JPA 的 EntityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--jpa是是实现产品的适配器-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<!--扫描加注解的包-->
<property name="packagesToScan" value="com.syshlang"></property>
<property name="jpaProperties">
<props>
<!-- 二级缓存相关 -->
<!--
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>
-->
<!-- 生成的数据表的列的映射策略 -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<!-- hibernate 基本属性 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 3. 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 4. 配置支持注解的事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 5. 配置 SpringData -->
<!-- 加入 jpa 的命名空间 -->
<!-- base-package: 扫描 Repository Bean 所在的 package -->
<jpa:repositories base-package="com.syshlang"
entity-manager-factory-ref="entityManagerFactory">
</jpa:repositories>
</beans>