Struts2,Spring,Ibatis的整合以及一些注意事项
通过MyEclipse 导入struts2和spring,注意把选择包的时候选择copy,ibatis需要自己去导入
Web.xml配置如下<!-- 添加struts2支持 --><filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>(/*:表示匹配所有的)
</filter-mapping>
<!-- 添加spring支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>(加载spring的配置文件)
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 默认启动 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Struts.xml配置如下<packagename="login" namespace="/login"extends="struts-default">
<actionname="login" class="cusAction">(这里的class="cusAction"与applicationContext.xml中的”cusAction”是相对应的)
<result>/index.jsp</result>
</action>
</package>
applicationContext.xml配置如下<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<propertyname="url"value="jdbc:mysql://localhost/ysh"></property>
<propertyname="username" value="cus"></property>
<propertyname="password" value="cus"></property>
</bean>
<beanid="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<propertyname="configLocation">
<value>/WEB-INF/sqlMapConfig.xml</value>
</property>
<propertyname="dataSource">
<reflocal="dataSource" />
</property>
</bean>(这个表示通过ibatis连接mysql数据库)
<beanname="cusAction" class="com.lkd.xj.bsms.action.CustomerAction">
<propertyname="customerBizIn">
<refbean="cusBiz" />
</property>
</bean>
<beanid="cusBiz"class="com.lkd.xj.bsms.biz.impl.CustomerBizImpl">
<propertyname="customerDaoIn">
<refbean="cusDao" />
</property>
</bean>
<beanid="cusDao"class="com.lkd.xj.bsms.dao.impl.CustomerDaoImpl">
<propertyname="sqlMapClient">
<reflocal="sqlMapClient" />
</property>
</bean>
sqlMapConfig.xml配置如下<sqlMapConfig>
<sqlMapresource="com/lkd/xj/bsms/entity/customer.xml" />(指向实体类的配置文件,ibatis是半自动化,需要自己去手工来写sql语句)
</sqlMapConfig>
customer.xml配置如下<sqlMapnamespace="Customer">
<selectid="getCusBean"parameterClass="com.lkd.xj.bsms.entity.Customer"
resultClass="com.lkd.xj.bsms.entity.Customer">(一个表示参数类型,一个表示结果集输出类型)
selectname,password from customer where name=#name# and password=#password#
</select>
</sqlMap>
特别注意包的问题struts2-spring-plugin-2.1.6.jar这个包MyEclipse不回帮你导入,你需要自己去下载导入工程,这样才正常启动