Skip to content

Commit 0ea2a56

Browse files
author
liangyudong
committed
完善mybatis生成器
1 parent fff7248 commit 0ea2a56

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

mybatis-generator/src/main/java/org/javamaster/mybatis/generator/MySqlMybatisGenerator.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import org.slf4j.LoggerFactory;
1212
import org.springframework.core.io.UrlResource;
1313
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;
1519
import org.w3c.dom.ls.DOMImplementationLS;
1620
import org.w3c.dom.ls.LSOutput;
1721
import org.w3c.dom.ls.LSSerializer;
@@ -28,9 +32,15 @@
2832
import java.net.URI;
2933
import java.net.URL;
3034
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;
3241
import java.util.ArrayList;
3342
import java.util.List;
43+
import java.util.Objects;
3444
import java.util.Properties;
3545

3646
/**
@@ -74,6 +84,12 @@ public static void generator(Properties source) throws Exception {
7484
if (source != null) {
7585
properties.putAll(source);
7686
}
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+
7793
inputStream = addTableToXml();
7894
inputStream.mark(inputStream.available());
7995
logger.info("xml content:\r\n{}", StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
@@ -128,13 +144,14 @@ private static InputStream addTableToXml() throws Exception {
128144
.evaluate("/generatorConfiguration/context/plugin[@type='org.javamaster.mybatis.generator.plugin.MybatisGeneratorPlugin']", rootEle, XPathConstants.NODE);
129145
parentNode.removeChild(node);
130146
}
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) {
136148
Node node = parentNode.getAttributes().getNamedItem("targetRuntime");
137149
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);
138155
}
139156

140157
String jdbcUrl = PropertiesUtils.getProp("jdbc.url");
@@ -177,9 +194,7 @@ private static InputStream addTableToXml() throws Exception {
177194
private static void changeDatabaseTinyintToInteger(XPath xPath, Document doc) throws Exception {
178195
Element rootEle = doc.getDocumentElement();
179196
boolean close = PropertiesUtils.getPropAsBoolean("disable.tinyint.to.integer", "true");
180-
if (close) {
181-
return;
182-
}
197+
183198
try (Connection connection = DriverManager.getConnection(PropertiesUtils.getProp("jdbc.url"),
184199
PropertiesUtils.getProp("jdbc.user"), PropertiesUtils.getProp("jdbc.password"))) {
185200
NodeList nodeList = (NodeList) xPath.evaluate("//table", rootEle, XPathConstants.NODESET);
@@ -193,15 +208,24 @@ private static void changeDatabaseTinyintToInteger(XPath xPath, Document doc) th
193208
ResultSet rs = databaseMetaData.getColumns(null, null, tableName, null);
194209
while (rs.next()) {
195210
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);
198228
}
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);
205229
}
206230
}
207231
}

mybatis-generator/src/main/resources/generatorConfig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<plugin type="org.javamaster.mybatis.generator.plugin.MybatisGeneratorOverridePlugin"/>
1818
<plugin type="org.javamaster.mybatis.generator.plugin.MybatisEnumsPlugin"/>
1919
<plugin type="org.javamaster.mybatis.generator.plugin.MybatisGeneratorPlugin"/>
20-
<plugin type="org.javamaster.mybatis.generator.plugin.TkMapperPlugin">
20+
<plugin type="${tk.plugin.class}">
2121
<property name="mappers" value="${package.Mapper}"/>
2222
</plugin>
2323

0 commit comments

Comments
 (0)