手机进销存管理系统--03基础框架

1.项目框架--Struts2
  • 引入Struts2核心jar包(struts2-core-2.3.12.jar,当然只引入核心包是不可以的,但是我们在测试struts2时,可以根据报错的提示,引入相应的jar包,直到启动时不再报错;或者找到以前做过的项目把用到struts2的jar直接copy即可)
  • 在web.xml中配置Struts2核心Filter--org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    <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>*.do</url-pattern>
    	</filter-mapping>
  • 在项目的classes路径下创建source folder--config,在config内创建struts.xml并创建我们测试的Action
    public class TestAction {
    	
    	private String name;
    
    	public String execute() {
    		System.out.println(name);
    		name = "Welcome " + name;
    		return "test";
    	}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    在struts.xml文件中配置testAction
    <constant name="struts.action.extension" value="do" />
    	<constant name="struts.devMode" value="false" />
    	
    	<package name="default" namespace="/" extends="struts-default">
    		<action name="testAction" class="com.mobile.web.action.TestAction">
    			<result name="test">/WEB-INF/jsp/index2.jsp</result>
    		</action>
    	</package>
  • 启动服务器进行测试

2.项目框架--Spring

  • 引入Spring的jar包和struts2整合Spring的插件(struts2-spring-plugin-2.3.12.jar)
  • 新建spring的xml文件--beans.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
    				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">
    	<!--  
    	<bean id="testAction" class="com.mobile.web.action.TestAction">
    		<property name="testService" ref="testService"></property>
    	</bean>
    	-->
    	
    	<!-- 确保id的名字即testService与Action中定义的属性的名称相同,因为默认是按照名称来注入 -->
    	<bean id="testService" class="com.mobile.service.impl.TestServiceImpl"></bean>
    
    </beans>
  • 在web.xml中配置监听器--用于在项目启动时监听beans.xml
    <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/classes/beans.xml
    		</param-value>
    	</context-param>
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
  • 测试

3.项目框架--IBatis

  • 引入IBatis jar包以及DBCP数据源和连接池jar包(ibatis-2.3.4.726.jar,commons-dbcp-1.4.jar,commons-pool-1.6.jar)
  • 在Spring配置文件中配置数据源DataSource,配置如下:
    <context:property-placeholder
    		location="WEB-INF/classes/jdbc.properties" />
    		
    	<bean id="dataSource"
    		class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="${driverName}"></property>
    		<property name="url" value="${url}"></property>
    		<property name="username" value="${username}"></property>
    		<property name="password" value="${password}"></property>
    		<property name="maxActive" value="30"></property>
    		<property name="maxIdle" value="10"></property>
    		<property name="minIdle" value="5"></property>
    		<property name="maxWait" value="5000"></property>
    	</bean>
  • 创建IBatis主配置文件SqlMapConfig.xml,并在Spring配置文件中声明
    <?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>
    
    	<sqlMap resource="com.mobile.domain.emp.xml" />
    
    </sqlMapConfig>
  • emp.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="emp">
    	<select id="findEmp" resultClass="java.lang.Integer">
    		select count(id) from emp where name=#value#
    	</select>
    </sqlMap>
  • SqlMapConfig.xml文件在Spring配置如下
    <bean id="sqlMapClient"
    		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    		<property name="configLocation"
    			value="/WEB-INF/classes/SqlMapConfig.xml">
    		</property>
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
  • 在beans.xml中注入commonDao
    public class CommonDaoImpl extends SqlMapClientTemplate implements CommonDao {
    
        public Object queryObject(String sqlId, Object parameter)
                throws SQLException {
            return getSqlMapClient().queryForObject(sqlId, parameter);
        }
    }
    <bean id="commonDao" class="com.mobile.dao.impl.CommonDaoImpl">
    		<property name="sqlMapClient" ref="sqlMapClient"></property>
    	</bean>
    	
    	
    	<!--  
    	<bean id="testAction" class="com.mobile.web.action.TestAction">
    		<property name="testService" ref="testService"></property>
    	</bean>
    	-->
    	
    	<!-- 确保id的名字即testService与Action中定义的属性的名称相同,因为默认是按照名称来注入 -->
    	<bean id="testService" class="com.mobile.service.impl.TestServiceImpl">
    		<property name="commonDao" ref="commonDao"></property>
    	</bean>
  • 书写CommonDaoImpl以及TestServiceImpl代码,并测试

你可能感兴趣的:(手机进销存管理系统--03基础框架)