现在的位置: 首页 > 综合 > 正文

Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【一】

2011年04月29日 ⁄ 综合 ⁄ 共 5490字 ⁄ 字号 评论关闭

开发环境:

System:Windows

WebBrowser:IE6+、Firefox3+

JavaEE Server:tomcat5.0.2.8、tomcat6

IDE:eclipse、MyEclipse 8

Database:MySQL

开发依赖库:

JavaEE5、Spring 3.0.5、Mybatis 3.0.4、myBatis-spring-1.0、Struts2.2.3、junit4.8.2、ext2.2.2

Email:hoojo_@126.com

Blog:http://blog.csdn.net/IBM_hoojo

http://hoojo.cnblogs.com/

上次介绍过Spring3、SpringMVC、MyBatis3整合,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/15/2016324.html

一、准备工作

1、 下载jar包

Struts2 jar下载:

http://labs.renren.com/apache-mirror//struts/library/struts-2.2.3-lib.zip

Spring3 jar下载:

http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE

MyBatis3 jar 下载:http://www.mybatis.org/java.html

2、 添加的jar包如下:

image

二、Spring、MyBatis整合

1、 需要的jar文件如下:

clip_image002

2、 在src目录中加入mybatis.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>

 

    <!-- 别名 -->

    <typeAliases>

        <typeAlias type="com.hoo.entity.Account" alias="account"/>

    </typeAliases>

    

</configuration>

上面的配置文件中,可以加入一些公共、常用的MyBatis方面的全局配置。如handler、objectFactory、plugin、以及mappers的映射路径(由于在applicationContext-common中的SqlSessionFactoryBean有配置mapper的location,这里就不需要配置)等。这个类的文件名称和下面的applicationContext-common.xml中的configLocation中的值对应,不是随便写的。

3、 在src目录中添加applicationContext-common.xml中加入内容如下:

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

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:aop="http://www.springframework.org/schema/aop" 

    xmlns:tx="http://www.springframework.org/schema/tx"

    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-3.0.xsd 

    http://www.springframework.org/schema/aop 

    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

    http://www.springframework.org/schema/tx  

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

    

    <!-- 配置DataSource数据源 -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

        <property name="url" value="jdbc:mysql://10.0.0.131:3306/ash2"/>

        <property name="username" value="dev"/>

        <property name="password" value="dev"/>

    </bean>

 

    <!-- 配置SqlSessionFactoryBean -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>

        <property name="configLocation" value="classpath:mybatis.xml"/>

        <!-- mapper和resultmap配置路径 --> 

        <property name="mapperLocations">

            <list>

                <!-- 表示在com.hoo.resultmap包或以下所有目录中,以-resultmap.xml结尾所有文件 --> 

                <value>classpath:com/hoo/resultmap/**/*-resultmap.xml</value>

                <value>classpath:com/hoo/entity/*-resultmap.xml</value>

                <value>classpath:com/hoo/mapper/**/*-mapper.xml</value>

            </list>

        </property>

    </bean>

    

    <!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource" />

    </bean>

 

    <!-- 配置事务的传播特性 -->

    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">

        <property name="transactionManager" ref="transactionManager" />

        <property name="transactionAttributes">

            <props>

                <prop key="add*">PROPAGATION_REQUIRED</prop>

                <prop key="edit*">PROPAGATION_REQUIRED</prop>

                <prop key="remove*">PROPAGATION_REQUIRED</prop>

                <prop key="insert*">PROPAGATION_REQUIRED</prop>

                <prop key="update*">PROPAGATION_REQUIRED</prop>

                <prop key="del*">PROPAGATION_REQUIRED</prop>

                <prop key="*">readOnly</prop>

            </props>

        </property>

    </bean>

    

    <!-- 通过扫描的模式,扫描目录在com/hoo/mapper目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.hoo.mapper"/>

        <property name="markerInterface" value="com.hoo.mapper.SqlMapper"/>

    </bean>

</beans>

DataSource,这里的是采用jdbc的dataSource;

SqlSessionFactory,是MyBatis团队提供整合Spring的SqlSession工厂Bean用它可以完成Spring和MyBatis的整合。SqlSessionFactoryBean需要注入DataSource,配置myBatis配置文件的location,以及配置mapper.xml和resultMap.xml文件的路径,可以用通配符模式配置。其实mapper里面是可以存放resultMap的内容的。由于resultMap文件的内容是和JavaBean及数据库表对象进行映射的。一般一张表、一个JavaBean(Model)对应一个resultMap文件。将resultMap独立出来提供可读性、维护性。

TransactionManager,事务管理器是采用jdbc的管理器。需要注入DataSource数据源,这里注入的数据源和SqlSessionFactory是同一个数据源。如果不同的数据源,事务将无法起到作用。

baseTransactionProxy,事务的传播特性才有spring提供的TransactionProxyFactoryBean这个事务代理工厂的拦截器类来完成。其他的暂时还没有可以支持事务配置的传播特性和隔离级别的方法,关于这里你可以参考:http://www.cnblogs.com/hoojo/archive/2011/04/15/2017447.html

MapperScannerConfigurer是MyBatis的mapper接口的扫描器,通过这个配置可以完成对指定basePackage报下的类进行扫描,如果这些类有继承SqlMapper这个类的,将会是MyBatis的接口。不需要单独配置mapper,而完成注入。

4、 在src目录添加applicationContext-beans.xml这个文件,文件内容如下:

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

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context"

    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-3.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 注解探测器 -->

    <context:component-scan base-package="com.hoo"/>

    

</beans>

这里面可以完成一下Action、Bean的注入配置

5、 JavaBean(Model、Entity)相关类、及resultMap.xml文件内容

Bean

package com.hoo.entity;

 

import java.io.Serializable;

import java.util.Date;

 

public class Account implements Serializable {

 

    private static final long serialVersionUID = -7970848646314840509L;

 

    private Integer accountId;

    private String username;

    private String password;

    private Date createTime;

    

    public Account() {

        super();

    }

    //getter、setter

抱歉!评论已关闭.