欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring2.5注释型 + jpa + 数据库池的配置(2)

程序员文章站 2022-03-02 14:38:43
...
文件配置:
applicationContext.xml


<?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:aop="http://www.springframework.org/schema/aop"
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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config></context:annotation-config>
<!--自动匹配${**}与classpath:jdbc.properties的值-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!-- 数据库池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${db.url}"></property>
<property name="driverClassName" value="${db.driverclass}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="maxActive" value="${db.maxActive}"></property>
<property name="maxIdle" value="${db.maxIdle}"></property>
<property name="maxWait" value="${db.maxWait}"></property>
<property name="minIdle" value="${db.minIdle}"></property>
</bean>
<!-- 实体管理工厂 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- myPersistenceXml对应persistence.xml中的persistence-unit name的值 -->
<property name="persistenceUnitName" value="myPersistenceXml"></property>
<!-- persistenceXmlLocation persistence.xml存放路径 -->
<property name="persistenceXmlLocation" value="META-INF/persistence.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 事务管理驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 自动扫描匹配 -->
<context:component-scan base-package="org.jinsheng.dao"></context:component-scan>
</beans>


persistence.xml
如果为java program 要所此文件放到 src/META-INF目录***意大写。


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="myPersistenceXml">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- 说明数据库池不在本配置文件中配置 -->
<non-jta-data-source></non-jta-data-source>
<!-- 实体类文件 -->
<class>org.jinsheng.jpa.entity.Person</class>
<!-- <properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
</properties> -->
</persistence-unit>
</persistence>


jdbc.properties



db.url=jdbc:oracle:thin:@localhost:1521:oracle
db.driverclass=oracle.jdbc.driver.OracleDriver
db.username=pjsh
db.password=pjsh
db.maxActive=10
db.maxIdle=5
db.maxWait=4
db.minIdle=2