@@ -102,12 +102,16 @@ public static ClassInfo processTableIntoClassInfo(String tableSql) throws IOExce
102102
103103 String [] fieldLineList = fieldListTmp .split ("," );
104104 if (fieldLineList .length > 0 ) {
105+ int i =0 ;//i为了解决primary key关键字出现的地方,出现在前3行,一般和id有关
105106 for (String columnLine :fieldLineList ) {
106- columnLine = columnLine .replaceAll ("\n " ,"" ).replaceAll ("\t " ,"" ).trim (); // `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
107+ i ++;
108+ columnLine = columnLine .replaceAll ("\n " ,"" ).replaceAll ("\t " ,"" ).trim ();
109+ // `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
110+ // 2018-9-18 zhengk 修改为contains,提升匹配率和匹配不按照规矩出牌的语句
107111 if (!columnLine .contains ("constraint" )&&!columnLine .contains ("using" )
108112 &&!columnLine .contains ("storage" )&&!columnLine .contains ("pctincrease" )
109113 &&!columnLine .contains ("buffer_pool" )&&!columnLine .contains ("tablespace" )
110- &&!columnLine .contains ("primary" )){
114+ &&!( columnLine .contains ("primary" )&& i > 3 )){
111115
112116 //如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理
113117 if (columnLine .length ()<5 ) continue ;
@@ -127,20 +131,20 @@ public static ClassInfo processTableIntoClassInfo(String tableSql) throws IOExce
127131 columnLine = columnLine .substring (columnLine .indexOf ("`" )+1 ).trim (); // int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
128132 String fieldClass = Object .class .getSimpleName ();
129133 //2018-9-16 zhengk 补充char/clob/blob/json等类型,如果类型未知,默认为String
130- if (columnLine .startsWith ("int" ) || columnLine .startsWith ("tinyint" ) || columnLine .startsWith ("smallint" )) {
134+ if (columnLine .contains ("int" ) || columnLine .contains ("tinyint" ) || columnLine .contains ("smallint" )) {
131135 fieldClass = Integer .TYPE .getSimpleName ();
132- } else if (columnLine .startsWith ("bigint" )) {
136+ } else if (columnLine .contains ("bigint" )) {
133137 fieldClass = Long .TYPE .getSimpleName ();
134- } else if (columnLine .startsWith ("float" )) {
138+ } else if (columnLine .contains ("float" )) {
135139 fieldClass = Float .TYPE .getSimpleName ();
136- } else if (columnLine .startsWith ("double" )) {
140+ } else if (columnLine .contains ("double" )) {
137141 fieldClass = Double .TYPE .getSimpleName ();
138- } else if (columnLine .startsWith ("datetime" ) || columnLine .startsWith ("timestamp" )) {
142+ } else if (columnLine .contains ("datetime" ) || columnLine .contains ("timestamp" )) {
139143 fieldClass = Date .class .getSimpleName ();
140- } else if (columnLine .startsWith ("varchar" ) || columnLine .startsWith ("text" )|| columnLine .startsWith ("char" )
141- || columnLine .startsWith ("clob" )||columnLine .startsWith ("blob" )||columnLine .startsWith ("json" )) {
144+ } else if (columnLine .contains ("varchar" ) || columnLine .contains ("text" )|| columnLine .contains ("char" )
145+ || columnLine .contains ("clob" )||columnLine .contains ("blob" )||columnLine .contains ("json" )) {
142146 fieldClass = String .class .getSimpleName ();
143- } else if (columnLine .startsWith ("decimal" )||columnLine .startsWith ("number" )) {
147+ } else if (columnLine .contains ("decimal" )||columnLine .contains ("number" )) {
144148 fieldClass = BigDecimal .class .getSimpleName ();
145149 } else {
146150 fieldClass = String .class .getSimpleName ();
0 commit comments