11
11
import org .slf4j .LoggerFactory ;
12
12
import org .springframework .core .io .UrlResource ;
13
13
import org .springframework .util .StreamUtils ;
14
- import org .w3c .dom .*;
14
+ import org .w3c .dom .DOMImplementation ;
15
+ import org .w3c .dom .Document ;
16
+ import org .w3c .dom .Element ;
17
+ import org .w3c .dom .Node ;
18
+ import org .w3c .dom .NodeList ;
15
19
import org .w3c .dom .ls .DOMImplementationLS ;
16
20
import org .w3c .dom .ls .LSOutput ;
17
21
import org .w3c .dom .ls .LSSerializer ;
28
32
import java .net .URI ;
29
33
import java .net .URL ;
30
34
import java .nio .charset .StandardCharsets ;
31
- import java .sql .*;
35
+ import java .sql .Connection ;
36
+ import java .sql .DatabaseMetaData ;
37
+ import java .sql .DriverManager ;
38
+ import java .sql .ResultSet ;
39
+ import java .sql .ResultSetMetaData ;
40
+ import java .sql .Types ;
32
41
import java .util .ArrayList ;
33
42
import java .util .List ;
43
+ import java .util .Objects ;
34
44
import java .util .Properties ;
35
45
36
46
/**
@@ -74,6 +84,12 @@ public static void generator(Properties source) throws Exception {
74
84
if (source != null ) {
75
85
properties .putAll (source );
76
86
}
87
+
88
+ String tk = PropertiesUtils .getProp ("tk.plugin.class" );
89
+ if (StringUtils .isBlank (tk )) {
90
+ properties .setProperty ("tk.plugin.class" , "org.javamaster.mybatis.generator.plugin.TkMapperPlugin" );
91
+ }
92
+
77
93
inputStream = addTableToXml ();
78
94
inputStream .mark (inputStream .available ());
79
95
logger .info ("xml content:\r \n {}" , StreamUtils .copyToString (inputStream , StandardCharsets .UTF_8 ));
@@ -128,13 +144,14 @@ private static InputStream addTableToXml() throws Exception {
128
144
.evaluate ("/generatorConfiguration/context/plugin[@type='org.javamaster.mybatis.generator.plugin.MybatisGeneratorPlugin']" , rootEle , XPathConstants .NODE );
129
145
parentNode .removeChild (node );
130
146
}
131
- if (!tkMybatis ) {
132
- Node node = (Node ) xPath
133
- .evaluate ("/generatorConfiguration/context/plugin[@type='org.javamaster.mybatis.generator.plugin.TkMapperPlugin']" , rootEle , XPathConstants .NODE );
134
- parentNode .removeChild (node );
135
- } else {
147
+ if (tkMybatis ) {
136
148
Node node = parentNode .getAttributes ().getNamedItem ("targetRuntime" );
137
149
node .setNodeValue ("MyBatis3Simple" );
150
+ } else {
151
+ String ex = "/generatorConfiguration/context/plugin[@type='%s']" ;
152
+ ex = String .format (ex , PropertiesUtils .getProp ("tk.plugin.class" ));
153
+ Node node = (Node ) xPath .evaluate (ex , rootEle , XPathConstants .NODE );
154
+ parentNode .removeChild (node );
138
155
}
139
156
140
157
String jdbcUrl = PropertiesUtils .getProp ("jdbc.url" );
@@ -177,9 +194,7 @@ private static InputStream addTableToXml() throws Exception {
177
194
private static void changeDatabaseTinyintToInteger (XPath xPath , Document doc ) throws Exception {
178
195
Element rootEle = doc .getDocumentElement ();
179
196
boolean close = PropertiesUtils .getPropAsBoolean ("disable.tinyint.to.integer" , "true" );
180
- if (close ) {
181
- return ;
182
- }
197
+
183
198
try (Connection connection = DriverManager .getConnection (PropertiesUtils .getProp ("jdbc.url" ),
184
199
PropertiesUtils .getProp ("jdbc.user" ), PropertiesUtils .getProp ("jdbc.password" ))) {
185
200
NodeList nodeList = (NodeList ) xPath .evaluate ("//table" , rootEle , XPathConstants .NODESET );
@@ -193,15 +208,24 @@ private static void changeDatabaseTinyintToInteger(XPath xPath, Document doc) th
193
208
ResultSet rs = databaseMetaData .getColumns (null , null , tableName , null );
194
209
while (rs .next ()) {
195
210
int dataType = rs .getInt ("DATA_TYPE" );
196
- if (Types .TINYINT != dataType && Types .BIT != dataType ) {
197
- continue ;
211
+ if (!close && (Types .TINYINT == dataType || Types .BIT == dataType )) {
212
+ String columnName = rs .getString ("COLUMN_NAME" );
213
+ Element columnOverrideEle = doc .createElement ("columnOverride" );
214
+ columnOverrideEle .setAttribute ("column" , columnName );
215
+ columnOverrideEle .setAttribute ("javaType" , "Integer" );
216
+ columnOverrideEle .setAttribute ("jdbcType" , "INTEGER" );
217
+ tableEle .appendChild (columnOverrideEle );
218
+ }
219
+
220
+ String typeName = rs .getString ("TYPE_NAME" );
221
+ if (Objects .equals (typeName , "INT UNSIGNED" )) {
222
+ String columnName = rs .getString ("COLUMN_NAME" );
223
+ Element columnOverrideEle = doc .createElement ("columnOverride" );
224
+ columnOverrideEle .setAttribute ("column" , columnName );
225
+ columnOverrideEle .setAttribute ("javaType" , "Long" );
226
+ columnOverrideEle .setAttribute ("jdbcType" , "BIGINT" );
227
+ tableEle .appendChild (columnOverrideEle );
198
228
}
199
- String columnName = rs .getString ("COLUMN_NAME" );
200
- Element columnOverrideEle = doc .createElement ("columnOverride" );
201
- columnOverrideEle .setAttribute ("column" , columnName );
202
- columnOverrideEle .setAttribute ("javaType" , "Integer" );
203
- columnOverrideEle .setAttribute ("jdbcType" , "INTEGER" );
204
- tableEle .appendChild (columnOverrideEle );
205
229
}
206
230
}
207
231
}
0 commit comments