SSH或者SSM的applicationContext.xml的配置

前端之家收集整理的这篇文章主要介绍了SSH或者SSM的applicationContext.xml的配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
因为用这个的时候一般我都不怎么记得,要去找之前的项目,个人又比较懒,所以东西都放的很乱,所以今天就把它总结一下,在这里mark下:

一般用SSH时个人习惯把数据库的驱动等相关信息写在配置文件中,这样的话程序灵活行更大些(比如有时候需要切换数据库,从oracle到sql时,我们就只用修改spring读取的配置文件就可以了),废话不多说上代码

jdbc.oracle.properties:
jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username = SCOTT
jdbc.password = root

maxActive = 1000
maxIdle = 50
minIdle = 5
maxWait = 3000
initialSize = 20
.......

hibernate.dialect = org.hibernate.dialect.OracleDialect
hibernate.show_sql = true
hibernate.hbm2ddl.auto = update
hibernate.format_sql = true

jdbc.sqlserver.properties:(这个就只写些基本信息了,要什么自己去网上搜,一搜一大把)
jdbc.driverClassName = com.microsoft.sqlserver.jdbc.sqlServerDriver
jdbc.url = jdbc:sqlserver://localhost:1433;DatabaseName=****
jdbc.username = sa
jdbc.password = *****


applicationContext.xml的配置:

<!--加载配置文件 -->
<bean id="placeHolderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.oracle.properties</value>
</property>
</bean>
<!--配置dbcp数据源 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}" />
<property name="maxWait" value="${maxWait}" />
<property name="initialSize" value="${initialSize}"/>
<property name="validationQuery" value="select 1 from dual" />
</bean>

<!-- 配置hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionfactorybean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath*:com/“”“”/hibernate</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>
</bean>

<!-- 配置spring事务管理bean -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*()" propagation="required" rollback-for="Exception"/>
<tx:method name="add*()" propagation="required" rollback-for="Exception"/>
<tx:method name="xinZeng*()" propagation="required" rollback-for="Exception"/>
<tx:method name="update*()" propagation="required" rollback-for="Exception"/>
<tx:method name="xiuGai*()" propagation="required" rollback-for="Exception"/>
<tx:method name="delete*()" propagation="required" rollback-for="Exception"/>
<tx:method name="del*()" propagation="required" rollback-for="Exception"/>
<tx:method name="upd*()" propagation="required" rollback-for="Exception"/>
<tx:method name="find*()" propagation="required" read-only="true"/>
<tx:method name="query*()" propagation="required" read-only="true"/>
<tx:method name="search*()" propagation="required" read-only="true"/>
<tx:method name="get*()" propagation="required" read-only="true"/>
<tx:method name="chaZhao*()" propagation="required" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.***.dao.impl.*.*(..))" advice-ref="txAdvice"/>
</aop:config>

<!--配置自动扫描的包-->
<context:component-scan base-package="com.***">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>

顺便写下c3p0数据源的配置(这里数据库就使用sqlserver了
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.sqlServerDriver"/>
<property name="jdbcUrl" value="jdbc:sqlserver://localhost:49608;DatabaseName=***"/>
<property name="user" value="sa"/>
<property name="password" value="sa"/>

<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
干脆加上SSM的配置算了(免得我这榆木脑袋又忘了),大同小异
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="required" rollback-for="Exception"/>
<tx:method name="delete*" propagation="required" rollback-for="Exception"/>
<tx:method name="update*" propagation="required" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- myBatis文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.sqlSessionfactorybean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置使用事务的方法和类 -->
<aop:config>
<aop:pointcut expression="execution(* com.项目名.service.*.*(..))" id="allMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
mybaties-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAliasalias="别名" type="类路径"/>
</ typeAliases>
<mappers>
<mapper resource="映射文件路径"/>
</ mappers>
</ configuration>

<!--这里也不一定是项目名,一般包命名com.公司名.项目名.层-->
<context:component-scan base-package="com.项目名.**" />
原文链接:https://www.f2er.com/xml/298460.html

猜你在找的XML相关文章