A. 导入坐标

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>LearnSpringDataJpa</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <spring.version>5.2.12.RELEASE</spring.version>
  11. <hibernate.version>5.4.27.Final</hibernate.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>4.13.1</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <version>5.1.49</version>
  23. </dependency>
  24. <!-- Spring AoP -->
  25. <dependency>
  26. <groupId>org.aspectj</groupId>
  27. <artifactId>aspectjweaver</artifactId>
  28. <version>1.9.6</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-aop</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <!-- Spring IoC -->
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-context</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-context-support</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-beans</artifactId>
  49. <version>${spring.version}</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-core</artifactId>
  54. <version>${spring.version}</version>
  55. </dependency>
  56. <!-- Spring ORM -->
  57. <dependency>
  58. <groupId>org.springframework</groupId>
  59. <artifactId>spring-orm</artifactId>
  60. <version>${spring.version}</version>
  61. </dependency>
  62. <!-- Hibernate -->
  63. <dependency>
  64. <groupId>org.hibernate</groupId>
  65. <artifactId>hibernate-core</artifactId>
  66. <version>${hibernate.version}</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.hibernate</groupId>
  70. <artifactId>hibernate-entitymanager</artifactId>
  71. <version>${hibernate.version}</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.hibernate.validator</groupId>
  75. <artifactId>hibernate-validator</artifactId>
  76. <version>6.1.6.Final</version>
  77. </dependency>
  78. <!-- c3p0 -->
  79. <dependency>
  80. <groupId>com.mchange</groupId>
  81. <artifactId>c3p0</artifactId>
  82. <version>0.9.5.5</version>
  83. </dependency>
  84. <!-- Spring Data Jpa -->
  85. <dependency>
  86. <groupId>org.springframework.data</groupId>
  87. <artifactId>spring-data-jpa</artifactId>
  88. <version>2.3.6.RELEASE</version>
  89. </dependency>
  90. <dependency>
  91. <groupId>org.springframework</groupId>
  92. <artifactId>spring-test</artifactId>
  93. <version>${spring.version}</version>
  94. </dependency>
  95. <!-- el beg 使用 Spring Data Jpa 必须导入的 -->
  96. <dependency>
  97. <groupId>javax.el</groupId>
  98. <artifactId>javax.el-api</artifactId>
  99. <version>3.0.0</version>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.glassfish</groupId>
  103. <artifactId>javax.el</artifactId>
  104. <version>3.0.0</version>
  105. </dependency>
  106. </dependencies>
  107. </project>

B. SpringDataJpa 的配置文件 applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/data/jpa
  14. http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
  15. <!--spring 和 spring data jpa的配置-->
  16. <!-- 1.创建entityManagerFactory对象交给spring容器管理-->
  17. <bean id="entityManagerFactoty" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  18. <property name="dataSource" ref="dataSource" />
  19. <!--配置的扫描的包(实体类所在的包) -->
  20. <property name="packagesToScan" value="com.example.domain" />
  21. <!-- jpa的实现厂家 -->
  22. <property name="persistenceProvider">
  23. <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
  24. </property>
  25. <!--jpa的供应商适配器 -->
  26. <property name="jpaVendorAdapter">
  27. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  28. <!--配置是否自动创建数据库表 -->
  29. <property name="generateDdl" value="false" />
  30. <!--指定数据库类型 -->
  31. <property name="database" value="MYSQL" />
  32. <!--数据库方言:支持的特有语法 比如 mysql的分页-->
  33. <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
  34. <!--是否显示sql -->
  35. <property name="showSql" value="true" />
  36. </bean>
  37. </property>
  38. <!--jpa的方言 :高级的特性 -->
  39. <property name="jpaDialect" >
  40. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
  41. </property>
  42. </bean>
  43. <!--2.创建数据库连接池 -->
  44. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  45. <property name="user" value="root"/>
  46. <property name="password" value="123456"/>
  47. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jpaTest"/>
  48. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  49. </bean>
  50. <!--3.整合spring dataJpa-->
  51. <jpa:repositories base-package="com.example.dao" transaction-manager-ref="transactionManager"
  52. entity-manager-factory-ref="entityManagerFactoty"/>
  53. <!--4.配置事务管理器 -->
  54. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  55. <property name="entityManagerFactory" ref="entityManagerFactoty"/>
  56. </bean>
  57. <!-- 5. 配置包扫描-->
  58. <context:component-scan base-package="com.example"/>
  59. </beans>

C. 编写实体类,使用 jpa 注解配置映射关系

D. 编写 DAO 层接口