`

spring3.0+struts2.0+mybatis3.2+jta+xapool配置文件示例

阅读更多

 

 

spring 中 mybatis的配置

 

1、独立的mybatis属性配置(MyBatisSqlMapConfig.xml)

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 查看mybatis.dtd,发现整合 spring 以后 只有settings 和 typeAliases 保留其他属性都会被spring覆盖 -->
	<!-- settings 属性 -->
	<settings>
		<setting name="cacheEnabled" value="true" />
		<!-- Enables automatic mapping from classic database column names A_COLUMN 
			to camel case classic Java property names aColumn. -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>
	
	<!-- typeAliases 属性 -->
	<typeAliases>
		<package name="com.lw.domain"/>
	</typeAliases>
</configuration>

 

 

2、数据源和事物配置(applicationContext-database.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">


	<!-- 测试库数据源 -->

	<bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown" >
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="oracle.jdbc.OracleDriver" />
				<property name="url" value="jdbc:oracle:thin:@192.168.249.86:1521:eis11g" />
			</bean>
		</property>
		<property name="user" value="bpw"/>
		<property name="password" value="bpw"/>
		<property name="jdbcTestStmt" value=" select 0 from dual "/>
	</bean>

        <!-- JotmFactoryBean是本地实例,具体代码在文章的最后面 -->
	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>
	
	<!-- 注解:事物 -->
	<tx:annotation-driven transaction-manager="transactionManager"  />

</beans>

  

 

 

3、mybatis整合到spring中,(applicationContext-mybatis.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">


	<!-- scan for mappers of oracle and let them be autowired -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.lw.persistence.mapper"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:context/MyBatisSqlMapConfig.xml"/>
		<!-- 类型别名(entity等) package -->
		<!-- 类型别名package配置可以在这里,或者在classpath:context/MyBatisSqlMapConfig.xml配置文件里面  -->
<!-- 		<property name="typeAliasesPackage" value="com.lw.domain"/> -->
	</bean>

</beans>

 

 

4、spring 的一些配置 (applicationContext-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" 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">


	<context:property-placeholder location="classpath:*/*.properties" ignore-unresolvable="true" />
	
	<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
	<context:component-scan base-package="com.lw" />

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

</beans>

 

 

5、struts2.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="user" extends="struts-default" namespace="/user">
		<action name="user" class="com.lw.action.UserAction">
			<result name="success">/index.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
	</package>
	
</struts>

 

 

6、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"
	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">

	<!-- spring 配置文件加载 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:context/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>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.html</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.htm</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>
	
	<!-- spring 监听,启动程序时加载spring配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- session过期时间m -->
	<session-config>
		<session-timeout>240</session-timeout>
	</session-config>

	<welcome-file-list>
		<welcome-file>login.html</welcome-file>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

 

 

 

注:(org.springframework.transaction.jta.JotmFactoryBean.java)

          org.springframework.beans.factory.FactoryBean.java

     org.springframework.beans.factory.DisposableBean.java

 

/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.jta;

import javax.naming.NamingException;

import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

/**
 * FactoryBean that retrieves the JTA UserTransaction/TransactionManager for
 * ObjectWeb's <a href="http://jotm.objectweb.org">JOTM</a>. Will retrieve an
 * already active JOTM instance if found (e.g. if running in JOnAS), else create
 * a new local JOTM instance. The same object implements both the UserTransaction
 * and the TransactionManager interface, as returned by this FactoryBean.
 *
 * <p>A local JOTM instance is well-suited for working in conjunction with
 * ObjectWeb's <a href="http://xapool.experlog.com">XAPool</a>, e.g. with bean
 * definitions like the following:
 *
 * <pre>
 * &lt;bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/&gt;
 *
 * &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
 *   &lt;property name="userTransaction" ref="jotm"/&gt;
 * &lt;/bean&gt;
 *
 * &lt;bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"&gt;
 *   &lt;property name="transactionManager" ref="jotm"/&gt;
 *   &lt;property name="driverName" value="..."/&gt;
 *   &lt;property name="url" value="..."/&gt;
 * &lt;/bean&gt;
 *
 * &lt;bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"&gt;
 *   &lt;property name="dataSource" ref="innerDataSource"/&gt;
 *   &lt;property name="user" value="..."/&gt;
 *   &lt;property name="password" value="..."/&gt;
 *   &lt;property name="maxSize" value="..."/&gt;
 * &lt;/bean&gt;</pre>
 *
 * Uses JOTM's static access method to obtain the JOTM Current object, which
 * implements both the UserTransaction and the TransactionManager interface.
 *
 * @author Juergen Hoeller
 * @since 21.01.2004
 * @see JtaTransactionManager#setUserTransaction
 * @see JtaTransactionManager#setTransactionManager
 * @see org.objectweb.jotm.Current
 */
  public class JotmFactoryBean implements FactoryBean, DisposableBean {

   private Current jotmCurrent;

   private Jotm jotm;


      public JotmFactoryBean() throws NamingException {
       // Check for already active JOTM instance.
       this.jotmCurrent = Current.getCurrent();

       // If none found, create new local JOTM instance.
          if (this.jotmCurrent == null) {
           // Only for use within the current Spring context:
           // local, not bound to registry.
           this.jotm = new Jotm(true, false);
           this.jotmCurrent = Current.getCurrent();
       }
   }

   /**
    * Set the default transaction timeout for the JOTM instance.
    * <p>Should only be called for a local JOTM instance,
    * not when accessing an existing (shared) JOTM instance.
    */
      public void setDefaultTimeout(int defaultTimeout) {
       this.jotmCurrent.setDefaultTimeout(defaultTimeout);
   }

   /**
    * Return the JOTM instance created by this factory bean, if any.
    * Will be <code>null</code> if an already active JOTM instance is used.
    * <p>Application code should never need to access this.
    */
      public Jotm getJotm() {
       return jotm;
   }


      public Object getObject() {
       return this.jotmCurrent;
   }

      public Class getObjectType() {
       return this.jotmCurrent.getClass();
   }

      public boolean isSingleton() {
       return true;
   }

   /**
    * Stop the local JOTM instance, if created by this FactoryBean.
    */
      public void destroy() {
          if (this.jotm != null) {
           this.jotm.stop();
       }
   }

}

 

java程序资源在附件, 表结构可以到http://davidhhs.iteye.com/admin/blogs/2037902

jar包没有导入太多了。

 

 

0
0
分享到:
评论

相关推荐

    Spring 3.0+Struts2+Mybatis 3 + p6spy 平台框架

    这是自己整合的Spring 3.0+Struts2+Mybatis 3 + p6spy +ehcache的平台框架,内含一点示例代码,目前ehcache没有使用。直接编译后发布就能用 测试环境基于JDK1.6+Tomcat 6.0. 大家拿到后请根据实际情况修改 ...

    spring3.0+struts2+mybatis3.0整合

    本话题主要关注的是"Spring 3.0"、"Struts 2" 和 "MyBatis 3.0" 这三大主流Java开源框架的集成,以实现增、删、查、改(CRUD)以及LIKE查询等基本功能。下面我们将详细探讨这些框架各自的功能以及它们如何协同工作。...

    spring+struts2+hibernate+mybatis

    一个简单的spring+struts2+hibernate+mybatis整合(数据库脚本放在项目资源文件的sql目录下) 因为没想好mvc用springmvc好,还是struts2好 所以没有整合进去

    Spring3.0+struts2+Mybatis3.2集成联表操作

    最近要用到Mybatis,在网上找了很久,很少有提到联表查询具体该怎么操作的,这里做了一个联表操作的例子,跟大家分享,数据库用的是oracle,表没有上传,在oracle里面的scott用户里面,有emp和dept表,这里是完全一模一样的,...

    mybatis3.0+spring3.0+struts2整合开发的一个小权限管理系统

    mybatis3.0+spring3.0+struts2整合开发的一个小权限管理系统,里面有搜索提示功能,导入导出excel文件,ajax异步刷新,拦截器控制,freemarker等,表关系很复杂,特附上表关系图

    隐患排查系统源码+项目说明+数据库(基于Spring3.2+Struts2.3+Mybatis+Mysql5.7).zip

    【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末...隐患排查系统源码+项目说明+数据库(基于Spring3.2+Struts2.3+Mybatis+Mysql5.7)...

    Struts2+Spring3.0+MyBatis3.0平台搭建spring+json+gson+mysql,经典版本spring3.0+,完整架包

    标题中的"Struts2+Spring3.0+MyBatis3.0平台搭建"涉及到的是一个常见的企业级Java Web开发框架组合。这个平台基于Struts2作为MVC框架,Spring3.0作为核心容器,负责依赖注入(DI)和面向切面编程(AOP),而MyBatis...

    struts2.0+spring3.0+mybatis3框架整合

    Struts2.0、Spring3.0和MyBatis3是Java Web开发中常见的三大框架,它们各自在应用程序的不同层次上发挥着重要作用。Struts2作为MVC(模型-视图-控制器)框架,主要负责处理请求和控制业务流程;Spring则是一个全面的...

    Spring+Struts2.0+Mybatis

    在Java开发领域,Spring、Struts2.0和Mybatis是三个非常重要的开源框架,它们分别专注于不同层面的问题。Spring作为一个全面的轻量级应用框架,提供了依赖注入(DI)和面向切面编程(AOP)等核心特性,极大地简化了...

    Spring3.0+Struts2+Mybatis3

    附件有3个project,分三个步骤对SSMy进行了搭建,先是struts2,再进行spring整合struts,第三个整合mybatis,以及注释,到数据库mysql,给正在初学的和已经忘记怎么搭建框架的coder,共享

    Struts+Spring+Mybatis+EasyUI(jQuery)注解案例

    Struts+Spring+Mybatis+EasyUI(jQuery)注解案例。采用注解的方式跟踪代码依赖性,实现替代配置文件功能。里面包含了平时用的增删改查及分页,分页查询时用的是存储过程,提高了数据库的性能。代码简单、易懂。

    基于注解的Spring+Struts2+Mybatis+Maven项目

    【基于注解的Spring+Struts2+Mybatis+Maven项目详解】 在现代Web开发中,Spring、Struts2、Mybatis和Maven是四个非常重要的组件,它们各自负责不同的职责,共同构建了一个高效、灵活且易于维护的Java Web应用。这个...

    基于SpringBoot+Spring+SpringMvc+Mybatis+Shiro+Redis 开发单点登录管理系统源码

    基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...

    spring2.0+struts2.0+ibatis2.3完整整和

    1. **配置文件**:如`struts.xml`(Struts2配置)、`spring-context.xml`(Spring配置)、`sqlMapConfig.xml`(iBatis配置)等,定义了各个框架的组件和行为。 2. **模型类(Model)**:Java类,代表业务对象和数据...

    struts2+Spring3.0+Mybatis组合(纯注解方式)

    Struts2、Spring3.0和Mybatis是Java Web开发中的三大框架,它们组合在一起能够构建出高效、灵活的企业级应用程序。在这个半成品项目中,全部依赖于注解来简化配置,提供更简洁的代码结构。 **Struts2** 是一个基于...

    Spring+Struts2.0实例代码

    Spring 和 Struts 2.0 是 Java Web 开发中两个重要的框架,它们分别负责不同的职责。Spring 是一个全面的后端框架,提供了依赖注入、面向切面编程、事务管理等功能,而 Struts 2.0 是一个前端控制器框架,主要用于...

    struts2+spring+mybatis+easyui的实现

    总的来说,"struts2+spring+mybatis+easyui"的实现是一个标准的Java Web项目结构,它利用Maven进行构建管理,通过整合四个组件,实现了后端的业务逻辑处理、数据访问和前端的用户界面展示。这种架构在实际开发中具有...

    struts2+spring3.0+mybatis3.0.4集成的邮件发送实例(可上传附件)

    Struts2、Spring3.0和Mybatis3.0.4是Java开发中常见的三大框架,它们各自在应用程序的不同层面提供了强大的支持。本实例将详细讲解如何在这三个框架基础上实现一个邮件发送功能,同时支持附件上传。对于Java开发者来...

Global site tag (gtag.js) - Google Analytics