1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| package com.qinjiangbo.gen.util;
import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.dom.DOMDocumentType; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.dom4j.tree.DefaultDocumentType;
import java.io.File; import java.io.FileOutputStream; import java.sql.*; import java.util.ArrayList; import java.util.List;
public class CreateGenConfigXML {
public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "Richard", "123456"); return connection; } catch (Exception e) { e.printStackTrace(); } return null; }
public static List<String> getTableNames() throws SQLException { List<String> names = new ArrayList<String>(); Connection connection = getConnection(); DatabaseMetaData databaseMetaData = connection.getMetaData(); String[] types = {"TABLE"}; ResultSet resultSet = databaseMetaData.getTables(null, null, "%", types); while (resultSet.next()) { String name = (String) resultSet.getObject("TABLE_NAME"); if (name.contains("(") || name.contains(")")) { continue; } names.add(name); } connection.close(); return names; }
private static Document createDom() throws SQLException { Document doc = DocumentHelper.createDocument(); doc.setDocType(new DOMDocumentType("generatorConfiguration", "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN", "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"));
Element root = doc.addElement("generatorConfiguration");
Element context = root.addElement("context"); context.addAttribute("id", "MBG"); context.addAttribute("targetRuntime", "Mybatis3"); context.addAttribute("defaultModelType", "conditional");
Element plugin = context.addElement("plugin"); plugin.addAttribute("type", "org.mybatis.generator.plugins.SerializablePlugin");
Element commentGenerator = context.addElement("commentGenerator"); Element property = commentGenerator.addElement("property"); property.addAttribute("name", "suppressAllComments"); property.addAttribute("value", "false");
Element jdbcConnection = context.addElement("jdbcConnection"); jdbcConnection.addAttribute("driverClass", "com.mysql.jdbc.Driver"); jdbcConnection.addAttribute("connectionURL", "jdbc:mysql://localhost:3306/mybatis"); jdbcConnection.addAttribute("userId", "Richard"); jdbcConnection.addAttribute("password", "123456");
Element javaTypeResolver = context.addElement("javaTypeResolver"); Element property2 = javaTypeResolver.addElement("property"); property2.addAttribute("name", "forceBigDecimals"); property2.addAttribute("value", "false");
Element javaModelGenerator = context.addElement("javaModelGenerator"); javaModelGenerator.addAttribute("targetPackage", "com.qinjiangbo.gen.model"); javaModelGenerator.addAttribute("targetProject", "src"); Element property3 = javaModelGenerator.addElement("property"); property3.addAttribute("name", "enableSubPackages"); property3.addAttribute("value", "true");
Element sqlMapGenerator = context.addElement("sqlMapGenerator"); sqlMapGenerator.addAttribute("targetPackage", "com.qinjiangbo.gen.mapper"); sqlMapGenerator.addAttribute("targetProject", "src"); Element property4 = sqlMapGenerator.addElement("property"); property4.addAttribute("name", "enableSubPackages"); property4.addAttribute("value", "true");
Element javaClientGenerator = context.addElement("javaClientGenerator"); javaClientGenerator.addAttribute("type", "XMLMAPPER"); javaClientGenerator.addAttribute("targetPackage", "com.qinjiangbo.gen.mapper"); javaClientGenerator.addAttribute("targetProject", "src"); Element property5 = javaClientGenerator.addElement("property"); property5.addAttribute("name", "enableSubPackages"); property5.addAttribute("value", "true");
List<String> names = getTableNames(); for (String name : names) { String[] words = name.split("_"); String entityName = ""; for (String word : words) { word = word.substring(0, 1).toUpperCase() + word.substring(1); if (entityName == null || entityName.equals("")) { entityName = word; } else { entityName = entityName + word; } } Element tableNode = context.addElement("table"); tableNode.addAttribute("tableName", name); tableNode.addAttribute("domainObjectName", entityName); tableNode.addAttribute("enableCountByExample", "true"); tableNode.addAttribute("enableUpdateByExample", "true"); tableNode.addAttribute("enableDeleteByExample", "true"); tableNode.addAttribute("enableSelectByExample", "true"); tableNode.addAttribute("selectByExampleQueryId", "true"); tableNode.addAttribute("enableDeleteByPrimaryKey", "true"); tableNode.addAttribute("enableSelectByPrimaryKey", "true"); tableNode.addAttribute("enableUpdateByPrimaryKey", "true"); tableNode.addAttribute("enableInsert", "true"); }
return doc; }
public static void writeXML() throws Exception { XMLWriter xmlWriter = null; String userDir = System.getProperty("user.dir"); String fileName = userDir + "/src/com/qinjiangbo/gen/util/generatorConfiguration.xml"; OutputFormat format = OutputFormat.createPrettyPrint(); FileOutputStream fos = new FileOutputStream(new File(fileName)); format.setEncoding("utf-8"); xmlWriter = new XMLWriter(fos, format); xmlWriter.write(createDom()); xmlWriter.close(); }
public static void main(String[] args) throws Exception { writeXML(); System.out.println("Completed!"); } }
|