package com.example.demo.Test;
import java.io.*;
/**
* 作者: lin
* 描述:
* 日期: 2018/12/5 14:04
*/
public class myTest {
public static void main(String[] args) {
String filePath = "D:\\00t1.txt";
myTest main = new myTest();
main.getModel(filePath);
main.getResultMapXML(filePath);
}
private void getModel(String filePath) {
System.out.println("//------------以下是生成的Model代码-----------");
InputStream is = null;
String propertyName;
String typeName;
String commentStr;
try {
is = new FileInputStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 512);
// 读取一行,存储于字符串列表中
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.length() <= 1) {
continue;
}
// 生成Model, private String aaColumn; //a_column的注释
if (line.contains("CREATE")) {
System.out.println();
System.out.println(line.substring(line.indexOf("`") + 1, line.lastIndexOf("`")));
}
propertyName = this.getPropertyName(line);
typeName = this.getType(line);
if (!"NULL".equals(typeName) && !"NULL".equals(propertyName)) {
commentStr = this.getComment(line);
System.out.println("private " + typeName + " " + propertyName + "; //" + commentStr);
}
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
is = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}//getModel
/**
* 根据SQL获取 ResultMap XML代码,支持多个建表语句
* 输入:建表语句的文件路径
* 输出:ResultMap
*
* @param filePath
*/
private void getResultMapXML(String filePath) {
InputStream is = null;
try {
is = new FileInputStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 512);
// 读取一行,存储于字符串列表中
System.out.println();
System.out.println("<!-- ----------以下是生成的XML代码--------- -->");
String tableName;
String typeName;
String propertyName;
String columnName;
StringBuilder sbTableName = new StringBuilder();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
line = line.trim();
if (line == null || line.length() == 0 || line.contains("-") || line.contains("DROP")) {
continue;
}
if (line.contains("CREATE")) {
tableName = line.substring(line.indexOf("`") + 1, line.lastIndexOf("`"));
tableName = this.getTableName(tableName);
sbTableName.append("<resultMap id=\"").append(tableName).append("Map\" ").append("class=\"").append(tableName).append("\">");
System.out.println(sbTableName.toString());
sbTableName.delete(0, sbTableName.length());
}
//生成xml <result property="abcProperty" column="abc_property"/>
propertyName = this.getPropertyName(line);
if (!"NULL".equals(propertyName)) {
columnName = this.getColumnName(line);
typeName = this.getType(line);
System.out.print("<result property=" + "\"" + propertyName + "\""
+ " column=" + "\"" + columnName + "\"");
if ("Date".equals(typeName)) {
System.out.println(" javaType=\"java.util.Date\""
+ "/>");
} else {
System.out.println("/>");
}
}
if (line.contains("ENGINE=")) {
System.out.println("</resultMap>");
System.out.println("");
}
}//for
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
is = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}//getResultMapXML
/**
* 获取对应的java类型
*
* @param line
* @return
*/
private String getType(String line) {
if (line.length() > 1 && !line.contains("CREATE") && !line.contains("PRIMARY") && !line.contains("KEY") && !line.contains("ENGINE=")) {
line = line.substring(line.lastIndexOf("`") + 2, line.length());
line = line.substring(0, line.indexOf(" "));
if (line.contains("bigint")) {
return "Long";
} else if (line.contains("char")) {//此时也包括 varchar类型
return "String";
} else if (line.contains("double")) {
return "Double";
} else if (line.contains("int")) {//此时也包括 tinyint类型
return "Integer";
} else if (line.contains("date")) {//此时也包括 datetime类型
return "Date";
}
}
return "NULL";
}
/**
* 把字段名转化为驼峰命名的属性名,abc_property ->abcProperty
*
* @param line
* @return
*/
private String getPropertyName(String line) {
if (line.length() > 1 && !line.contains("CREATE") && !line.contains("PRIMARY") && !line.contains("KEY") && !line.contains("ENGINE=")) {
String tempDelimiter = line.substring(line.indexOf("`") + 1, line.lastIndexOf("`"));
String tempDelimiterArray[] = tempDelimiter.split("_");//如果不包含“_”,此时就含有字符串一个元素
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tempDelimiterArray.length; i++) {
if (i == 0) {
sb.append(tempDelimiterArray[i]);
}
if (i != 0) { //除第一个单词外,其他的单词的首字母大写,String substring(int beginIndex)
sb.append(tempDelimiterArray[i].substring(0, 1).toUpperCase() + tempDelimiterArray[i].substring(1));
}
}//for
return sb.toString();
}
return "NULL";
}
/**
* 获取SQL的注释
*
* @param line
* @return
*/
private String getComment(String line) {
if (line.contains("COMMENT")) {
return line.substring(line.indexOf("'") + 1, line.lastIndexOf("',"));
} else if (line.contains("AUTO_INCREMENT")) {
return "主键Id";
} else if (line.contains("created_time")) {
return "创建时间";
} else if (line.contains("updated_time")) {
return "修改时间";
}
return "NULL";
}
/**
* 获取sql的字段名.`abc_property` -> abc_property
*
* @param line
* @return
*/
private String getColumnName(String line) {
line = line.substring(line.indexOf("`") + 1, line.lastIndexOf("`"));
return line;
}
/**
* 转化为驼峰命名的表名,abc_table -> AbcTable
*
* @param tempDelimiter
* @return
*/
private String getTableName(String tempDelimiter) {
String tempDelimiterArray[] = tempDelimiter.split("_");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tempDelimiterArray.length; i++) {
sb.append(tempDelimiterArray[i].substring(0, 1).toUpperCase() + tempDelimiterArray[i].substring(1));
}//for
return sb.toString();
}
}
输出结果
//------------以下是生成的Model代码-----------
xxxxx
private String ID; //主键
private String INSERTLOGINID; //新增操作人ID
private String INSERTSYSTEMTAG; //新增操作系统标识
private String INSERTTIME; //新增操作时间(yyyy-MM-dd HH:mm:ss)
private String UPDATELOGINID; //更新操作人ID
private String UPDATESYSTEMTAG; //更新操作系统标识
private String UPDATETIME; //更新操作时间(yyyy-MM-dd HH:mm:ss)
private String USINGSTATUS; //在用状态(枚举)
private String BUSID; //业务ID
private String CELLPHONE; //手机号码
private String CHECKCODE; //校验码
private String EMAIL; //电子邮箱
private Integer ERRORTIMES; //密码错误次数
private String FIELD1; //备用字段
private String FIELD2; //备用字段
private String FIELD3; //备用字段
private String LOGINNAME; //登录名
private String PWD; //密码(MD5)
private String SECURITYCODE; //验证码
private String SYSTEMTAG; //系统标识
private String TOKEN; //登录token
private String USERCARD; //身份证号码
private String USERID; //人员ID
private String DEVICEID; //登录设备ID
private String DEVICEMODEL; //登录设备型号
private String TOKENAPP; //一次登录获取的TOKEN
private Integer LOGINCOUNT; //0' COMMENT '登录次数统计
<!-- ----------以下是生成的XML代码--------- -->
<resultMap id="xxxx" class="xxxxx">
<result property="ID" column="ID"/>
<result property="INSERTLOGINID" column="INSERT_LOGIN_ID"/>
<result property="INSERTSYSTEMTAG" column="INSERT_SYSTEM_TAG"/>
<result property="REMARKS" column="REMARKS"/>
<result property="UPDATELOGINID" column="UPDATE_LOGIN_ID"/>
<result property="UPDATESYSTEMTAG" column="UPDATE_SYSTEM_TAG"/>
<result property="USINGSTATUS" column="USING_STATUS"/>
<result property="BUSID" column="BUS_ID"/>
<result property="CELLPHONE" column="CELLPHONE"/>
<result property="CHECKCODE" column="CHECK_CODE"/>
<result property="EMAIL" column="EMAIL"/>
<result property="ERRORTIMES" column="ERROR_TIMES"/>
<result property="FIELD1" column="FIELD1"/>
<result property="FIELD2" column="FIELD2"/>
<result property="FIELD3" column="FIELD3"/>
<result property="LOGINNAME" column="LOGIN_NAME"/>
<result property="PWD" column="PWD"/>
<result property="SECURITYCODE" column="SECURITY_CODE"/>
<result property="SYSTEMTAG" column="SYSTEM_TAG"/>
<result property="TOKEN" column="TOKEN"/>
<result property="USERCARD" column="USER_CARD"/>
<result property="USERID" column="USER_ID"/>
<result property="DEVICEID" column="DEVICE_ID"/>
<result property="DEVICEMODEL" column="DEVICE_MODEL"/>
<result property="RISKINFO" column="RISK_INFO"/>
<result property="TOKENAPP" column="TOKEN_APP"/>
<result property="LOGINCOUNT" column="LOGIN_COUNT"/>
</resultMap>