struts,spring和mybatis框架整合
1.spring的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=””> <!--这个bean主要是为了得到ApplicationContext--> <bean id="applicationContextUtils" class="com.cattsoft.baseplatform.core.util.ApplicationContextUtil"></bean> <!--引入其他模块文件--> <import resource="classpath:spring-beans-core-service.xml" /> <!--省略aop编程--> <!-- 配置属性文件列表 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:/jdbc-config.properties</value> <value>classpath*:/dbcp-config.properties</value> </list> </property> </bean> <!-- 配置dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="testOnBorrow" value="${dbcp.testOnBorrow}" /> <property name="testOnReturn" value="${dbcp.testOnReturn}" /> <property name="testWhileIdle" value="${dbcp.testWhileIdle}" /> <property name="validationQuery" value="${dbcp.validationQuery}" /> <property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="minEvictableIdleTimeMillis" value="${dbcp.minEvictableIdleTimeMillis}" /> <property name="timeBetweenEvictionRunsMillis" value="${dbcp.timeBetweenEvictionRunsMillis}" /> </bean> <!-- 配置spring的PlatformTransactionManager,名字为默认值 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- mybatis 配置文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation"> <value>classpath:mybatis-config.xml</value> </property> </bean> <!-- mybatis mappers, 自动扫描文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage"> <value> com.abc.nxii.persistence, com.abc.baseplatform.func.sm.persistence, com.abc.baseplatform.core.persistence, com.abc.baseplatform.report.persistence, com.abc.baseplatform.imandexport.persistence </value> </property> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
2.jdbc-config.properties文件
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.username=gqcjxtcs jdbc.password=gqcjxtcs jdbc.url=jdbc:oracle:thin:@10.6.111.153:1521:orcl
3.dbcp-config.properties文件
dbcp.maxIdle=5 dbcp.maxActive=20 dbcp.minEvictableIdleTimeMillis=180000 dbcp.timeBetweenEvictionRunsMillis=360000 dbcp.defaultAutoCommit=false dbcp.testOnBorrow=true dbcp.testOnReturn=true dbcp.testWhileIdle=true dbcp.validationQuery=SELECT 1 FROM DUAL
4.mybatis-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> <!-- 设定MyBatis在运行时的行为方式 --> <settings> <setting name="cacheEnabled" value="false" /> <setting name="lazyLoadingEnabled" value="false" /> <setting name="aggressiveLazyLoading" value="true" /> <setting name="multipleResultSetsEnabled" value="true" /> <setting name="useColumnLabel" value="true" /> <setting name="useGeneratedKeys" value="false" /> <setting name="autoMappingBehavior" value="PARTIAL" /> <setting name="defaultExecutorType" value="SIMPLE" /> <setting name="defaultStatementTimeout" value="10000" /> </settings> <!-- 类型别名是为Java类型命名一个短的名字, 它只和XML配置有关,用来减少类完全限定名的多余部分 --> <typeAliases> <typeAlias alias="BscInfo" type="com.cattsoft.nxii.api.entity.BscInfo"/> </typeAliases> <mappers> <mapper resource="com/cattsoft/baseplatform/report/persistence/CommonMapper.xml"/> </mappers> </configuration>
5.web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用","号或空格分隔 此参数用于后面的Spring Context Loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/applicationContext.xml </param-value> </context-param> <!-- Struts2 过滤器 --> <filter> <filter-name>struts2CleanupFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter> <filter-name>struts2Filter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2CleanupFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
6.applicationContext.xml配置文件中整合进mybatis-config.xml文件,之后再把applicationContext.xml文件整合进web.xml文