<?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>
數據庫的更新幾種狀態:mysql
validate 加載hibernate時,驗證建立數據庫表結構
create 每次加載hibernate,從新建立數據庫表結構,這就是致使數據庫表數據丟失的緣由。
create-drop 加載hibernate時建立,退出是刪除表結構
update 加載hibernate自動更新數據庫結構web
第二種配置: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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--指定倉庫包 --> <jpa:repositories base-package="com.smallcode.pan.repository" /> <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> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.smallcode.pan.model" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="generateDdl" value="true" /> </bean> </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 name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/scpan</value> </property> <property name="username"> <value>root</value> </property> <property name="password" value="root" /> </bean> <!--掃描業務層 --> <context:component-scan base-package="com.smallcode.pan.service" /> </beans>
若是配置了 packagesToScan,就不能配置persistenceUnitName,不然packagesToScan會徹底不起做用,sql