iBatorConfig.xml详解

1、Abator生成Java类文件时,根据注释区分属性和方法是系统生成或用户自定义,以此决定保留或者覆写.
 
2、Abator生成SQLMap的xml文件时,根据元素id是否包含前缀 ibatorgenerated_ 区分元素是系统
      生成或用户自定义,以此决定保留或者覆写.。
 
3、注意ibatorConfig.xm 文件中节点的顺序
 
4、生成的数据对象
 
    Primary Key Class 主键的所有组成字段在一个类中
     
    Record Class 非主键字段非BLOB字段组成的类,继承于Primary Key Class
     
    Record With BLOBs Class 所有BLOB字段组成的类,继承于Record Class (如不存在),就会继承Primary Key Class
                            不支持只包含BLOB字段的表。
     
    Example Class    用于生成动态where条件的类
 
5、example Class 使用(可以使用逻辑运算的结合律简化where条件)
 
代码:
  TestTableExample example = new TestTableExample();
  example.createCriteria().andField1EqualTo(5);
 
产生条件:
 
  where field1 = 5
 
代码:
 
  TestTableExample example = new TestTableExample();
 
  example.createCriteria()
    .andField1EqualTo(5)
    .andField2IsNull();
 
  example.or(example.createCriteria()
    .andField3NotEqualTo(9)
    .andField4IsNotNull());
 
  List<Integer> field5Values = new ArrayList<Integer>();
  field5Values.add(8);
  field5Values.add(11);
  field5Values.add(14);
  field5Values.add(22);
 
  example.or(example.createCriteria()
    .andField5In(field5Values));
 
  example.or(example.createCriteria()
    .andField6Between(3, 7));
 
 
产生条件:
 
  where (field1 = 5 and field2 is null)
     or (field3 <> 9 and field4 is not null)
     or (field5 in (8, 11, 14, 22))
     or (field6 between 3 and 7)
     
 
6、ibatorConfig.xm 文件分析
 
<?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>
 
    <properties url="file:/home/guo/workspace_google/iBATIS/config/config.properties"/>
     
    <!--
      url     指定属性文件绝对路径。注意与指定数据库jdbc驱动jar包路径的区别哈。
                    可以使用${property}的格式引用属性文件中的属性值。
    -->
     
    <classPathEntry location="/home/guo/java/workspace/newbee/lib/ibatis/postgresql-8.3-604.jdbc3.jar" />
     
    <!--
      classPathEntry 指定数据库jdbc驱动jar包的绝对路径。
    -->
     
    <ibatorContext id="context1" targetRuntime="Ibatis2Java5">
     
    <!--
      id                 这个id可以在使用命令行运行Abator时指定,以单独处理某一个ibatorContext
      targetRuntime        Ibatis2Java5 生成适合JDK5.0的类,另一个选项是 Ibatis2Java2,生成适合Java2的类。
    -->
     
        <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="Criteria" />
        </ibatorPlugin>
         
        <!--
          ibatorPlugin 继承自IbatorPluginAdapter,包名必须是 org.apache.ibatis.ibator.plugins,具体实现可以参考官方文档
            必须有替换和被替换字符属性。
        -->
     
        <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://192.168.1.2:5432/newbee" userId="sa" password="esoon" />
         
        <!--
          driverClass        数据库驱动类
          connectionURL        数据库连接地址
          userId            用户
          password            密码
          
                还可以使用以下格式添加数据库的其他连接属性
          <property name="" value=""/>
        -->
        
        <javaTypeResolver >
        
          <property name="forceBigDecimals" value="false" />
          
          <!--
                  默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
                  true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
        -->
          
        </javaTypeResolver>
             
        <javaModelGenerator targetPackage="com.newbee.bean" targetProject="newbee/src" />
         
        <!--
          targetProject        生成的Java Bean放置在哪个项目的哪个目录下
          targetPackage        生成的Java Bean的包名
                 一个有用的属性
          <property name="trimStrings" value="true" />
                  从数据库返回的值被清理前后的空格
          <property name="enableSubPackages" value="false" />
                  是否在包名后加上scheme名称
        -->
             
        <sqlMapGenerator targetPackage="com.newbee.xml" targetProject="newbee/src" />
         
        <!--
          targetProject        生成的 SqlMap.xml 文件放置在哪个项目的哪个目录下
          targetPackage        生成的 SqlMap.xml 文件的包名
          <property name="enableSubPackages" value="false" />
                  是否在包名后加上scheme名称
        -->
             
        <daoGenerator targetPackage="com.newbee.dao" targetProject="newbee/src" type="GENERIC-CI" />
         
        <!--
          targetProject        生成的 dao类文件放置在哪个项目的哪个目录下
          targetPackage        生成的 dao类文件的包名
          <property name="enableSubPackages" value="false" />
                  是否在包名后加上scheme名称
          type        生成dao文件的类型,可选择IBATIS、SPRING、GENERIC-CI、GENERIC-SI。默认使用GENERIC-CI
                      dao类在构造器中获取 SqlMapClient。
                   
        -->
             
        <table tableName="ALLTYPES" domainObjectName="Customer" >
         
        <!--
          tableName 数据库表明,据说可以包含SQL通配符%和_。
          domainObjectName 数据库表对应的数据对象名称,默认使用表名作为对象名称。
        -->
         
          <property name="useActualColumnNames" value="true"/>
          
          <!--
                  对象的属性名是否使用字段名称
          -->
          
          <generatedKey column="ID" sqlStatement="DB2" identity="true" />
          
          <!--
              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" />
               
          -->
          
          <columnOverride column="DATE_FIELD" property="startDate" />
          
              <!--
                  column  字段名
                  property 字段对应的属性名。(默认使用字段名的)
                  javaType 对应的Java类型
                  jdbcType 对应的jdbc类型
                   
                      这里的设置覆写javaTypeResolver中的指定
              -->
          
          <ignoreColumn column="FRED" />
          
          <!--
              column    需要忽略的数据库字段
          -->
          
          <columnRenamingRule searchString="^CUST_" replaceString="" />
          
          <!--
                  数据库字段名称到对象属性名称的影射关系。就是一个替换处理。
          -->
          
        </table>
         
    </ibatorContext>
     
</ibatorConfiguration>

 

 <?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="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.1.8:1521:orcl" userId="user" password="password" >
      <classPathEntry location="D:/jar/jdbc/classes12.jar" />
    </jdbcConnection>
    <javaModelGenerator targetPackage="model" targetProject="ibator" />
    <sqlMapGenerator targetPackage="maps" targetProject="ibator" />
    <daoGenerator targetPackage="dao" targetProject="ibator" type="GENERIC-CI" />
    <table schema="user" tableName="%" >
    </table>
  </abatorContext>
</abatorConfiguration>

abatorConfig.xml for mysql :

<?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.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="passwrord" >
      <classPathEntry location="D:/jar/jdbc/mysql-connector-java-5.1.7-bin.jar" />
    </jdbcConnection>
    <javaModelGenerator targetPackage="model" targetProject="ibator" />
    <sqlMapGenerator targetPackage="maps" targetProject="ibator" />
    <daoGenerator targetPackage="dao" targetProject="ibator" type="GENERIC-CI" />
    <table tableName="tablename" />
    <table schema="test" tableName="tablename" >
        <generatedKey column="tablename_id" sqlStatement="MySql" identity="true"  /> 
    </table>
  </abatorContext>
</abatorConfiguration>


ibatorConfig for Mysql :

<?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="D:/jar/jdbc/mysql-connector-java-5.1.7-bin.jar" />
    <ibatorContext id="context1" targetRuntime="Ibatis2Java5">
        <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$" />
            <property name="replaceString" value="Criteria" />
        </ibatorPlugin>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="password" />
        <javaTypeResolver >
          <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <javaModelGenerator targetPackage="com.bean" targetProject="proj/src" />
        <sqlMapGenerator targetPackage="com.xml" targetProject="proj/src" />
        <daoGenerator targetPackage="com.dao" targetProject="proj/src" type="GENERIC-CI" />
        <table tableName="ba_message">
          <property name="useActualColumnNames" value="true"/>
          <generatedKey column="table_id" sqlStatement="MySql" identity="true" />
        </table>
    </ibatorContext>
</ibatorConfiguration>

SqlMapConfig.xml :

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

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

    <properties resource="testibatis/SqlMapConfig.properties"/>

    <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
    <transactionManager type="JDBC">
        <dataSource type="SIMPLE">
            <property value="${driver}" name="JDBC.Driver"/>
            <property value="${url}" name="JDBC.ConnectionURL"/>
            <property value="${username}" name="JDBC.Username"/>
            <property value="${password}" name="JDBC.Password"/>
        </dataSource>
    </transactionManager>

    <sqlMap resource="xml/ba_message_SqlMap.xml"/>

</sqlMapConfig>

SqlMapConfig.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/oa3
username=root
password=root

Source:

        String resource = "testibatis/SqlMapConfig.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        BaMessageDAOImpl dao = new BaMessageDAOImpl(sqlMap);
        //dao.setSqlMapClient(sqlMap);
        BaMessage message=dao.selectByPrimaryKey("8ac082092133ee45012133f0eff10001");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值