简化SSH配置+c3po连接池配置

使用Properties文件存储访问数据库连接参数。

以下为connection_setting.properties文件配置:


#database setting 连接设置
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/website
datasource.username=数据库连接名
datasource.password=数据库连接密码

#hibernate setting hibernate的配置
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true

#c3p0 setting c3p0连接池配置
minPoolSize=5
maxPoolSize=30
initialPoolSize=10
maxIdleTime=60
acquireIncrement=5
idleConnectionTestPeriod=60
acuireRetryAttempts=30

编写好上述properties文件后保存在WebRoot的web-inf 下面,接下来进行spring的文件配置


<!--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: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/tx
	 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	 http://www.springframework.org/schema/aop
	 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  >
<!-- 配置spring加载properties的bean -->
<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <!--该项配置spring会在classpath里面加载文件 -->				                            <!--<value>classpath:connection_setting.properties</value>-->
            <!--因为刚才配置在web-inf下面所以改成 -->				                            <!--<value>/WEB-INF/connection_setting.properties</value>-->
	</list>
    </property>
</bean>


到这里就配置好了加载properties的操作,接下来介绍两种连接数据库的方式,以便满足是否使用连接池的需要

首先介绍普通方式(即不采用连接池)

<!--不采用连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${datasource.driverClassName}" />
		<property name="url" value="${datasource.url}" />
		<property name="username" value="${datasource.username}" />
		<property name="password" value="${datasource.password}" />
</bean>

以下为c3p0连接池配置

<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClassName" value="${datasource.driverClassName}" />
		<property name="jdbcUrl" value="${datasource.url}" />
		<property name="user" value="${datasource.username}" />
		<property name="password" value="${datasource.password}" />                                     <!--以下为连接池的详细配置-->                                                                      <property name="minPoolSize" value="${minPoolSize}">                                            <property name="maxPoolSize" value="${maxPoolSize}" />                                          <property name="initialPoolSize" value="${initialPoolSize}"/>                                   <property name="maxIdleTime" value="${maxIdleTime}" />                           </bean>

接下来是配置sessionFactory


<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="mappingDirectoryLocations">                                                    <!--配置hibernate实体配置文件扫描-->
	<list>
		<value>classpath:com/concur/dogbank/entity</value>
	</list>                                                                                        <!--配置hibernate属性基本配置-->
	</property>
	    <property name="hibernateProperties">
			<props>
			<prop key="hibernate.dialect">${hibernate.dialect}</prop>
			<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			<prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
			</props>
		</property>
	</bean>
<!-- 事务通知 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 <tx:method name="*" propagation="REQUIRED" />
 <tx:method name="add*" propagation="REQUIRED" />
 <tx:method name="delete*" propagation="REQUIRED" />
 <tx:method name="update*" propagation="REQUIRED" />
 <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
 <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
 </tx:attributes>
 </tx:advice>

    <!--切面-->
 <aop:config>
 <aop:pointcut id="bizMethods" expression="execution(* com.concur.dogbank.biz.impl.*.*(..))" />
 <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
 </aop:config>
</beans>

好了配置完以上 spring+hibernate基本已经可以使用的。记得要导入对应的jar包。

接下来简化点struts的配置


<!--struts的头部 以后struts的子文件都继承现在的package 不再是继承struts-default 或者json-default -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
     <!--基本常量的配置-->
     <constant name="struts.i18n.encoding" value="utf-8" />                                          <constant name="strtus.configuration.xml.reload" value="true" />                                <package name="gloab-default" extends="json-default"
        <!--全局返回 适用于ajax操作的项目。如ext jquery等-->                                                     <global-results>                                                                                        <result name="success" type="json">                                                                <param name="root">json</param>                                                                    <!--以下配置适用于jsonp请求的返回-->                                                              <param name="callbackParameter">callback</param>                                            </result>                                                                                </global-result>
<!-- 如果项目有多个struts的xml文件可以通过下列方式引入到struts主文件 采用通配符的方式-->
</package>                                                                                        <include file="com/concur.dogbank.web/**/*-action.xml" />                                       </struts>

到此完成了struts父文件的配置。以后的子文件package只需要继承该文件的就可以拥有该文件的东西。

子文件可采用通配符的方式进行更简约的配置。在本文就不在叙述。详情百度。



你可能感兴趣的:(c3p0,ssh,配置,简化)