ssi(spring+struts1+ibatis)整合

配置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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>s1_s2_i2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  	<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>
	
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
  
  
</web-app>

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/root" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	
	
	<bean id="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
	</bean>
	
	
	<bean id="abstractDao" abstract="true">
		<property name="sqlMapClient" ref="sqlMapClient" />
	</bean>
	
	
	<!-- 定义Struts配置 -->
	<bean name="/test" class="com.action.TestAction">
		<property name="service" ref="service" />
	</bean>
	
	
	<bean name="service" class="com.service.TestService">
		<property name="dao" ref="dao" />
	</bean>
	
	
	<bean name="dao" class="com.dao.UserDaoImpl" parent="abstractDao"></bean>

</beans>

 

配置sqlmap.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="system">

	<typeAlias alias="User" type="com.action.TestForm"/>

	<select id="select_user_list" parameterClass="hashmap" resultClass="hashmap">
		select * from user
	</select>
	
	
</sqlMap>

  

配置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  
		cacheModelsEnabled="true"    
		enhancementEnabled="true"  
		lazyLoadingEnabled="true"  
		errorTracingEnabled="true"  
		maxRequests="32"  
		maxSessions="10"  
		maxTransactions="5"  
		useStatementNamespaces="true"  
	/>
  <sqlMap resource="sqlmap.xml"/>
</sqlMapConfig>

 配置struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
	<form-beans>
		<form-bean name="testForm" type="com.action.TestForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/test" name="testForm" scope="request"
			type="org.springframework.web.struts.DelegatingActionProxy">
			<forward name="success" path="/success.jsp" />
		</action>
	</action-mappings>

	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
	</plug-in>
</struts-config>

 javabean类

TestForm.java

package com.action;

import org.apache.struts.action.ActionForm;

public class TestForm extends ActionForm {

	/**
	 * javabean类,也就是实体类。
	 */
	private static final long serialVersionUID = 1L;

	protected String str1 = null;

	protected String str2 = null;

	public String getStr1() {
		return str1;
	}

	public void setStr1(String str1) {
		this.str1 = str1;
	}

	public String getStr2() {
		return str2;
	}

	public void setStr2(String str2) {
		this.str2 = str2;
	}
}

 dao类

UserDaoImpl.java

package com.dao;

import java.util.HashMap;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class UserDaoImpl extends SqlMapClientDaoSupport {
	/*
		dao层,也就是持久层。
	*/
	@SuppressWarnings("rawtypes")
	public List select(HashMap input) {
		System.out.println("-------dao-------");
		List list = getSqlMapClientTemplate().queryForList("system.select_user_list", input);
		System.out.println("list="+list);
		return list;
	}
}

 service类

TestService.java

package com.service;

import java.util.HashMap;

import org.apache.struts.actions.DispatchAction;

import com.dao.UserDaoImpl;

public class TestService extends DispatchAction {

/*	
	业务层,也就是service层。
	*/
	private UserDaoImpl dao;

	public UserDaoImpl getDao() {
		return dao;
	}

	public void setDao(UserDaoImpl dao) {
		this.dao = dao;
	}

	@SuppressWarnings("rawtypes")
	public void service() {
		System.out.println("--------service-------");
		dao.select(new HashMap());
	}
}

 action类

TestAction.java

package com.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.service.TestService;


public class TestAction extends DispatchAction {
	
	private TestService service;

	public TestService getService() {
		return service;
	}

	public void setService(TestService service) {
		this.service = service;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("----------进入action了--------------");
		service.service();
		ActionForward forward = mapping.findForward("success");
		return forward;
	}
}

 

你可能感兴趣的:(struts1)