`
san_yun
  • 浏览: 2606491 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Spring对Ibatis的封装

 
阅读更多
spring 对ibatis的封装通过SqlMapClientTemplate实现。

通常如果我们直接使用ibatis的sqlMapClient进行insert操作或者queryForObject都会抛出sqlSQLException:

	 try {
			cli.insert("MS-INSERT-UNREADCOUNT", unreadCount);
		} catch (SQLException e) {
			logger.error("create UnReadCount: ", e);
		}

处理这种错误很没意思,通过spring的sqlMapClientTemplate只需一行代码:
sqlMapClientTemplate.insert("MS-INSERT-UNREADCOUNT", unreadCount);

看看spring如何帮我们封装的:
	public <T> T execute(SqlMapClientCallback<T> action) throws DataAccessException {
		Assert.notNull(action, "Callback object must not be null");
		Assert.notNull(this.sqlMapClient, "No SqlMapClient specified");

		// We always need to use a SqlMapSession, as we need to pass a Spring-managed
		// Connection (potentially transactional) in. This shouldn't be necessary if
		// we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
		// we still need it to make iBATIS batch execution work properly: If iBATIS
		// doesn't recognize an existing transaction, it automatically executes the
		// batch for every single statement...

		SqlMapSession session = this.sqlMapClient.openSession();
		if (logger.isDebugEnabled()) {
			logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
		}
		Connection ibatisCon = null;

		try {
			Connection springCon = null;
			DataSource dataSource = getDataSource();
			boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);

			// Obtain JDBC Connection to operate on...
			try {
				ibatisCon = session.getCurrentConnection();
				if (ibatisCon == null) {
					springCon = (transactionAware ?
							dataSource.getConnection() : DataSourceUtils.doGetConnection(dataSource));
					session.setUserConnection(springCon);
					if (logger.isDebugEnabled()) {
						logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
					}
				}
				else {
					if (logger.isDebugEnabled()) {
						logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
					}
				}
			}
			catch (SQLException ex) {
				throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
			}

			// Execute given callback...
			try {
				return action.doInSqlMapClient(session);
			}
			catch (SQLException ex) {
				throw getExceptionTranslator().translate("SqlMapClient operation", null, ex);
			}
			finally {
				try {
					if (springCon != null) {
						if (transactionAware) {
							springCon.close();
						}
						else {
							DataSourceUtils.doReleaseConnection(springCon, dataSource);
						}
					}
				}
				catch (Throwable ex) {
					logger.debug("Could not close JDBC Connection", ex);
				}
			}

			// Processing finished - potentially session still to be closed.
		}
		finally {
			// Only close SqlMapSession if we know we've actually opened it
			// at the present level.
			if (ibatisCon == null) {
				session.close();
			}
		}
	}


最后所有的insert,update,delete,query都通过execute被执行。
分享到:
评论

相关推荐

    访问数据库基本封装(spring/hibernate/ibatis)

    访问数据库基本封装(spring/hibernate/ibatis)

    Spring+ibatis+struts框架整合+springmvc数据接口封装含数据表可直接运行

    Spring+ibatis+struts框架整合+springmvc数据接口封装含数据表可直接运行

    ibatis封装

    ibatis封装

    封装通用的Spring3+Struts2+IBatis的CRUD

    封装通用的Spring3+Struts2+IBatis的CRUD

    Spring数据库访问之iBatis

    而iBatis可以为我们做的更多,比如对查询结果的封装等等。虽然不如全自动SQL方便,但是SQL的主动权却在我们开发人员的手中,对SQL优化的掌控则是很直接的。对于Hibernate和iBatis的其它讨论,不是我们探究的范围。...

    Spring开发指南

    Spring初探 准备工作 构建Spring基础代码 ... ibatis in Spring Aspect Oriented Programming AOP 概念 AOP in Spring Dynamic Proxy 与Spring AOP CGLib 与 Spring AOP AOP 应用 DAO Support Remoting

    SSI(struts2+ibatis+spring)

    一个SSI整合的小例子,CRUD都有,架包全有,导入即可运行,数据库用的是mysql,dao层用了江南白衣的思想,已封装,在此拓展就行,希望能对你有所帮助

    spring jar 包详解

    (8) spring-orm.jar 这个jar文件包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖 spring-dao.jar...

    spring 3.0 jar 所有开发包及开发项目实例

    org.springframework.asm-3.0.0.M4.jar: 提供对ASM(一个字节码框架)的简单封装 org.springframework.expression-3.0.0.M4.jar: spring表达式语言 org.springframework.test-3.0.0.M4.jar: spring提供的一个...

    spring4.3.9相关jar包

    spring-jdbc.jar(必须) :这个jar 文件包含对Spring 对JDBC 数据访问进行封装的所有类。 外部依赖spring-beans,spring-dao。 spring-web.jar(必须) :这个jar 文件包含Web 应用开发时,用到Spring 框架时所需...

    spring-framework-3.0.0.M4-with-docs

    org.springframework.asm-3.0.0.M4.jar: 提供对ASM(一个字节码框架)的简单封装 org.springframework.expression-3.0.0.M4.jar: spring表达式语言 org.springframework.test-3.0.0.M4.jar: spring提供的一个...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

     这个jar文件包含对Spring对JDBC数据访问进行封装的所有类。 (8) spring-orm.jar  这个jar文件包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在...

    最新最全的spring开发包

    这个jar文件包含Spring对DAO特性集进行了扩展,使其支持 iBATIS、JDO、OJB、TopLink,因为Hibernate已经独立成包了,现在不包含在这个包里了。这个jar文件里大部分的类都要依赖 spring-dao.jar里的类,用这个包时你...

    Spring基础与快速入门

    其他还有一些特点不是Spring的核心,这里只做简单陈述,如:对JDBC的封装与简化,提供事务管理功能,对O/R mapping工具(hibernate、iBATIS)的整合;提供MVC解决方案,也可以与其他web框架(Struts、JSF)进行整合...

    ibatis 开发指南(pdf)

    使用ibatis 提供的ORM 机制,对业务逻辑实现人员而言,面对的是纯粹的Java 对象, 这一层与通过Hibernate 实现ORM 而言基本一致,而对于具体的数据操作,Hibernate 会自动生成SQL 语句,而ibatis 则要求...

    Jquery+Spring3+Struts2+Ibatis3框架源代码工程(含权限)

    自己封装包,工具 博文链接:https://atgoingguoat.iteye.com/blog/1600676

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    Spring面试题

    ☆ Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。 ☆ Spring Web 模块:...

Global site tag (gtag.js) - Google Analytics