之前的Spring腳手架項目,因業務須要,是基於多數據源 and不一樣方言的數據庫的,對於普通項目過於複雜了, html
因而從新搭了個單數據源的,用STS新建完畢,這是項目目錄, java
加上配置以後,先列下application-db-config.xml文件,它提供了spring集成jpa的配置,固然這一版是有點小問題的,下面咱們來看 web
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${database1.driverClassName}"/> <property name="jdbcUrl" value="${database1.url}"/> <property name="user" value="${database1.username}"/> <property name="password" value="${database1.password}"/> <property name="minPoolSize" value="10"/> <property name="maxPoolSize" value="80"/> <property name="maxIdleTime" value="1800"/> <property name="acquireIncrement" value="2"/> <property name="acquireRetryDelay" value="1000"/> <property name="maxStatements" value="0"/> <property name="initialPoolSize" value="20"/> <property name="idleConnectionTestPeriod" value="60"/> <property name="acquireRetryAttempts" value="30"/> <property name="breakAfterAcquireFailure" value="false"/> <property name="testConnectionOnCheckout" value="false"/> <property name="testConnectionOnCheckin" value="false"/> </bean> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="false" /> <!-- <property name="showSql" value="false" /> --> <!-- <property name="database" value="MYSQL"/> --> </bean> <!-- Class 'org.hibernate.ejb.HibernatePersistence' is marked deprecated --> <bean id="persistenceProvider" class="org.hibernate.jpa.HibernatePersistenceProvider"/> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="persistenceXmlLocation" value="classpath:persistence.xml"/> <property name="persistenceUnitName" value="persistenceUnit" /> <property name="persistenceProvider" ref="persistenceProvider"/> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <property name="jpaDialect" ref="jpaDialect"/> <property name="packagesToScan"> <list> <value>com.buwei.webpageapp.domain</value> </list> </property> <property name="jpaProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.connection.charSet">UTF-8</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/> </beans>和 persistence.xml,讓spring來徹底管理JPA,因此此文件裏不須要什麼內容,
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"/> </persistence>而後運行單元測試,個人dao是由Spring Data JPA完成的,報了個」Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemLogRepository'「
我第一反應時候,這個domain包裏的Entity沒有被掃描到嗎,通過baidu 和 Stack Overflow一番,有說 spring
若是配置了 packagesToScan,就不能配置persistenceUnitName,不然packagesToScan會徹底不起做用, sql
詳解能夠參考 http://www.cnblogs.com/taven/archive/2013/10/04/3351841.html 數據庫
把persistenceUnitName註釋後的再運行單元測試,仍是報錯, api
」javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, ......「 app
彷佛是少了el依賴,因而在pom文件中加上 dom
<dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.el</artifactId> <version>3.0.0</version> </dependency>再運行單元測試,測試成功
數據插入成功, ide
同時把persistenceXmlLocation註釋掉,這樣不須要persistence.xml文件也不影響spring與jpa的集成,
可是persistence.xml的配置下得轉移到entityManagerFactory的jpaProperties
<property name="jpaProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.connection.charSet">UTF-8</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> </props> </property>