首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ibatis3 (mybatis) 实例代码上载兼ibatis3优劣分析

2012-11-04 
ibatis3 (mybatis) 实例代码下载兼ibatis3优劣分析(本文章于2010-06-30更新以适应mybatis的最新版本)?作为

ibatis3 (mybatis) 实例代码下载兼ibatis3优劣分析

(本文章于2010-06-30更新以适应mybatis的最新版本)

?

作为rapid-framework路线图的一部分,集成ibatis3也是以后要更新的内容之一.

现编写了ibatis3的代码例子.?

?

一.首先我们来看现在的xml mapper关于增删改查的编写

?

?

?

?

与ibatis2 sqlmap的主要异同:

1. insert节点现在可以直接指定mysql auto_increment(或是sqlserver identity)的主键生成策略

??useGeneratedKeys="true" keyProperty="userId"?

?

2.动态构造sql部分,test部分采用的是struts2 ognl表达式,还有choose,foreach语句等,跟struts2 tag是否很像呢?

??(注:此处现可以使用ONGL访问静态方法来解决:@Ognl@isNotEmpty(userId))

??Ognl静态方法调用

?

二.构造SqlSessionFactory,以前的SqlMapClient

?

?

以上就是完整的示例, 具体demo代码下载: ?http://rapid-framework.googlecode.com/files/ibatis3_demo.zip

?

?

ibatis3 annotation评价:

难听点,根本是个脑残方案,如果用annotation写,我还不如使用类似jdbc的java代码. 不知道自己优势是在xml文件,瞎跟风.

?

而spring现在还没有对ibatis3集成,不过以后rapid会先与spring发布ibatis3的插件, 只提供生成器模板,现不自己开发集成,等待spring. 当然以后对提供类似ibatis2的基于方言Dialect的分页还是会提供的.

?

?

最后仍然做下广告: rapid-framework, 现最好的项目脚手架

http://code.google.com/p/rapid-framework/

?

?

?

@Intercepts({@Signature(type= StatementHandler.class, method = "prepare", args = {Connection.class})})public class DiclectStatementHandlerInterceptor implements Interceptor {private Properties properties;private static final String DIALECT = "dialect";public Object intercept(Invocation invocation) throws Throwable {RoutingStatementHandler statement = (RoutingStatementHandler)invocation.getTarget();PreparedStatementHandler handler = (PreparedStatementHandler) ReflectUtil.getField(statement, "delegate", StatementHandler.class);Integer rowOffset = (Integer)ReflectUtil.getField(handler, "rowOffset", int.class);Integer rowLimit = (Integer)ReflectUtil.getField(handler, "rowLimit", int.class);if(rowLimit !=0) {BoundSql boundSql = statement.getBoundSql();String sql = boundSql.getSql();String dialectStr = properties.getProperty(DIALECT);Dialect dialect = (Dialect) Class.forName(dialectStr).newInstance();sql = dialect.getLimitString(sql, rowOffset, rowLimit);ReflectUtil.setFieldValue(boundSql, "sql", String.class, sql);}return invocation.proceed();}public Object plugin(Object target) {return Plugin.wrap(target, this);}public void setProperties(Properties properties) {this.properties = properties;}}
8 楼 badqiu 2009-09-28   你现在这个切入点是不行的,必须要能够修改limit及offset的值才行, 我已经写好了,我写一下文章介绍一下. 9 楼 melin 2009-09-28   badqiu 写道你现在这个切入点是不行的,必须要能够修改limit及offset的值才行, 我已经写好了,我写一下文章介绍一下.

反射能获取,肯定能修改
期待你的思路。。。 10 楼 littleJava 2009-11-09   iBATIS3.0和iBATIS2.0在XML的规则上有写区别,近期估计不会有iBATIS3的实际项目吧 11 楼 poplong_2001 2009-12-13   楼主的原文:ibatis3 annotation评价:

难听点,根本是个脑残方案,如果用annotation写,我还不如使用类似jdbc的java代码. 不知道自己优势是在xml文件,瞎跟风.

楼主这段话说明楼主压根没有认真看ibatis3的官方文档。
ibatis3的官方文档中明确说明了:“Java Annotations are unfortunately limited in their expressiveness and flexibility. Despite a lot of time spent in investigation, design and trials, the most powerful iBATIS mappings simply cannot be built with Annotations – without getting ridiculous that is.”That said, the Java!Annotation based configuration is not without its benefits.
ibatis3之所以推出这种配置方式,主要是为了顺应新的jdk的变化,为部分用户提供一种选择而已,文档中其实已经明确指出了java annotations的缺陷,而且并不鼓励大家在大型的项目中使用。并强调xml的方式仍然是发挥ibatis3优势的最好选择。

热点排行