//import其他配置文件
<import resource="applicationContext-sec-authoriz.xml"/>
//指定配置文件
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">//位置
<value>/datasource.properties</value>
</property>
</bean>
典型datasource 配置属性
datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.url=jdbc/:sqlserver/://192.168.3.134/:1433;databaseName/=portal;
datasource.username=sa
datasource.password=123456
datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000
datasource.defaultAutoCommit=true
datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
datasource.removeAbandoned=true
datasource.removeAbandonedTimeout=120
//用jndi配置数据源
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/PDPORTALDB"/>
</bean>
tomcat conf/context.xml
<ResourceLink name="jdbc/PDPORTALDB" global="jdbc/PDPORTALDB" type="javax.sql.DataSourcer"/>
tomcat conf/server.xml
<Resource name="jdbc/PDPORTALDB" auth="Container" description="DB Connection"
type="javax.sql.DataSource" username="sa" password="123456"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://192.168.3.134:1433;DatabaseName=portal"
maxActive="10" maxIdle="2" maxWait="120000" defaultAutoCommit="true"
removeAbandoned="true"
removeAbandonedTimeout="120"/>
工程web.xml
<resource-ref>
<description>DataSource</description>
<res-ref-name>jdbc/PDPORTALDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
//配置sessionFactory
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/> //数据源
</property>
<property name="mappingResources">
<list>//hibernate 映射
<value>cn/com/dragontec/tool/reportmgr/data/model/Report.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
</props>
</property>
</bean>
//logic bean 定义dao
<bean id="reportDao" class="cn.com.dragontec.tool.reportmgr.data.dao.hibernate.ReportDaoImpl">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
<property name="report2Dao"> //引用其他dao
<ref local="report2Dao"/>
</property>
</bean>
//定义 事务管理
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" >
<ref local="mySessionFactory"/>
</property>
</bean>
//事务管理
参照:http://blog.youkuaiyun.com/dcju002/archive/2009/05/11/4168910.aspx
本文详细介绍了如何在Spring框架中配置数据库连接池及Hibernate会话工厂,并展示了使用JNDI配置数据源的过程。此外,还包含了事务管理和DAO层的具体实现。
969

被折叠的 条评论
为什么被折叠?



