框架版本:

struts2、spring2.5、ibatis

1,新建一个web project。

2,添加spring支持:将applicationContext.xml放在项目WebRoot/WEB-INF文件夹下

3,导入spring的jar包

4,导入的struts的jar包

5,加入ibatis jar包

6,配置applicationContext.xml文件添加以下代码

<!-- 引入参数配置文件 -->  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">         <list>             <value>classpath:sqlMapping.properties</value>         </list>    </property> </bean> 


<!-- 数据源配制 -->  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  <property name="driverClassName">  <value>${jdbc.driverClassName}</value>  </property>  <property name="url">  <value>${jdbc.url}</value>  </property>  <property name="username">  <value>${jdbc.username}</value>  </property>  <property name="password">  <value>${jdbc.password}</value>  </property>   </bean>   <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->   <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   <property name="configLocation" >   <value>classpath:SqlMapConfig.xml</value>   </property>   <property name="dataSource" ref="dataSource"/>  </bean> 


7,之后创建 sqlMapConfig.xml文件和sqlMapping.properties文件



sqlMapConfig.xml文件代码为:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  "http://www.ibatis.com/dtd/sql-map-config-2.dtd">  <sqlMapConfig>  <settings  cacheModelsEnabled="true"  enhancementEnabled="true"  lazyLoadingEnabled="true"  errorTracingEnabled="false"  maxRequests="32"  maxSessions="10"  maxTransactions="5"  useStatementNamespaces="false"  />  <sqlMap resource="com/naxl/oa/model/tb_User.xml" />  <sqlMap resource="com/naxl/oa/model/tb_Department.xml" />  </sqlMapConfig>    sqlMapping.properties文件代码为:  jdbc.driverClassName=com.mysql.jdbc.Driver  jdbc.url=jdbc:mysql://192.168.1.104:3306/naxloa  jdbc.username=root  jdbc.password=admin

8,接下来就要配置web.xml了,在web.xml添加以下代码



<!-- 配置Spring -->  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext.xml</param-value>  </context-param>    <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>

如果你的web.xml文件中没有配置struts,则需添加如下代码

<filter>  <filter-name>struts2</filter-name>  <filter-class>  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  </filter-class>  <init-param>  <param-name>config</param-name>  <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>    </init-param>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>
9,Struts.xml文件代码为:

<?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>  <package name="default" extends="json-default">    <action name="" class="" method="">  <result name=""> </result>  <result name=""> </result>  </action>   </package>  </struts>
OK!