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="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    18. <property name="dataSource" ref="dataSource"/>
    19. <!--配置扫描的包(实体类所在的包)-->
    20. <property name="packagesToScan" value="com.cedric.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. <!--数据库方言:支持的特有语法 -->
    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"></property>
    46. <property name="password" value="cedricasdsaca"></property>
    47. <property name="jdbcUrl" value="jdbc:mysql:///springdata"></property>
    48. <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    49. </bean>
    50. <!--3.整合spring Data Jpa-->
    51. <jpa:repositories base-package="com.cedric.dao" transaction-manager-ref="transactionManager"
    52. entity-manager-factory-ref="entityManagerFactory">
    53. </jpa:repositories>
    54. <!--4.配置事务管理器-->
    55. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    56. <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    57. </bean>
    58. <!--5.声明式事务-->
    59. <!--6.配置包扫描-->
    60. <context:component-scan base-package="com.cedric"></context:component-scan>
    61. </beans>