将数据库脚本转换成ibatis代码自动生成脚本
表结构:
create table tab_hr_emp_education(
edu_id varchar2(10) not null,
emp_id varchar2(10) not null,
school varchar2(50) not null,
record varchar2(50) not null,
study_sys char(1) not null,
study_context varchar2(2000),
startdate date,
enddate date,
relate_person varchar2(30),
relate_phone varchar2(40),
remark varchar2(2000)
);
转换后的结果:
<table tableName="tab_hr_emp_education" domainObjectName="HrEmpEducationBean">
<property name="useActualColumnNames" value="false"/>
<columnOverride column="edu_id" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="emp_id" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="school" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="record" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="study_sys" jdbcType="CHAR" javaType="String"/>
<columnOverride column="study_context" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="startdate" jdbcType="Date" javaType="java.util.Date"/>
<columnOverride column="enddate" jdbcType="Date" javaType="java.util.Date"/>
<columnOverride column="relate_person" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="relate_phone" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="remark" jdbcType="VARCHAR2" javaType="String"/>
</table>
完整的abatorConfig1.xml文件
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"
"http://ibatis.apache.org/dtd/abator-config_1_0.dtd ">
<!-- Notice(should set true): SqlMapConfig.xml/sqlMapConfig/settings/useStatementNamespaces="true" -->
<abatorConfiguration>
<abatorContext id="forTest">
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.10.84:1521:ora10"
userId="nnbh_spro"
password="nnbh_spro">
<classPathEntry location="D:\ibaits\classes12.jar" />
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.forlink.cc.entity.bean"
targetProject="D:\CVS_ROOT\impl\java\ver1.0\apps\cc\src">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="sqlmap.autogenerate"
targetProject="D:\CVS_ROOT\impl\java\ver1.0\apps\conf\cc">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<daoGenerator type="GENERIC-CI" targetPackage="com.forlink.cc.entity.dao"
targetProject="D:\CVS_ROOT\impl\java\ver1.0\apps\cc\src">
<property name="enableSubPackages" value="false"/>
<property name="methodNameCalculator" value="default"/>
<property name="exampleMethodVisibility" value="public"/>
<property name="rootClass" value="com.forlink.cc.DAO"/>
</daoGenerator>
<table tableName="tab_hr_emp_education" domainObjectName="HrEmpEducationBean">
<property name="useActualColumnNames" value="false"/>
<columnOverride column="edu_id" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="emp_id" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="school" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="record" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="study_sys" jdbcType="CHAR" javaType="String"/>
<columnOverride column="study_context" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="startdate" jdbcType="Date" javaType="java.util.Date"/>
<columnOverride column="enddate" jdbcType="Date" javaType="java.util.Date"/>
<columnOverride column="relate_person" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="relate_phone" jdbcType="VARCHAR2" javaType="String"/>
<columnOverride column="remark" jdbcType="VARCHAR2" javaType="String"/>
</table>
</abatorContext>
</abatorConfiguration>
转换数据代码
public static void main(String[] args)
{
String xml = "";
try
{
File b = new File("D:\\1.sql");
BufferedReader bis = new BufferedReader(new FileReader(b));
String t = "";
boolean start = false;
while ((t = bis.readLine()) != null)
{
if(!start && t.indexOf("create")>-1 && t.indexOf("table")>-1)
{
start = true;
String tableName = t.substring(t.indexOf("table")+6,t.indexOf("(")).trim();
String tableBean = "";
char[] tt = tableName.toCharArray();
boolean tmpFlag = true;
for (int i = 4; i < tt.length; i++)
{
if(tmpFlag)
{
tableBean += String.valueOf(tt[i]).toUpperCase();
tmpFlag = false;
}
else
{
if(tt[i] == '_')
{
tmpFlag = true;
continue;
}
else
tableBean += String.valueOf(tt[i]);
}
}
tableBean += "Bean";
xml += "<table tableName=\""+tableName+"\" domainObjectName=\""+tableBean+"\">\n\t<property name=\"useActualColumnNames\" value=\"false\"/>\n";
// System.out.println(tableName.toUpperCase());
}
else
{
if(start)
{
if(t.indexOf("primary")>-1)
{
//xml += "\t<generatedKey column=\""+t.substring(t.indexOf("primary key")+11,t.indexOf(")")).trim().replaceAll("\\(", "")+"\" sqlStatement=\"ORACLE\" identity=\"true\" />\n" ;
if(t.indexOf(");")>-1)
{
start = false;
xml +="</table>\n";
}
continue;
}
if(t.indexOf(");")>-1)
{
start = false;
xml +="</table>\n";
continue;
}
//处理特殊类型
t = t.replaceAll("long varchar", "longvarchar");
t = t.replaceAll("\t", "*");
String tmp = t.replaceAll(" ", "*");
String[] bb = tmp.split("\\*");
String field = "";
String dataType = "";
for (int i = 0; i < bb.length; i++)
{
if(field.equals("") && !bb[i].equals(""))
{
field = bb[i].trim();
// field = field.replaceAll(" ", "");
continue;
}
else if(dataType.equals("") && !bb[i].equals(""))
{
dataType = bb[i].trim().toUpperCase();
break;
}
}
if(dataType.indexOf("(")>-1)
dataType = dataType.substring(0,dataType.indexOf('('));
if(dataType.indexOf(',')>-1)
dataType = dataType.substring(0,dataType.indexOf(','));
if(dataType.equals("DATE"))
dataType = "Date";
xml += "\t<columnOverride column=\""+field.toLowerCase()+"\" jdbcType=\""+ dataType + "\" javaType=\""+toJavaType(dataType)+"\"/>\n";
}
}
}
System.out.println(xml);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static String toJavaType(String dataType)
{
dataType = dataType.toUpperCase();
if(dataType.equals("NUMBER"))
{
return "long";
}
else if(dataType.equals("INT"))
{
return "int";
}
else if(dataType.equals("VARCHAR2"))
{
return "String";
}
else if(dataType.equals("VARCHAR"))
{
return "String";
}
else if(dataType.equals("LONGVARCHAR"))
{
return "String";
}
else if(dataType.equals("TEXT"))
{
return "String";
}
else if(dataType.equals("BLOB"))
{
return "byte[]";
}
else if(dataType.equals("DATE"))
{
return "java.util.Date";
}
else if(dataType.equals("TIMESTAMP"))
{
return "java.sql.Timestamp";
}
else if(dataType.equals("CHAR"))
{
return "String";
} else
return "";
}