Spring3.0和Mybatis的集成,含事务配置

相对于hibernate来说,比较喜欢mybatis一些。虽然mybatis没有hibernate那么成熟,但可以手动控制sql语句怎么长。废话不多说,上代码。欢迎拍砖。





Xml代码 
<?xml version="1.0" encoding="UTF-8"?> 
<beans default-autowire="byType" 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.0.xsd"> 
    <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="locations"> 
            <list> 
                <value>classpath:spring-jdbc.properties</value> 
            </list> 
        </property> 
    </bean> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${jdbc.driver}"> 
        </property> 
        <property name="url" value="${jdbc.url}"> 
        </property> 
        <property name="username" value="${jdbc.username}"> 
        </property> 
        <property name="password" value="${jdbc.password}"> 
        </property> 
    </bean> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> 
        <property name="dataSource" ref="dataSource"></property> 
        <property name="mapperLocations" value="classpath*:mappers-*.xml"></property> 
    </bean> 
    <!-- 由spring管理mybatis的事物 --> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource"></property> 
    </bean> 
    <!-- 定义拦截器,用来指定事物属性,级别,和异常处理 --> 
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
        <property name="transactionManager" ref="transactionManager"></property> 
        <property name="transactionAttributes"> 
            <props> 
                <!-- PROPAGATION_SUPPORTS: 如果已经存在事务,则加入事务;如果没有事务,则以非事务的方式执行;   
                PROPAGATION_MANDATORY: 使用当前事务, 如果没有, 则抛出异常;   
                PROPAGATION_REQUIRED: 新建事务,如果当前有事务, 则挂起; P  
                ROPAGATION_NOT_SUPPORTED:以非事务的方式执行, 如果当前有事务, 则挂起;   
                PROPAGATION_NEVER:以非事务的方式执行, 如果当前有事务,则抛出异常;   
                +/-Exception</prop> + 表示异常出现时事物提交 - 表示异常出现时事务回滚 --> 
                <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop> 
                <prop key="del*"> PROPAGATION_SUPPORTS</prop> 
                <prop key="update*">PROPAGATION_REQUIRED</prop> 
                <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> 
            </props> 
        </property> 
    </bean> 
    <!-- 用来定义那些类需要采用事物管理 spring 事物的动态代理类 BeanNameAutoProxyCreator 根据类名自动代理,接受表达式 --> 
    <bean id="BeanProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
        <property name="beanNames"> 
            <!-- 对类名以MybatisService结尾的类进行代理 --> 
            <value>*MybatisService</value> 
        </property> 
        <!-- 对代理类进行加载拦截器(实现通知的过程) --> 
        <property name="interceptorNames"> 
            <list> 
                <value>transactionInterceptor</value> 
            </list> 
        </property> 
    </bean> 
</beans> 




鉴于网络参数和鉴权信息是经常变的,所以单独拿出来放在属性文件中了。spring-jdbc.properties内容如下:



Java代码 
jdbc.driver=oracle.jdbc.driver.OracleDriver  
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl  
jdbc.username=admin  
jdbc.password=pwd 

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=admin
jdbc.password=pwd



Mybatis中的拦截器和一些bean的配置信息就放在SqlMapConfig.xml这个文件中了

Xml代码 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE configuration  
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> 
<configuration> 
    <!-- 要在environment之前 --> 
    <typeAliases> 
        <typeAlias type="com.jacarri.document.vo.WriteFileVo" alias="WriteFileVo" /> 
    </typeAliases> 
    <plugins> 
        <plugin interceptor="com.jacarri.common.filter.PaginationInterceptor" /> 
        <plugin interceptor="com.jacarri.common.filter.ColumnsControlInterceptor" /> 
    </plugins> 
</configuration> 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<!-- 要在environment之前 -->
<typeAliases>
<typeAlias type="com.jacarri.document.vo.WriteFileVo" alias="WriteFileVo" />
</typeAliases>
<plugins>
<plugin interceptor="com.jacarri.common.filter.PaginationInterceptor" />
<plugin interceptor="com.jacarri.common.filter.ColumnsControlInterceptor" />
</plugins>
</configuration>备注:两个interceptor,第一个是用来分页的,第二个是用来控制select语句中列名的,就不上代码了。想要的可以给我发消息。至于WriteFileVo就是一个普通的bean,如果你要用到缓存,记得bean要实现java.io.Serializable。





同时附上jar包清单(该清单是在搭建struts2+Spring3+Mybatis框架时用到的清单)





Java代码 
antlr-2.7.6.jar  
aopalliance.jar  
asm-2.2.3.jar  
asm-commons-2.2.3.jar  
backport-util-concurrent-3.1.jar  
cglib-nodep-2.1_3.jar  
commons-collections-3.1.jar  
commons-dbcp-1.2.1.jar  
commons-fileupload-1.2.1.jar  
commons-io-1.3.2.jar  
commons-lang-2.3.jar  
commons-logging-1.1.1.jar  
commons-pool-1.2.jar  
dom4j-1.6.1.jar  
ehcache-1.5.0.jar  
freemarker-2.3.15.jar  
jakarta-oro-2.0.8.jar  
javassist-3.12.0.GA.jar  
json-lib-2.1-jdk15.jar  
jta.jar  
log4j-1.2.16.jar  
mybatis-3.0.6-SNAPSHOT.jar  
mybatis-spring-1.0.2-SNAPSHOT.jar  
mysql-connector-java-5.1.17-bin.jar  
ognl-2.7.3.jar  
org.springframework.aop-3.1.0.M2.jar  
org.springframework.asm-3.1.0.M2.jar  
org.springframework.beans-3.1.0.M2.jar  
org.springframework.context-3.1.0.M2.jar  
org.springframework.core-3.1.0.M2.jar  
org.springframework.expression-3.1.0.M2.jar  
org.springframework.jdbc-3.1.0.M2.jar  
org.springframework.orm-3.1.0.M2.jar  
org.springframework.transaction-3.1.0.M2.jar  
org.springframework.web-3.1.0.M2.jar  
struts2-config-browser-plugin-2.2.1.jar  
struts2-convention-plugin-2.2.1.jar  
struts2-core-2.2.1.jar  
struts2-json-plugin-2.2.1.jar  
struts2-spring-plugin-2.2.1.jar  
xwork-core-2.2.1.jar 

你可能感兴趣的:(spring,AOP,struts)