struts1.x+ibatis+spring配置整合

时间:2020-12-24 21:41:23

SSH想必为大家所熟悉,其应用也相当之广泛;SSI其实也是一种很好的组合,应用与小中型系统,且简单快捷,下面就说一下struts+spring+ibatis的配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <context-param>

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

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

</context-param> 
  <listener>
    <description>上下文监听</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <description>Request,Session监听</description>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts-config.xml

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

<!--<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"  "http://struts.apache.org/dtds/struts-config_1_3.dtd">

--><!-- 上面的表头,如果不能上网就会报错。换成下面的 -->
<!--<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
--><struts-config>


<!-- ================================================ Form Bean 定义 -->

    <form-beans>
    	<!-- MESSSEND -->
    	<form-bean name="LoginForm" type="com.ods.form.UserForm"></form-bean>
    	</form-beans>
	<action-mappings>
       <action
        attribute="LoginForm"
        name="LoginForm"
        path="/login"
        parameter="o"
        scope="request"
        validate="true"
        input="/index.jsp"
        type="com.ods.action.LoginAction">
        <forward name="list" path="/list.jsp"/>
       </action>
       
    </action-mappings>
   

  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />


  <message-resources parameter="MessageResources" />
</struts-config>


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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<import resource="ods_applicationContext.xml"/>
	<!-- 配置数据源 -->
	<bean id="dataSource" 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}" />
	</bean>
	
	<!--  数据库属性文件  -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>/WEB-INF/DataBase.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- 事物管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>

	<!-- SqlMap配置 -->
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"
			value="WEB-INF/sql-map-config.xml">
		</property>
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	
</beans>

ods_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:jee="http://www.springframework.org/schema/jee"
    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/jee
                        http://www.springframework.org/schema/jee/spring-jee-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"> 
                        
	
	
	<!-- Dao配置-->
	<!-- ####################################################### -->
	<bean id="userdao" class="com.ods.dao.imp.UserDaoImp">
 		<property name="sqlMapClient" ref="sqlMapClient"></property>
	</bean>

	<!-- Service配置 -->
	<!-- ####################################################### -->
	<bean id="userservice" class="com.ods.service.imp.UserServiceImp">
		<property name="userdao" ref="userdao"></property>
	</bean>

	<!-- Action配置 -->
	<!-- ####################################################### -->
	<bean name="/login" class="com.ods.action.LoginAction">
    	<property name="userserviceimp" ref="userservice"></property>
    </bean>
</beans>

sql-map-config.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>
<sqlMap resource="com/ods/xml/User.xml"/>
</sqlMapConfig>

DataBase.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ods_user

jdbc.username=root
jdbc.password=123