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

文件配置:
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值