您的位置:首页 > 编程语言 > Java开发

关于搭建spring struts ibatis框架

2014-01-15 11:15 429 查看
工作中一直都是在用同事搭建好的框架来实现功能,自己从来也没有搭建过框架,本人的技术很一般,所以在时间充足的情况下,自己尝试的搭建了一下,经测试可以正常使用,如有不好的地方希望大家多多指教互相学习,谢谢!

开始贴代码:

框架中用的数据库mysql

首先web.xml中的配置

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>testSSI</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:applicationContext.xml</param-value>

</context-param>

<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>*.action</url-pattern>

</filter-mapping>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

根据自己不文件名字相应改动一下

下面是spring文件

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"

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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- DataSource -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />

<property name="url" value="jdbc:mysql://192.168.1.126:3306/ywyd" />

<property name="username" value="ywyd" />

<property name="password" value="ywyd" />

<property name="maxWait" value="5000" />

<property name="initialSize" value="2" />

<property name="maxActive" value="10" />

<property name="maxIdle" value="3" />

<property name="minIdle" value="1" />

<!-- <property name="removeAbandoned" value="true" />

<property name="removeAbandonedTimeout" value="100" />

<property name="logAbandoned" value="false" /> -->

</bean>



<bean id="testTM"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- SqlMapClient -->

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

<property name="configLocation" value="classpath:SqlMapConfig.xml" />

<property name="dataSource" ref="dataSource" />

</bean>

<bean id="testaction" class="com.test.action.testaction" scope="prototype">

<property name="tser" ref="testservice"></property>

</bean>

<bean id="testservice" class="com.test.service.testservice" scope="prototype">

<property name="tdao" ref="testdao"></property>

</bean>

<bean id="testdao" class="com.test.dao.testdao">

<property name="sqlMapClient" ref="sqlMapClient"/>

</bean>





</beans>

接下来是SqlMapConfig.xml

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

<!DOCTYPE sqlMapConfig

PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"

"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

<settings useStatementNamespaces="true"/>

<sqlMap resource="TestMap.xml" />

</sqlMapConfig>

下面是struts.xml

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

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="default" extends="struts-default" >

<action name="testaction" class = "testaction" >

<result name="success">/index.jsp</result>

</action>

</package>

</struts>

这是我个人在搭建中写的,action中对应的方法自己相应改一下就可以了!

我的全部代码已贴出,复制后改点东西可以直接用,如有不好的地方欢迎提出互相学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐