Reference :
http://www.javabeat.net/articles/52-spring-ibatis-integration-1.html
http://www.mybatis.org/generator/running/runningWithJava.html
http://www.linuxso.com/architecture/13674.html
0.download required jar file :
oracle/ojdbc14.jar
mybatis/mybatis-generator-core-1.3.0.jar
mybatis/mybatis-2.3.5.jar
1.ibatis-Generator.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="NGPIBase" targetRuntime="Ibatis2Java5">
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@DBserver:1521:XXX"
userId="userid"
password="userPwd">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.xxx.g3.report.model" targetProject="src">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="false" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.xxx.g3.resources.db" targetProject="src">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator type="SPRING" targetPackage="com.xxxx.g3.report.persistence.dao" targetProject="src">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table schema="databaseSchemaName" tableName="REPORT" domainObjectName="Report"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="false"/>
<property name="useGeneratedKeys" value="true"/>
<property name="rootClass" value="com.xxxx.base.model.BaseVO"/>
<property name="rootInterface" value="com.xxxx.base.persistence.dao.BaseDAO"/>
<!-- <generatedKey column="RPT_REF" sqlStatement="SELECT RPT_SEQ.nextval FROM dual" type="pre"/> -->
<columnRenamingRule searchString="%_" replaceString="" />
</table>
</context>
</generatorConfiguration>
2.PersistenceGenerator.java
public class PersistenceGenerator {
/**
* DAO, DAO Implementation, model, model example and ibatis config files
* will be generated in the specified folders
* @param location - location of the Persistence Configuration file.
* @throws Exception
*/
public void generate(String location) throws Exception {
try {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
InputStream is = this.getClass().getResourceAsStream(location);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
e.printStackTrace();
//throw e;
}
}
public static void main(String[] args){
PersistenceGenerator generator = new PersistenceGenerator();
try {
generator.generate("/com/xxx/g3/resources/ibatis-Generator.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
================遇到的问题以及解决:
1. 在TABLENAME_SqlMap.xml中, parameterClass = 参数类型 resultClass = 返回类型 resultMap = 返回成员映射如何要自己修改要注意上面三个变量;例如:
<select id="selectByStatus" resultMap="BaseResultMap" parameterClass="java.lang.String" >
select
<include refid="ACHADMIN_CURRENCY.Base_Column_List" />
from ACHADMIN.CURRENCY
where STATUS = #status:CHAR#
</select>
Pass in 的status 是String,就要说明是String.
问题是:Auto gen的
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterClass="com.bcsis.report.model.Currency" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu Feb 23 17:55:17 SGT 2012.
-->
select
<include refid="ACHADMIN_CURRENCY.Base_Column_List" />
from ACHADMIN.CURRENCY
where CURRENCYCODE = #currencycode:CHAR#
</select>
parameterClass 和currencyCode类型也是不一致,却可以通过。问题待解决??。。。。