Spring Data 概述

  1. Spring Data : Spring 的一个子项目。用于简化数据库访问,支持NoSQL 和 关系数据存储。其主要目标是使数据库的访问变得方便快捷。

2)SpringData 项目所支持 NoSQL 存储:
MongoDB (文档数据库)
Neo4j(图形数据库)
Redis(键/值存储)
Hbase(列族数据库)

  1. SpringData 项目所支持的关系数据存储技术:

JDBC
JPA

Spring Data JPA 概述

  1. JPA Spring Data : 致力于减少数据访问层 (DAO) 的开发量. 开发者唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!
  2. 框架怎么可能代替开发者实现业务逻辑呢?比如:当有一个 UserDao.findUserById() 这样一个方法声明,大致应该能判断出这是根据给定条件的 ID 查询出满足条件的 User 对象。Spring Data JPA 做的便是规范方法的名字,根据符合规范的名字来确定方法需要实现什么样的逻辑。

    Spring Data JPA HelloWorld

    使用 Spring Data JPA 进行持久层开发需要的四个步骤:
    –配置 Spring 整合 JPA
    –在 Spring 配置文件中配置 Spring Data,让 Spring 为声明的接口创建代理对象。配置了 jpa:repositories 后,Spring 初始化容器时将会扫描 base-package 指定的包目录及其子目录,为继承 Repository 或其子接口的接口创建代理对象,并将代理对象注册为 Spring Bean,业务层便可以通过 Spring 自动封装的特性来直接使用该对象。
    –声明持久层的接口,该接口继承 Repository,Repository 是一个标记型接口,它不包含任何方法,如必要,Spring Data 可实现 Repository 其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法。
    –在接口中声明需要的方法。Spring Data 将根据给定的策略(具体策略稍后讲解)来为其生成实现代码。

    1.配置xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:tx="http://www.springframework.org/schema/tx"
    6. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    11. <!-- 配置自动扫描的包 -->
    12. <context:component-scan base-package="cn.carven.springdata"></context:component-scan>
    13. <!-- 1. 配置数据源 -->
    14. <context:property-placeholder location="classpath:db.properties"/>
    15. <bean id="dataSource"
    16. class="com.mchange.v2.c3p0.ComboPooledDataSource">
    17. <property name="user" value="${jdbc.user}"></property>
    18. <property name="password" value="${jdbc.password}"></property>
    19. <property name="driverClass" value="${jdbc.driverClass}"></property>
    20. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    21. <!-- 配置其他属性 -->
    22. </bean>
    23. <!-- 2. 配置 JPA EntityManagerFactory -->
    24. <bean id="entityManagerFactory"
    25. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    26. <property name="dataSource" ref="dataSource"></property>
    27. <property name="jpaVendorAdapter">
    28. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
    29. </property>
    30. <property name="packagesToScan" value="cn.carven.springdata"></property>
    31. <property name="jpaProperties">
    32. <props>
    33. <!-- 二级缓存相关 -->
    34. <!--
    35. <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
    36. <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>
    37. -->
    38. <!-- 生成的数据表的列的映射策略 -->
    39. <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
    40. <!-- hibernate 基本属性 -->
    41. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    42. <prop key="hibernate.show_sql">true</prop>
    43. <prop key="hibernate.format_sql">true</prop>
    44. <prop key="hibernate.hbm2ddl.auto">update</prop>
    45. </props>
    46. </property>
    47. </bean>
    48. <!-- 3. 配置事务管理器 -->
    49. <bean id="transactionManager"
    50. class="org.springframework.orm.jpa.JpaTransactionManager">
    51. <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    52. </bean>
    53. <!-- 4. 配置支持注解的事务 -->
    54. <tx:annotation-driven transaction-manager="transactionManager"/>
    55. <!-- 5. 配置 SpringData -->
    56. <!-- 加入 jpa 的命名空间 -->
    57. <!-- base-package: 扫描 Repository Bean 所在的 package -->
    58. <jpa:repositories base-package="cn.carven.springdata"
    59. entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
    60. </beans>

    2.创建实体类

    1. @Table(name = "jpa_persons")
    2. @Entity
    3. public class Person {
    4. private Integer id;
    5. private String lastName;
    6. private String email;
    7. private Date birth;
    8. @GeneratedValue
    9. @Id
    10. public Integer getId() {
    11. return id;
    12. }
    13. public void setId(Integer id) {
    14. this.id = id;
    15. }
    16. @Column(name = "last_name")
    17. public String getLastName() {
    18. return lastName;
    19. }
    20. public void setLastName(String lastName) {
    21. this.lastName = lastName;
    22. }
    23. public String getEmail() {
    24. return email;
    25. }
    26. public void setEmail(String email) {
    27. this.email = email;
    28. }
    29. public Date getBirth() {
    30. return birth;
    31. }
    32. public void setBirth(Date birth) {
    33. this.birth = birth;
    34. }
    35. @Override
    36. public String toString() {
    37. return "Person [id=" + id + ", lastName=" + lastName + ", email="
    38. + email + ", brith=" + birth + "]";
    39. }
    40. }

    3.声明接口及方法

    1. public interface PersonRepository extends Repository<Person, Integer> {
    2. // 通过lastName获取Person对象
    3. Person getByLastName(String lastName);
    4. }

    4.测试

    1. private ApplicationContext context = null;
    2. {
    3. context = new ClassPathXmlApplicationContext("applicationContext.xml");
    4. }
    5. @Test
    6. public void testHelloWorldSpringData(){
    7. PersonRepository personRepository = context.getBean(PersonRepository.class);
    8. Person person = personRepository.getByLastName("qq");
    9. System.out.println(person);
    10. }