现在的位置: 首页 > 综合 > 正文

Ibatis 生成工具ibator的使用

2013年01月12日 ⁄ 综合 ⁄ 共 7362字 ⁄ 字号 评论关闭

      最近做一个项目要用到ibatis、struts2、spring框架,以前看到过ibatis生成工具。想在项目中使用,下了官方的文档,里面有几种方法生成方法:buider.xml,java Class等,研究了一下,花了很多时间,现在觉得还是用插件好一些,现成的东西不用,要用什么命令行,真傻!咱做的是技术应用,不是研发,能最快用技术解决需要才是重点。下面总结一下用Eclipse Plugin生成sqlmap.xml,javabean,dao,daoimp等的方法:

说明:

这里使用的是ibator作为ibatisEclipse插件,ibator的主页是:http://ibatis.apache.org/ibator.html

它提供两种方式:一种是Eclipse插件,一种是独立运行。

目前针对Eclipse的版本有两个版本,

1.2.1能运行在:

· Eclipse 3.4.1 or higher

· JDK 5.0 or Higher

· 它的Eclipse插件URL下载地址为:http://ibatis.apache.org/tools/ibator

1.1.0运行在:

· Eclipse 3.2 or higher

· JDK 1.4 or Higher

· 它的Eclipse插件URL下载地址为:http://ibatis.apache.org/tools/abator

 

注意:版本问题,笔者开始用的是myEclipse6.0.1,是eclipse 3.3.1平台,装了1.2.1怎么也不能用,调查才发现,不要因为版本而影响作用。

范例

新建一个工程,然后在该工程下新建文件,截图如下:(如果这里没有请到other..中找,截图如右:)

点击后就会新建一个文件:abatorConfig.xml。

以下是1.1.0的写法,供大家参照:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >

<abatorConfiguration >

<abatorContext >

<jdbcConnection driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver" connectionURL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=cmspro" userId="cmspro" password="cmspro" >

<classPathEntry location="D:/POM/project/msbase.jar" />

<classPathEntry location="D:/POM/project/mssqlserver.jar"/>

<classPathEntry location="D:/POM/project/msutil.jar" />

</jdbcConnection>

<javaModelGenerator targetPackage="com.ucap.account.model" targetProject="account-service-impl" />

<sqlMapGenerator targetPackage="resources.sqlmaps" targetProject="account-service-impl" />

<daoGenerator targetPackage="com.ucap.account.dao" targetProject="account-service-impl" type="GENERIC-CI" />

<table tableName="cmspro_channel_privilege" domainObjectName="ChannelPrivilege"/>

</abatorContext>

</abatorConfiguration>

这里<table>标签中的domainObjectName,是用来重新命名类名。

 

以下是1.2.1的写法,供大家参照:

<?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>
 <!--
      url     指定属性文件绝对路径。注意与指定数据库jdbc驱动jar包路径的区别哈。
                    可以使用${property}的格式引用属性文件中的属性值。
    <properties url="D:/WorkSpace/SSIStudy/ibator-config.properties"/>
    -->
    <!--
      classPathEntry 指定数据库jdbc驱动jar包的绝对路径。 可为为空
      The full path name of a JAR/ZIP file to add to the classpath, or a directory to add to the classpath
    --> 
    <classPathEntry location="D:/WorkSpace/SSIStudy/WebRoot/WEB-INF/lib" />
   
    <!--
      id                 这个id可以在使用命令行运行Abator时指定,以单独处理某一个ibatorContext
      targetRuntime        Ibatis2Java5 生成适合JDK5.0的类,另一个选项是 Ibatis2Java2,生成适合Java2的类。
    -->
  <ibatorContext id="SSIStudy" targetRuntime="Ibatis2Java2">
   <!--
           ibatorPlugin 继承自IbatorPluginAdapter,包名必须是 org.apache.ibatis.ibator.plugins,具体实现可以参考官方文档
             必须有替换和被替换字符属性。 可以没有
     -->
     <!--
   <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">
             <property name="searchString" value="Example$" />
             <property name="replaceString" value="Criteria" />
     </ibatorPlugin>
    
           driverClass        数据库驱动类  这里连接sqlserver2000
           connectionURL      数据库连接地址
           userId             用户
           password           密码
            还可以使用以下格式添加数据库的其他连接属性
           <property name="" value=""/>
     -->
      
     <jdbcConnection driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver" connectionURL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo" userId="sa" password="sa" >
     </jdbcConnection>
    
     <javaTypeResolver >
           <!--
                   默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
                   true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
           --> 
           <property name="forceBigDecimals" value="false" />
     </javaTypeResolver>
     <!-- //When running outside of an IDE environment like Eclipse, Ibator interprets the targetProject and targetPackage attributes in all XML configurations as follows
           targetProject        生成的Java Bean放置在哪个项目的哪个目录下
           targetPackage        生成的Java Bean的包名
          
             一个有用的属性
            从数据库返回的值被清理前后的空格
           <property name="trimStrings" value="true" />
            是否在包名后加上scheme名称       
           <property name="enableSubPackages" value="false" />      
     --> 
     <javaModelGenerator targetPackage="cn.per.jiabeis.model" targetProject="D:/WorkSpace/SSIStudy/src">
      <property name="enableSubPackages" value="true" />
     <property name="trimStrings" value="true" />
     </javaModelGenerator>
     <!--
           targetProject        生成的 SqlMap.xml 文件放置在哪个项目的哪个目录下
           targetPackage        生成的 SqlMap.xml 文件的包名
           <property name="enableSubPackages" value="false" />
                   是否在包名后加上scheme名称
      -->
     <sqlMapGenerator targetPackage="cn.per.jiabeis.model.config" targetProject="D:/WorkSpace/SSIStudy/src">
      <property name="enableSubPackages" value="true" />
     </sqlMapGenerator>
      <!--
           targetProject        生成的 dao类文件放置在哪个项目的哪个目录下
           targetPackage        生成的 dao类文件的包名
           <property name="enableSubPackages" value="false" />
                   是否在包名后加上scheme名称
           type        生成dao文件的类型,可选择IBATIS、SPRING、GENERIC-CI、GENERIC-SI。默认使用GENERIC-CI
                       dao类在构造器中获取 SqlMapClient。
                   
     -->
     <daoGenerator targetPackage="cn.per.jiabeis.dao" targetProject="D:/WorkSpace/SSIStudy/src" type="IBATIS">
     <property name="enableSubPackages" value="true" />
  </daoGenerator>
  
  <!--
           tableName 数据库表明,据说可以包含SQL通配符%和_。
           domainObjectName 数据库表对应的数据对象名称,默认使用表名作为对象名称。
     -->
     <table tableName="student" domainObjectName="Student">
      <!--
                   对象的属性名是否使用字段名称,默认为true
      -->
      <property name="stuname" value="stuname"/>
       <!--
               column    自增长或使用sequence生成的字段名
               sqlStatement 生成字段的sql片段或其简称(参考官方文档)
               identity    true表示后生成,false表示预生成
               
                   例如:
                   
                   postgresql:<generatedKey 
                       column="lid" 
                       sqlStatement="select nextval('tb000000producttype_lid_seq')" 
                       identity="false" />
                       
                   sqlserver:<generatedKey 
                       column="lid" 
                       sqlStatement="SqlServer" 
                       identity="true" />
                       
                   oracle:<generatedKey 
                       column="lid" 
                       sqlStatement="select tb000000producttype_lid_seq.nextval from dual" 
                       identity="false" />
                   MySql:<generatedKey 
                       column="lid" 
                       sqlStatement="MySql" 
                       identity="false" />
               
         -->
      <generatedKey column="stuid" sqlStatement="SqlServer" identity="true"/>
      
         <!--
                   数据库字段名称到对象属性名称的影射关系。就是一个替换处理。
          -->
         <columnRenamingRule searchString="^CUST_" replaceString="" /> 
         <!--
                   column  字段名
                   property 字段对应的属性名。(默认使用字段名的)
                   javaType 对应的Java类型
                   jdbcType 对应的jdbc类型
                   
                       这里的设置覆写javaTypeResolver中的指定
          -->
       <columnOverride column="DATE_FIELD" property="startDate" />
           
          <!--
               column    需要忽略的数据库字段
          -->  
          <ignoreColumn column="FRED" />  
         
     </table>
   
     <table tableName="teacher"/>
     <table tableName="course"/>
     <table tableName="stu_course"/>
    </ibatorContext>
</ibatorConfiguration>

以上书写好xml后,请鼠标右键点击该xml文件生成Artifacts。

抱歉!评论已关闭.