首先,强烈推荐一篇文章,介绍的特详细
http://www.iteye.com/topic/821983
1. 插件安装
http://blog.csdn.net/rchm8519/article/details/40792821
2. 创建项目,导入ibator-2.0.jar
略。。。。
3. 创建ibator描述文件
<?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 > <!-- 指定数据库驱动jar包路径;注意:该属性不在默认配置文件中,需要手动添加,建议使用绝对路径--> <classPathEntry location="D:\javaTools\ojdbc14.jar" /> <!-- id:使用命令行运行Abator时指定,以单独处理某一个ibatorContext targetRuntime:Ibatis2Java5 生成适合JDK5.0的类,另一个选项是 Ibatis2Java2,生成适合Java2的类。 --> <ibatorContext id="FlatJava5" targetRuntime="Ibatis2Java5"> <property name="suppressTypeWarnings" value="true" /> <!-- ibatorPlugin ibator插件 begin --> <!-- 所有的ibatorPlugin 继承自IbatorPluginAdapter,包名必须是org.apache.ibatis.ibator.plugins.XXX,具体实现可以参考官方文档 --> <!-- 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.PackageRefactorPlugin"> <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="com.li.test.common.database.entity" /> <property name="targetProject" value="src" /> </ibatorPlugin> --> <!-- ibatorPlugin ibator插件 end --> <!-- JDBC链接配置 :当前使用的是oracle数据库;数据库连接属性也可以使用这种格式 <property name="userId" value="test"/>--> <!-- driverClass="com.mysql.jdbc.Driver" --> <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl" userId="test" password="test"> <!-- 是否开启数据库返回注释 :MySQL默认是true,oracle默认是false--> <property name="remarksReporting" value="true" /> </jdbcConnection> <!--默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal --> <!-- <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> --> <!-- javaBean生成器 --> <!-- targetPackage:目标包路径 targetProject:目标项目路径,默认="src",如果使用maven环境请指定为绝对路径,比如:targetProject="C:\workspace3\ibatisDemo\src\main\java" --> <javaModelGenerator targetPackage="com.li.test.common.database.entity" targetProject="src"> <!-- 如果为TRUE,下面的设置了schema,那么包名就是会增加个schema名 例如:schema="userName1",生成类:com.li.test.common.database.entity.userName1.XXXX; --> <property name="enableSubPackages" value="true" /> <!-- 继承哪个父类,这个为了有时候日志需要打印整个对象,而采用的apache打印对象的每个属性 --> <!--<property name="rootClass" value="org.apache.ibatis.ibator.ant.BaseBean" /> --> <property name="rootClass" value="ibator.BaseBean" /> </javaModelGenerator> <!-- SQL映射文件生成器 --> <sqlMapGenerator targetPackage="com.li.test.common.database.sqlmap" targetProject="src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- dao生成器 --> <!-- type:生成dao类的模板,可选择IBATIS、SPRING、GENERIC-CI、GENERIC-SI --> <!-- implementationPackage:dao实现类的包名 --> <daoGenerator type="SPRING" implementationPackage="com.li.test.common.database.dao.impl" targetPackage="com.li.test.common.database.dao" targetProject="src" > <property name="enableSubPackages" value="true" /> </daoGenerator> <!-- 表属性映射生成器 --> <!-- schema:即用户名 --> <!-- tableName:数据库表名 --> <!-- domainObjectName:生成的类名,不写则默认和表名相同 --> <!-- 如果数据库里面有多个相同表名在不同的schema下,那么你得加上: schema="XXXX"和<property name="ignoreQualifiersAtRuntime" value="true" /> 不然ibator会以找到的最后一个为最终对象 --> <table schema="test" tableName="t_questionwh" domainObjectName="QuestionwhEO"> <!-- 忽略schema,避免在xml中出现schema.表名 --> <property name="ignoreQualifiersAtRuntime" value="true" /> <!-- columnOverride 指定属性名称,不指定则以默认规则处理:字母先全变小写,然后去掉下划线,下划线后首字母大写 --> <!-- <columnOverride column="finish_flag" javaType="Boolean"/> <columnOverride column="receive_fee" javaType="Long" /> <columnOverride column="operate_flag" javaType="Integer" /> <columnOverride column="out_daily_total" javaType="BigDecimal" /> --> <generatedKey column="ID" sqlStatement="select seq_questionwh from dual" identity="false"/> <columnOverride column="id" javaType="Long" /> <!-- 精确到时分秒时,需要设置下: jdbcType="TIMESTAMP"--> <columnOverride column="createtime" jdbcType="TIMESTAMP"/> </table> <table schema="test" tableName="t_questiondetil" domainObjectName="QuestionDetailEO"> <property name="ignoreQualifiersAtRuntime" value="true" /> </table> </ibatorContext> </ibatorConfiguration>
4. 添加ibator插件java类
ibatorConfig.xml中使用了自定义插件org.apache.ibatis.ibator.plugins.PackageRefactorPlugin
源码如下:
package org.apache.ibatis.ibator.plugins; import java.util.List; import org.apache.ibatis.ibator.api.FullyQualifiedTable; import org.apache.ibatis.ibator.api.IbatorPluginAdapter; import org.apache.ibatis.ibator.api.IntrospectedTable; import org.apache.ibatis.ibator.api.dom.xml.XmlElement; import org.apache.ibatis.ibator.config.TableConfiguration; public class PackageRefactorPlugin extends IbatorPluginAdapter{ @Override public boolean validate(List<String> warnings) { // TODO Auto-generated method stub return true; } @Override public void initialized(IntrospectedTable introspectedTable) { TableConfiguration tableConfig = introspectedTable.getTableConfiguration(); String pack = getPackage(tableConfig); calculateDAOAttributes(pack,introspectedTable); calculateModelAttributes(pack,introspectedTable); calculateXmlAttributes(pack,introspectedTable); } protected void calculateXmlAttributes(String pakkage,IntrospectedTable introspectedTable){ introspectedTable.setIbatis2SqlMapPackage(pakkage); String sqlmap = introspectedTable.getFullyQualifiedTable().getDomainObjectName().replaceAll("EO", "")+".xml"; introspectedTable.setIbatis2SqlMapFileName(sqlmap); } protected void calculateDAOAttributes(String pakkage,IntrospectedTable introspectedTable) { FullyQualifiedTable fullyQualifiedTable = introspectedTable.getFullyQualifiedTable(); StringBuilder sb = new StringBuilder(); sb.append(pakkage); sb.append('.'); sb.append(fullyQualifiedTable.getDomainObjectName().replaceAll("EO", "")); sb.append("DAOImpl"); //$NON-NLS-1$ introspectedTable.setDAOImplementationType(sb.toString()); sb.setLength(0); sb.append(pakkage); sb.append('.'); sb.append(fullyQualifiedTable.getDomainObjectName().replaceAll("EO", "")); sb.append("DAO"); //$NON-NLS-1$ introspectedTable.setDAOInterfaceType(sb.toString()); } protected void calculateModelAttributes(String pakkage,IntrospectedTable introspectedTable) { FullyQualifiedTable fullyQualifiedTable = introspectedTable.getFullyQualifiedTable(); StringBuilder sb = new StringBuilder(); sb.append(pakkage); sb.append('.'); sb.append(fullyQualifiedTable.getDomainObjectName()); sb.append("Key"); //$NON-NLS-1$ introspectedTable.setPrimaryKeyType(sb.toString()); sb.setLength(0); sb.append(pakkage); sb.append('.'); sb.append(fullyQualifiedTable.getDomainObjectName()); introspectedTable.setBaseRecordType(sb.toString()); sb.setLength(0); sb.append(pakkage); sb.append('.'); sb.append(fullyQualifiedTable.getDomainObjectName().replaceAll("EO", "")); sb.append("WithBLOBs"); //$NON-NLS-1$ introspectedTable.setRecordWithBLOBsType(sb.toString()); sb.setLength(0); sb.append(pakkage); sb.append('.'); sb.append(fullyQualifiedTable.getDomainObjectName().replaceAll("EO", "")); sb.append("Example"); //$NON-NLS-1$ introspectedTable.setExampleType(sb.toString()); } public String getPackage(TableConfiguration tableConfig){ String basePack = "com.sdo.mas.common.database"; String pack = tableConfig.getProperty("package"); if( null != pack && "".equals(pack) == false){ return pack; }else{ pack = tableConfig.getTableName().toLowerCase().replaceAll("_", ""); } pack = basePack +"."+pack; return pack; } @Override public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { return true; } @Override public boolean sqlMapUpdateByExampleSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { return true; } @Override public boolean sqlMapUpdateByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { // AbstractXmlElementGenerator generator = new UpdateByExampleWithBLOBsElementGenerator(); // generator.setIntrospectedTable(introspectedTable); // generator.setIbatorContext(ibatorContext); // generator.addElements(element); return true; } @Override public boolean sqlMapUpdateByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { // AbstractXmlElementGenerator generator = new UpdateByExampleWithoutBLOBsElementGenerator(); // generator.setIntrospectedTable(introspectedTable); // generator.setIbatorContext(ibatorContext); // generator.addElements(element); return true; } @Override public boolean sqlMapUpdateByPrimaryKeySelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { // AbstractXmlElementGenerator generator = new UpdateByPrimaryKeySelectiveElementGenerator(); // generator.setIntrospectedTable(introspectedTable); // generator.setIbatorContext(ibatorContext); // generator.addElements(element); return true; } @Override public boolean sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { // AbstractXmlElementGenerator generator = new UpdateByPrimaryKeyWithBLOBsElementGenerator(); // generator.setIntrospectedTable(introspectedTable); // generator.setIbatorContext(ibatorContext); // generator.addElements(element); return true; } @Override public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { // AbstractXmlElementGenerator generator = new UpdateByPrimaryKeyWithoutBLOBsElementGenerator(); // generator.setIntrospectedTable(introspectedTable); // generator.setIbatorContext(ibatorContext); // generator.addElements(element); return true; } @Override public boolean sqlMapInsertSelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { // AbstractXmlElementGenerator generator = new InsertSelectiveElementGenerator(); // generator.setIntrospectedTable(introspectedTable); // generator.setIbatorContext(ibatorContext); // generator.addElements(element); return true; } }
5. 生成代码
java类如下,直接执行它就行
package com.li.test.util; 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[] args) { try { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; String file = IbatorRunTest.class.getClassLoader().getResource("ibatorConfig.xml").getFile(); File configFile = new File(file); 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(); } } }
6. 使用说明
http://www.cnblogs.com/angus332770349/archive/2012/06/04/2534970.html