论坛首页 Java企业应用论坛

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

浏览 21807 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-03-08   最后修改:2012-03-13

[2012年3月13日12:11:57 add  如果不想CTRL+C CTRL+V只想看效果的同志请移步至http://code.google.com/p/ssm-study/]

 

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

 

 

<?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内容如下:

 

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 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框架时用到的清单)

 

 

  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

 

 

 

  不知道大家是喜欢mybatis一些还是喜欢hibernate一些呢?

 

另外对于mybatis的批量插入,大家是怎么做的呢?如果单纯的mybatis,就很好弄,但是跟spring集成了,感觉有点困难了。

   发表时间:2012-03-09  
更喜欢myibatis多一点。
0 请登录后投票
   发表时间:2012-03-09  
打个包+pom.xml 吧
0 请登录后投票
   发表时间:2012-03-12  
楼主 能不能将配置好的发我一份  我始终对和Spring进行整合的事务管理那块 搞不懂呀
dong_junnan@163.com
3Q
0 请登录后投票
   发表时间:2012-03-12  
做项目,spl自己清楚肯定是比较好的,但是你在上面所说的myibatis批量插入指的是上面?list<Object>循环插入?还是?
0 请登录后投票
   发表时间:2012-03-12  
530247683@qq.com 谢谢
0 请登录后投票
   发表时间:2012-03-12  
threejin520 写道
做项目,spl自己清楚肯定是比较好的,但是你在上面所说的myibatis批量插入指的是上面?list<Object>循环插入?还是?

 

目前的批量插入解决方案是在dao层将list遍历循环调mybatis的插入api。 

我想了解的是有没有一个api,让我给他一个list,它批量帮我插入。

0 请登录后投票
   发表时间:2012-03-12  
dongjunnan 写道
楼主 能不能将配置好的发我一份  我始终对和Spring进行整合的事务管理那块 搞不懂呀
dong_junnan@163.com
3Q

 

SVN地址  :http://myfirst-cmcc.googlecode.com/svn/demo/ssm

0 请登录后投票
   发表时间:2012-03-12  
530247683 写道
530247683@qq.com 谢谢

 

SVN地址    http://myfirst-cmcc.googlecode.com/svn/demo/ssm

0 请登录后投票
   发表时间:2012-03-12  
不是喜欢谁的问题,是业务需要用什么,比如我们现在开发的互联网项目,hibernate肯定没ibatis好,开发速度方面挺纠结

企业开发什么的hibernate完胜
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics