Spring4及已经的版本放弃了对ibatis的集成支持,那有什么办法可以将自己的框架迁移升级到spring4呢。
我这里有2个办法可供参考:
1、改造spring-orm包:
A、首先从spring-orm的jar包中将ibatis相关的class文件及包结构全部复制出来,以备后用。
B、更改工程的spring版本号到spring4
C、将已经复制出来的ibatis包结构全部拖入spring4中spring-orm包的响应位置
D、测试一下当前的应用,是不是已经可以了呢 ^_^
2、将spring直接升级到spring4.0,同时集成新的mybatis替代老版本的ibatis。
说明:这两种办法都各有优势,同时也各有不足,实际升级过程中需要权衡考虑利弊关系。
第一中方法,在架构升级过程中,确保了最小改动量的原则,但是会造成一个隐形的私有开发包,一旦架构再次进行spring版本升级,本次的升级操作无法平滑过渡,这种潜规则式的改造方式,需要通过口传心授的方式进行知识的传递,其实在一定程度上给后来的开发人员埋了一个不大不小的坑。
第二种方法将系统的依赖变更提现在pom文件的依赖变化,虽然解决了升级产生的潜规则,但改造周期、测试周期都比较长。因此需要升级者权衡,如果工期紧张,而后续又有持续的改进升级计划,第一种方式可以作为一种临时的高效过渡方案,当有较充足的资源时,可以采用第二种方式,彻底将问题解决掉。
下面我们详细介绍下spring4.0与ibatis的集成,便于大家在升级框架的过程中进行参考:
1、为方便说明,我们新建一个工程,来演示spring与mybatis的集成,新建工程:webinst,结构如下:
D:. └─src ├─main │ ├─java │ │ └─com │ │ └─myteay │ │ └─common │ │ └─dal │ │ └─dinner │ │ ├─dao │ │ ├─dataobject │ │ ├─exec │ │ └─ibatis │ ├─resources │ │ ├─META-INF │ │ │ └─spring │ │ └─sqlmap │ │ └─goods │ └─webapp │ └─WEB-INF └─test └─java └─com └─myteay └─myibatis
工程树说明:
- dao路径下用于存放定义的mybatis数据库操作接口
- dataobject路径下用于存放数据模型
- ibatis路径下用于存放数据库操作接口的实现
- exec用于执行相关测试验证代码
- META-INF/spring路径下存放当前工程的spring配置
- sqlmap路径下存放mybatis相关配置文件
- WEB-INF路径下存放工程相关基础配置如:applicationContext.xml、log4j.xml、web.xml等
本例以spring4.0.0.RELEASE版本为例来说明spring与mybatis的集成,pom文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.myteay</groupId> <artifactId>webinst</artifactId> <version>0.0.1</version> <packaging>war</packaging> <name>webinst</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_2.4_spec</artifactId> <version>1.1.1</version> <scope>provwided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.25</version> </dependency> <!-- zxing --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.0.0</version> </dependency> <!-- mysql dependency start --> <dependency> <groupId>org.mysql</groupId> <artifactId>mysql-connector-java-commercial</artifactId> <version>5.1.33</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- mysql dependency end --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- spring dependency start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- spring dependency end --> <!-- unit test start --> <dependency> <groupId>jmock</groupId> <artifactId>jmock</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>jmock</groupId> <artifactId>jmock-cglib</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- unit test end --> <dependency> <groupId>org.apache.mina</groupId> <artifactId>mina-core</artifactId> <version>2.0.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.4.726</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>commons-jelly</groupId> <artifactId>commons-jelly</artifactId> <exclusions> <exclusion> <groupId>servletapi</groupId> <artifactId>servletapi</artifactId> </exclusion> </exclusions> <version>1.0-RC1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-jexl</groupId> <artifactId>commons-jexl</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>0.9.5</version> </dependency> <dependency> <groupId>org.w3c.css</groupId> <artifactId>sac</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xerces</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.antlr</groupId> <artifactId>antlr</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.antlr</groupId> <artifactId>antlr-runtime</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.28</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!--javamail--> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <!--velocity--> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <finalName>webinst</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <configuration> <scanIntervalSeconds>3</scanIntervalSeconds> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>80</port> </connector> </connectors> <scanTargetPatterns> <scanTargetPattern> <directory>src/main/webapp/WEB-INF</directory> <excludes> <exclude>**/*.jsp</exclude> </excludes> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </scanTargetPattern> </scanTargetPatterns> </configuration> </plugin> </plugins> </build> </project>
说明:
- pom中增加了对jetty的依赖,这样可以方便我们后面的测试验证
- 为方便演示,增加了数据库操作相关jar依赖
- 本例中已mybatis3.4.2为目标集成版本
其中对mybatis的关键依赖如下:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency>
对spring的关键依赖如下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.0.RELEASE</version> </dependency>
其他依赖为本人框架升级需要用到的,各位可以忽略。
下面开始进行工程完善:
1、定义数据模型(建数据库表的过程这里省略):
/** * Myteay.com Inc. * Copyright (c) 2015-2016 All Rights Reserved. */ package com.myteay.common.dal.dinner.dataobject; import java.io.Serializable; import java.util.Date; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; /** * 商品信息数据模型 * * @author Administrator * @version $Id: GoodsInfoDO.java, v 0.1 2016年3月5日 上午12:28:53 Administrator Exp $ */ public class GoodsInfoDO implements Serializable { /** serialVersionUID */ private static final long serialVersionUID = 4624303532159349944L; /** ID流水号 */ private String id; /** 店铺流水号 */ private String shopId; /** 图片地址 */ private String picAddr; /** 商品标题 */ private String goodsTitle; /** 价格 */ private String price; /** 备注 */ private String summary; /** 上架时间 */ private Date gmtCreated; /** 最后修改时间 */ private Date gmtModified; /** * Getter method for property <tt>id</tt>. * * @return property value of id */ public String getId() { return id; } /** * Setter method for property <tt>id</tt>. * * @param id value to be assigned to property id */ public void setId(String id) { this.id = id; } /** * Getter method for property <tt>shopId</tt>. * * @return property value of shopId */ public String getShopId() { return shopId; } /** * Setter method for property <tt>shopId</tt>. * * @param shopId value to be assigned to property shopId */ public void setShopId(String shopId) { this.shopId = shopId; } /** * Getter method for property <tt>picAddr</tt>. * * @return property value of picAddr */ public String getPicAddr() { return picAddr; } /** * Setter method for property <tt>picAddr</tt>. * * @param picAddr value to be assigned to property picAddr */ public void setPicAddr(String picAddr) { this.picAddr = picAddr; } /** * Getter method for property <tt>goodsTitle</tt>. * * @return property value of goodsTitle */ public String getGoodsTitle() { return goodsTitle; } /** * Setter method for property <tt>goodsTitle</tt>. * * @param goodsTitle value to be assigned to property goodsTitle */ public void setGoodsTitle(String goodsTitle) { this.goodsTitle = goodsTitle; } /** * Getter method for property <tt>price</tt>. * * @return property value of price */ public String getPrice() { return price; } /** * Setter method for property <tt>price</tt>. * * @param price value to be assigned to property price */ public void setPrice(String price) { this.price = price; } /** * Getter method for property <tt>summary</tt>. * * @return property value of summary */ public String getSummary() { return summary; } /** * Setter method for property <tt>summary</tt>. * * @param summary value to be assigned to property summary */ public void setSummary(String summary) { this.summary = summary; } /** * Getter method for property <tt>gmtCreated</tt>. * * @return property value of gmtCreated */ public Date getGmtCreated() { return gmtCreated; } /** * Setter method for property <tt>gmtCreated</tt>. * * @param gmtCreated value to be assigned to property gmtCreated */ public void setGmtCreated(Date gmtCreated) { this.gmtCreated = gmtCreated; } /** * Getter method for property <tt>gmtModified</tt>. * * @return property value of gmtModified */ public Date getGmtModified() { return gmtModified; } /** * Setter method for property <tt>gmtModified</tt>. * * @param gmtModified value to be assigned to property gmtModified */ public void setGmtModified(Date gmtModified) { this.gmtModified = gmtModified; } /** * @see java.lang.Object#toString() */ public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
2、定义DAO接口
/** * Danlley Wei (mailto://danlley@126.com) * Copyright (c) 2005-2017 All Rights Reserved. */ package com.myteay.common.dal.dinner.dao; import java.util.List; import com.myteay.common.dal.dinner.dataobject.GoodsInfoDO; /** * * @author danlley(danlley@126.com) * @version $Id: GoodsInfoDAO.java, v 0.1 2017年4月3日 下午5:19:11 danlley(danlley@126.com) Exp $ */ public interface GoodsInfoDAO { public List<GoodsInfoDO> findAll(); }
相关推荐
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 <bean id="sqlMapClient" class="org.spring...
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
spring集成ibatis的源码 可以运行
spring与ibatis整合集成实例 spring与ibatis整合集成实例
部调整了),而iBatis目前最新版本是基于旧版的Castle.DynamicProxy 1.5,如果升级到3.0代码修改量 和测试工作量比较大,估计要看iBatis的发展规划了。 我把Castle.DynamicProxy1.5 和 IBatis.NET 1.92 1.6.2(目前...
ibatis与spring 框架整合
spring+ibatis集成开发文档,集成框架。
简单易入手,使用mysql数据库进行数据管理
spring ibatis整合所需jar包
struts2 ibatis spring 集成开发
spring ibatis 配置spring ibatis 配置spring ibatis 配置spring ibatis 配置
spring3.0加ibatis , 实例, 公司用的框架, 空项目 ,很稳定
iBatis和Spring整合 iBatis和Spring整合
ibatis spring springmvc 整合 integrate
ibatis spring struts1.2整合demo
ibatis spring dao spring ibatis集成
一部介绍Spring+Hibernate或Spring+Ibatis的教材
内容包里面是源代码,运行该例子的方式就是,使用里面的sql...使用cmd进入该解压包,运行 java -jar *.jar ibatis2spring.jar 就可以了。欲了解代码的详细,请访问作者博客,搜索《ibatis + Spring 多表查询》文章。
Spring与iBATIS的集成示例代码,希望对初学者有用