Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--1 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/pro-jpa2?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--2 配置EntityManagerFactory
基本对象的构建:通过CriteriaBuilder对象获得CriteriaBuilder对象获得CriteriaQuery实例的from方法可以获得Root实例-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.imooc"/>
<property name="jpaProperties">
<props>
<!--建表的命名规则Hibernate4.3.11支持 生成的数据库字段名带有下划线分隔-->
<!--<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>-->
<!--建表的命名规则Hibernate5.0.2支持 自定义类的数据库字段名带有下划线分隔-->
<prop key="hibernate.physical_naming_strategy">com.imooc.util.ImprovedNamingStrategy</prop>
<!--数据库方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>-->
<!--显示SQL语句-->
<prop key="hibernate.show_sql">true</prop>
<!--hibernate sql语句格式化-->
<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"/>
</bean>
<!--4 配置支持注解的事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--5 配置spring data-->
<jpa:repositories base-package="com.imooc" entity-manager-factory-ref="entityManagerFactory"/>
<!--6 配置spring 注解扫描-->
<context:component-scan base-package="com.imooc"/>
</beans>