`
hyw520110
  • 浏览: 211365 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ibator改造之返回数据库注释和数据库分页

    博客分类:
  • java
阅读更多

转载:http://www.iteye.com/topic/821983
欢迎 Javaeye 祸鞋归来。

参考文献:

1、Ibator支持分页的plugin 

2、ibator改进,生成中文注释

插件基于ibator1.2.2(http://svn.apache.org/repos/asf/ibatis/java/ibator) 

个人感觉用ibator Eclipse插件不如直接用这种方式生产代码方便,出错几率小的多,还能log跟踪。

更新:

 

ibator_2010-12-7.jar版本增加了一个插件,ChangeReturnPlugin,功能:

把所有delete、update、insert返回为int类型的方法改为返回布尔值类型

   大部分都用不到返回的行数,多数情况只是关心是否执行成功

在配置文件中加入如下配置即可:

<!-- 修改dao里面返回值,把增删改的返回值由整型改为布尔型 -->
   <ibatorPlugin type="org.apache.ibatis.ibator.plugins.ChangeReturnPlugin" />

和插件SerializablePlugin一样使用,如果没有配置,就不会修改dao类里面的部分方法的返回值,方法还是默认返回int类型。

ibator_2010-12-7.jar对应的源码过些天在上传。

 

强烈建议用一下的java方法生成所需的xml、dao、pojo。不建议用ibator的eclipse插件方式。

自己可以新建一个java工程,里面存放这个.java文件和ibator.jar文件。如果要生成代码到其他工程里面去可以在xml里面配置:

targetProject="../你要生成的目的工程名/src">  记得xml里面的3个targetProject都要修改哦。

 

Java代码 复制代码
  1. package ibator;   
  2.   
  3. import java.io.File;   
  4. import java.util.ArrayList;   
  5. import java.util.List;   
  6.   
  7. import org.apache.ibatis.ibator.api.Ibator;   
  8. import org.apache.ibatis.ibator.config.IbatorConfiguration;   
  9. import org.apache.ibatis.ibator.config.xml.IbatorConfigurationParser;   
  10. import org.apache.ibatis.ibator.internal.DefaultShellCallback;   
  11.   
  12. public class IbatorRunTest {   
  13.   
  14.     public static void main(String... strings) {   
  15.         try {   
  16.             List<String> warnings = new ArrayList<String>();   
  17.             boolean overwrite = true;   
  18.             File configFile = new File(ClassLoader.getSystemResource("ConfigIbatisExample.xml").getFile());   
  19.             IbatorConfigurationParser cp = new IbatorConfigurationParser(   
  20.                     warnings);   
  21.             IbatorConfiguration config = cp   
  22.                     .parseIbatorConfiguration(configFile);   
  23.             DefaultShellCallback callback = new DefaultShellCallback(overwrite);   
  24.             Ibator ibator = new Ibator(config, callback, warnings);   
  25.             ibator.generate(null);   
  26.             for (String warning : warnings) {   
  27.                 System.out.println("warning:" + warning);   
  28.             }   
  29.         } catch (Exception ex) {   
  30.             ex.printStackTrace();   
  31.         }   
  32.     }   
  33.   
  34. }  
package ibator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.ibator.api.Ibator;
import org.apache.ibatis.ibator.config.IbatorConfiguration;
import org.apache.ibatis.ibator.config.xml.IbatorConfigurationParser;
import org.apache.ibatis.ibator.internal.DefaultShellCallback;

public class IbatorRunTest {

	public static void main(String... strings) {
		try {
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			File configFile = new File(ClassLoader.getSystemResource("ConfigIbatisExample.xml").getFile());
			IbatorConfigurationParser cp = new IbatorConfigurationParser(
					warnings);
			IbatorConfiguration config = cp
					.parseIbatorConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			Ibator ibator = new Ibator(config, callback, warnings);
			ibator.generate(null);
			for (String warning : warnings) {
				System.out.println("warning:" + warning);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}

 

修改日志:

1、增加数据库注释,oracle默认不返回数据库注释,需要设置一个参数

2、当oracle得字段为number没有指定长度时,ibator会设置字段为Big Decimal 

3、去掉不必要的其他注释

4、改进分页的生成方式

5、其他详情见附件源码

 

 

配置文件代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE ibatorConfiguration   
  3.   PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN"  
  4.   "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd">   
  5.   
  6. <ibatorConfiguration>   
  7.     <classPathEntry location="E:/jars/ojdbc/ojdbc14_10.2.0.4.jar" />   
  8.     <ibatorContext id="FlatJava5" targetRuntime="Ibatis2Java5">   
  9.         <property name="suppressTypeWarnings" value="true" />   
  10.         <!-- Serializable化 -->   
  11.         <ibatorPlugin type="org.apache.ibatis.ibator.plugins.SerializablePlugin" />   
  12.         <!-- 这个插件添加方法为例(实际上的内部类)来支持不区分大小写像搜索。这个演示了增加功能,通过一个实例类插件,而不是延长上课。 -->   
  13.         <ibatorPlugin type="org.apache.ibatis.ibator.plugins.CaseInsensitiveLikePlugin" />   
  14.         <!-- 分页 -->   
  15.         <ibatorPlugin type="org.apache.ibatis.ibator.plugins.PaginationPlugin">   
  16.             <property name="enablePagination" value="true" />   
  17.             <!-- mysql or oracle -->   
  18.             <property name="databaseType" value="oracle" />   
  19.         </ibatorPlugin>   
  20.         <!-- 重命名example类 -->   
  21.         <ibatorPlugin   
  22.             type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">   
  23.             <property name="searchString" value="Example$" />   
  24.             <property name="replaceString" value="Criteria" />   
  25.         </ibatorPlugin>   
  26.         <!-- 产生sqlmap.xml   
  27.         <ibatorPlugin type="org.apache.ibatis.ibator.plugins.SqlMapConfigPlugin">   
  28.             <property name="targetPackage" value="ibatortest.generated.flat" />   
  29.             <property name="targetProject" value="src" />   
  30.         </ibatorPlugin>   
  31.         -->   
  32.   
  33.         <!-- driverClass="com.mysql.jdbc.Driver" -->   
  34.         <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"  
  35.             connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ora10g" userId="test"  
  36.             password="test" >   
  37.             <!-- 是否返回数据库注释,MySQL默认是true,oracle默认是false -->   
  38.             <property name="remarksReporting" value="true"/>   
  39.             </jdbcConnection>   
  40.         <javaModelGenerator targetPackage="test.pojo"  
  41.             targetProject="src">   
  42.             <!-- 如果为TRUE,下面的设置了schema,那么包名就是会增加个schema名,   
  43.             如果schema=“aaa”  , test.pojo.aaa.XXXX;   
  44.             <property name="enableSubPackages" value="true" />   
  45.              -->   
  46.              <!-- 继承哪个父类,这个为了有时候日志需要打印整个对象,而采用的apache打印对象的每个属性 -->   
  47.              <property name="rootClass" value="ibator.BaseBean" />   
  48.         </javaModelGenerator>   
  49.   
  50.         <sqlMapGenerator targetPackage="test.sqlmap"  
  51.             targetProject="src">   
  52.             <!--   
  53.             <property name="enableSubPackages" value="true" />   
  54.              -->   
  55.         </sqlMapGenerator>   
  56.   
  57.         <daoGenerator type="SPRING" targetPackage="test.dao" implementationPackage="test.dao.impl"  
  58.             targetProject="src">   
  59.             <!--   
  60.             <property name="enableSubPackages" value="true" />   
  61.              -->   
  62.         </daoGenerator>   
  63.   
  64.         <!--   
  65.         如果数据库里面有多个相同表名在不同的schema下,那么你得加上:   
  66.         schema="XXXX"和<property name="ignoreQualifiersAtRuntime" value="true" />   
  67.         不然ibator会以找到的最后一个为最终对象,这个问题苦恼了我很久,后来才发现   
  68.          -->   
  69.         <table tableName="OTA_APPLETE_INFO" schema="XXX">   
  70.          <!-- 忽略schema,避免在xml中出现schema.表名   
  71.           -->   
  72.          <property name="ignoreQualifiersAtRuntime" value="true" />   
  73.           <!-- 精确到时分秒时,需要设置下:  jdbcType="TIMESTAMP"-->   
  74.           <columnOverride column="UPDATED_DATE" jdbcType="TIMESTAMP"/>   
  75.         </table>   
  76.     </ibatorContext>   
  77. </ibatorConfiguration>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ibatorConfiguration
  PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN"
  "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd">

<ibatorConfiguration>
	<classPathEntry location="E:/jars/ojdbc/ojdbc14_10.2.0.4.jar" />
	<ibatorContext id="FlatJava5" targetRuntime="Ibatis2Java5">
		<property name="suppressTypeWarnings" value="true" />
		<!-- Serializable化 -->
		<ibatorPlugin type="org.apache.ibatis.ibator.plugins.SerializablePlugin" />
		<!-- 这个插件添加方法为例(实际上的内部类)来支持不区分大小写像搜索。这个演示了增加功能,通过一个实例类插件,而不是延长上课。 -->
		<ibatorPlugin type="org.apache.ibatis.ibator.plugins.CaseInsensitiveLikePlugin" />
		<!-- 分页 -->
		<ibatorPlugin type="org.apache.ibatis.ibator.plugins.PaginationPlugin">
			<property name="enablePagination" value="true" />
			<!-- mysql or oracle -->
			<property name="databaseType" value="oracle" />
		</ibatorPlugin>
		<!-- 重命名example类 -->
		<ibatorPlugin
			type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">
			<property name="searchString" value="Example$" />
			<property name="replaceString" value="Criteria" />
		</ibatorPlugin>
		<!-- 产生sqlmap.xml
		<ibatorPlugin type="org.apache.ibatis.ibator.plugins.SqlMapConfigPlugin">
			<property name="targetPackage" value="ibatortest.generated.flat" />
			<property name="targetProject" value="src" />
		</ibatorPlugin>
		-->

		<!-- driverClass="com.mysql.jdbc.Driver" -->
		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
			connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ora10g" userId="test"
			password="test" >
			<!-- 是否返回数据库注释,MySQL默认是true,oracle默认是false -->
			<property name="remarksReporting" value="true"/>
			</jdbcConnection>
		<javaModelGenerator targetPackage="test.pojo"
			targetProject="src">
			<!-- 如果为TRUE,下面的设置了schema,那么包名就是会增加个schema名,
			如果schema=“aaa”  , test.pojo.aaa.XXXX;
			<property name="enableSubPackages" value="true" />
			 -->
			 <!-- 继承哪个父类,这个为了有时候日志需要打印整个对象,而采用的apache打印对象的每个属性 -->
			 <property name="rootClass" value="ibator.BaseBean" />
		</javaModelGenerator>

		<sqlMapGenerator targetPackage="test.sqlmap"
			targetProject="src">
			<!--
			<property name="enableSubPackages" value="true" />
			 -->
		</sqlMapGenerator>

		<daoGenerator type="SPRING" targetPackage="test.dao" implementationPackage="test.dao.impl"
			targetProject="src">
			<!--
			<property name="enableSubPackages" value="true" />
			 -->
		</daoGenerator>

		<!--
		如果数据库里面有多个相同表名在不同的schema下,那么你得加上:
		schema="XXXX"和<property name="ignoreQualifiersAtRuntime" value="true" />
		不然ibator会以找到的最后一个为最终对象,这个问题苦恼了我很久,后来才发现
		 -->
		<table tableName="OTA_APPLETE_INFO" schema="XXX">
		 <!-- 忽略schema,避免在xml中出现schema.表名
		  -->
		 <property name="ignoreQualifiersAtRuntime" value="true" />
		  <!-- 精确到时分秒时,需要设置下:  jdbcType="TIMESTAMP"-->
		  <columnOverride column="UPDATED_DATE" jdbcType="TIMESTAMP"/>
		</table>
	</ibatorContext>
</ibatorConfiguration>

 

 

Java代码 复制代码
  1. package test.pojo;   
  2.   
  3. import ibator.BaseBean;   
  4. import java.io.Serializable;   
  5. import java.util.Date;   
  6. /**  
  7.  * 2009-07-27  ss       
  8.  * <p>  
  9.  * 每个应用的具体信息  
  10.  */  
  11. public class BaseUsers extends BaseBean implements Serializable {   
  12.     /**  
  13.      * 用户ID  
  14.      */  
  15.     private String userId;   
  16.   
  17.     /**  
  18.      * 组织结构编码  
  19.      */  
  20.     private String groupId;   
  21.   
  22.     /**  
  23.      * 姓名  
  24.      */  
  25.     private String userName;   
  26.   
  27.     /**  
  28.      * 证件号码  
  29.      */  
  30.     private String cardNo;  
package test.pojo;

import ibator.BaseBean;
import java.io.Serializable;
import java.util.Date;
/**
 * 2009-07-27  ss     
 * <p>
 * 每个应用的具体信息
 */
public class BaseUsers extends BaseBean implements Serializable {
    /**
     * 用户ID
     */
    private String userId;

    /**
     * 组织结构编码
     */
    private String groupId;

    /**
     * 姓名
     */
    private String userName;

    /**
     * 证件号码
     */
    private String cardNo;
 

 

 

附件一个为编译后的jar文件,一个是源码+doc。

 

 

分享到:
评论

相关推荐

    ibator优化版,使用数据库的注释

    使用数据库的注释,不用自带的注释 http://blog.csdn.net/tiantangpw/article/details/43489817 运行命令 java -jar ibator.jar -configfile ibatorConfig.xml -overwrite &gt;&gt;ibator.log

    ibator1.2.2无注释

    ibator1.2.2多了点功能,具体可以百度,重新编译了下,生成注释去掉了

    适用mysql分页的ibator

    NULL 博文链接:https://guoba6688-sina-com.iteye.com/blog/1098212

    ibator的eclipse插件

    直接丢到eclipse的plugins目录下就可以了,在一些公司下载不了的地方,方便使用

    IBATOR动态生成sql和DAO层

    此项目通过对ibator的改造,通过执行cmd命令自动生成sql与Dao,大大提高开发效率

    iBATOR-V1.1.0

    iBATOR is a code generator for iBATIS.

    ibator优化的jar包

    ibator插件优化的jar包,安装完ibator后,将eclipse\plugins\org.apache.ibatis.ibator.core_1.2.1下的jar包替换即可。

    eclipse集成的ibator插件

    eclipse的集成插件,ibator.jar,可以直接mybatis导表,图形化展示,简单易用,直接放到eclipse的plugin文件夹下即可

    ibator教学视频

    ibator教学视频,手把手教你使用ibator

    为 Ibatis 2.3.4 构建增强的 Apache Ibator 实体类生成工具

    Ibator is a code generator for iBATIS. Ibator will introspect a database table (or many tables) and will generate iBATIS artifacts that can be used to access the table(s). This abates some of the ...

    ibator使用指导

    在myelipse7.5中安装ibator插件的方法与安装一般插件的方法是一致的,有以下2种方式(个人推荐使用手动安装方式,避免网络等原因造成安装不成功): 1. 手动安装方式 将邮件中附件的Ibator插件压缩文件IbatorForEclipse...

    删除注释工具类

    删除注释工具类。针对Ibator生成的实体产生的注释,填写参数,删除注释。

    Ibator参考程序

    做SSI项目时,Ibator映射出来的内容实在太多了,有很多内容都用不上,参考Ibator的结构,实现对基本字段和方法的映射!

    ibator 1.2.1

    eclipse的ibatis代码生成器,最新版ibator 1.2.1

    ibator使用心得

    ibator相对于hibernate框架能完全自主编写sql代码,同时又有hibernate便于管理的优点,是非常理想的持久层技术

    处理后的ibator1.2.1

    去除了注释、去除Example方法及去除生成的id前面的“ibatorgenerated_”

    ibatis自动生成工具ibator,改进版

    开源ibator什么xml和pojo的时候,经常会带一些讨厌的注释还有一些没用的ibatorgenerator等的,我改了它的源代码,弄了个干净的ibator

    ibator1.2.1配置文件

    自动生成dto\dao\xml 自带批处理自动生成程序

    ibator-eclipse插件1.2.1 包含优化后jar包

    ibatis代码自动生成工具Eclipse插件,已进行优化处理,不生成注释和精简dao方法,亲测可用

    ibator API帮助文档.chm

    Ibatis 生成器 Ibator 的 API 文档

Global site tag (gtag.js) - Google Analytics