persistence.xml html
<?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="customermanager"> <!-- 實體bean集合名字 --> <!-- JPA驅動提供者 --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name="hibernate.hbm2ddl.auto" value="validate"/> <property name="hibernate.max_fetch_depth" value="3"/> <property name="hibernate.jdbc.fetch_size" value="18"/> <property name="hibernate.jdbc.batch_size" value="10"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> </properties> </persistence-unit> </persistence>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 將一個業務邏輯實體交給Spring管理有兩種方式:註解+掃描、 配置 --> <!-- 掃描以cn.yj開頭的包,以及子包都會掃描的到 --> <context:component-scan base-package="com.zyios.customermanager" /> <!-- 數據源<strong>配置</strong> --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 驅動名稱 --> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- JDBC鏈接串 --> <property name="url" value="jdbc:mysql://localhost:3306/customermanager?useUnicode=true&characterEncoding=UTF-8" /> <!-- 數據庫用戶名稱 --> <property name="username" value="root" /> <!-- 數據庫密碼 --> <property name="password" value="123456" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 初始化大小 --> <property name="initialSize" value="5" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="2" /> <!-- 逐出鏈接的檢測時間間隔 --> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <!-- 最小逐出時間 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 測試有效用的SQL Query --> <property name="validationQuery" value="SELECT 'x'" /> <!-- 鏈接空閒時測試是否有效 --> <property name="testWhileIdle" value="true" /> <!-- 獲取鏈接時測試是否有效 --> <property name="testOnBorrow" value="false" /> <!-- 歸還鏈接時是否測試有效 --> <property name="testOnReturn" value="false" /> </bean> <!-- JPA實體工廠<strong>配置</strong> --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/> --> <property name="persistenceUnitName" value="customermanager" /> <!-- 掃描實體路徑 <property name="packagesToScan" value="com.zyios.customermanager.model" /> --> <!-- 運行時植入 <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> <property name="database" value="MYSQL" /> </bean> </property> </bean> <!-- bean post-processor for JPA annotations <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> --> <!-- 事務管理器,Spring提供的使用JPA進行開發的事務管理器 --> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 兩種使用事務的配置方式:xml、註解 --> <tx:annotation-driven transaction-manager="txManager" /> </beans>
網上淘來的,網址:http://www.verydemo.com/demo_c146_i25712.html java
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"> <!-- 數據源<strong>配置</strong> --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 驅動名稱 --> <property name="DriverClassName" value="com.mysql.jdbc.Driver" /> <!-- JDBC鏈接串 --> <property name="url" value="jdbc:mysql://192.168.7.7:3306/tmc_db?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true" /> <!-- 數據庫用戶名稱 --> <property name="username" value="root" /> <!-- 數據庫密碼 --> <property name="password" value="root" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 初始化大小 --> <property name="initialSize" value="5" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="2" /> <!-- 逐出鏈接的檢測時間間隔 --> <property name="timeBetweenEvictionRunsMillis" value="3000" /> <!-- 最小逐出時間 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 測試有效用的SQL Query --> <property name="validationQuery" value="SELECT 'x'" /> <!-- 鏈接空閒時測試是否有效 --> <property name="testWhileIdle" value="true" /> <!-- 獲取鏈接時測試是否有效 --> <property name="testOnBorrow" value="false" /> <!-- 歸還鏈接時是否測試有效 --> <property name="testOnReturn" value="false" /> </bean> <!-- JPA實體工廠<strong>配置</strong> --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 掃描實體路徑 --> <property name="packagesToScan" value="com.spinfosec.dao.entity" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> </bean> </property> <property name="jpaProperties"> <props> <!--設置外鏈接抓取樹的最大深度 --> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.jdbc.fetch_size">18</prop> <prop key="hibernate.jdbc.batch_size">10</prop> <!-- 自動建表類型 validate|create|create-drop|update --> <prop key="hibernate.hbm2ddl.auto">validate</prop> <!-- 是否顯示SQL --> <prop key="hibernate.show_sql">false</prop> <!-- 顯示SQL是否格式化 --> <prop key="hibernate.format_sql">false</prop> <!-- 關閉二級緩存 --> <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> <!-- 關閉實體字段映射校驗 --> <prop key="javax.persistence.validation.mode">none</prop> </props> </property> </bean> <!-- <strong>配置</strong>事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- <strong>配置</strong>事務傳播特性 --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- <strong>配置</strong>參與事務的類 --> <aop:config> <aop:pointcut id="dao-tx" expression="execution(* *..dao.impl..*(..))" /> <aop:pointcut id="service-tx" expression="execution(* *..service.impl..*(..))" /> <aop:advisor pointcut-ref="dao-tx" advice-ref="txAdvice" /> <aop:advisor pointcut-ref="service-tx" advice-ref="txAdvice" /> </aop:config> <!-- 事務註解 --> <tx:annotation-driven /> </beans>