spring4.1.2、mybatis3.0.5、struts2.3.16.3搭建学习例子

此种搭建框架的例子已有很多,本文只供参考。

环境

myeclipse5.5GA、mysql、tomcat6.0、jdk6.0

版本

spring4.1.2、mybatis3.0.5、struts2.3.16.3,可去官网下载。

jar包



application.properties

#mysql version database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/website?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

#hibernate settings
hibernate.show_sql=true
hibernate.format_sql=true

#dbcp settings
dbcp.initialSize=5
dbcp.maxActive=20
dbcp.maxIdle=10
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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<description>Spring公共配置 </description>

	<!-- 定义受环境影响易变的变量 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="false" />
		<property name="locations">
			<list>
				<value>classpath*:/config/application.properties</value>
			</list>
		</property>
	</bean>

	<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
	<context:component-scan base-package="com.blz.*" />
	
   <!-- 定义数据库连接池数据源bean destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用 -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <!-- 设置JDBC驱动名称 -->
       <property name="driverClassName" value="${jdbc.driver}" />
       <!-- 设置JDBC连接URL -->
       <property name="url" value="${jdbc.url}" />
       <!-- 设置数据库用户名 -->
       <property name="username" value="${jdbc.username}" />
       <!-- 设置数据库密码 -->
       <property name="password" value="${jdbc.password}" />
		<!-- Connection Pooling Info -->
       <!-- 设置连接池初始值 -->
		<property name="initialSize" value="${dbcp.initialSize}" />
		<!-- 设置连接池最大连接数 -->
		<property name="maxActive" value="${dbcp.maxActive}" />
		<!-- 设置连接池最大空闲连接数 -->
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="defaultAutoCommit" value="false" />
   </bean>
   
	<!-- 事务管理器配置,单数据源事务 -->
	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />

	<!-- transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- enable autowire -->
	<context:annotation-config />

	<!-- enable transaction demarcation with annotations -->
	<tx:annotation-driven />

	<!-- 定义 SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
		<property name="typeAliasesPackage"
			value="com.blz.common.bean" />
			<!-- value可以用","号分割配置多个 -->
	</bean>

	<!-- 扫描映射文件,自动注入 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage"
			value="com.blz.common.persistence" />
			<!-- value可以用","号分割配置多个 -->
	</bean>
</beans>
sturts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<constant name="struts.convention.default.parent.package" value="crud-default" />
	<constant name="struts.convention.package.locators" value="action,web" />
	<constant name="struts.convention.package.locators.basePackage" value="" />
	<!-- 用于CRUD Action的parent package -->
	<package name="crud-default" extends="convention-default">
		<!--
			基于paramsPrepareParamsStack, 增加store
			interceptor保证actionMessage在redirect后不会丢失
		-->
		<interceptors>
			<interceptor-stack name="crudStack">
				<interceptor-ref name="store">
					<param name="operationMode">AUTOMATIC</param>
				</interceptor-ref>
				<interceptor-ref name="paramsPrepareParamsStack" />
			</interceptor-stack>
		</interceptors>

		<default-interceptor-ref name="crudStack" />

	</package>

	<!--
		使用Convention插件,实现约定大于配置的零配置文件风格. 特殊的Result路径在Action类中使用@Result设定.
	-->
</struts>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<!-- 加载spring配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/config/applicationContext.xml</param-value>
	</context-param>

	
	<!-- 启用spring监听 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	
	<!-- 添加对struts2的支持 -->  
	<filter>  
		<filter-name>struts2</filter-name>  
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
		 <init-param>
            <param-name>config</param-name>
           <!-- 配置装载struts.xml路径,其中struts.xml放在/src/struts/下-->
            <param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
        </init-param>
	</filter> 
	<filter-mapping>  
		<filter-name>struts2</filter-name>  
		<url-pattern>*.action</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher> 
	</filter-mapping>
	
	
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
下载地址

http://download.csdn.net/detail/djl100/8228809



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值