准备条件:
将下面的文件放入同一目录下
操作步骤:
1/ 在 generatorConfig.xml 中配置相关的参数,与需要被自动生成的表
也可以 执行项目中的MybatisConfigAutoGen.java 将得到的结果复制过来 替换当前文档的内容
2/ 打开cmd 窗口,进入当前文件夹下,执行命令:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
3/ 将src 中的文件复制到 项目对应的目录下即可根据实际来修改
附:
1/ generatorConfig.xml 的内容为:(下面的配置文件是对应Oracle数据库)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <classPathEntry location="ojdbc7.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" userId="panie" password="panie123"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.panie.modules.configure.entity" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mappings.com.panie.modules.configure" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.panie.modules.configure.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="BPMP_VERSION" domainObjectName="BpmpVersion" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
2/ 使用Java 代码 遍历 ORACLE ,生成可用的配置
package com.panie.common.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MybatisConfigAutoGen { public static String jdbcDriver = "oracle.jdbc.OracleDriver"; public static String jdbcUrl = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL"; public static String jdbcUser = "panie"; public static String jdbcPassword = "panie123"; public static String newline = " "; public static String tableKey = "BPMP_VERSION"; public static String driverJar = "ojdbc7.jar";//所使用驱动包 public static String entityPkg = "com.panie.modules.configure.entity"; public static String mappingPkg = "mappings.com.panie.modules.configure"; public static String daoPkg = "com.panie.modules.configure.dao"; public static String genConfig() { StringBuilder sb = new StringBuilder(); sb.append("<?xml version="1.0" encoding="UTF-8"?>").append(newline); sb.append("<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" ").append(newline); sb.append(""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> ").append(newline); sb.append("<generatorConfiguration>").append(newline); sb.append(" <classPathEntry location="").append(driverJar).append(""/> ").append(newline); sb.append(" <context id="DB2Tables" targetRuntime="MyBatis3">").append(newline); sb.append(" <commentGenerator>").append(newline); sb.append(" <property name="suppressDate" value="true"/>").append(newline); sb.append(" <property name="suppressAllComments" value="true"/> ").append(newline); sb.append(" </commentGenerator>").append(newline); sb.append(" <jdbcConnection driverClass="").append(jdbcDriver).append("" connectionURL="").append(jdbcUrl).append("" userId="").append(jdbcUser).append("" password="").append(jdbcPassword).append(""> ").append(newline); sb.append(" </jdbcConnection>").append(newline); sb.append(" <javaTypeResolver> ").append(newline); sb.append(" <property name="forceBigDecimals" value="false"/> ").append(newline); sb.append(" </javaTypeResolver>").append(newline); sb.append(" <javaModelGenerator targetPackage="").append(entityPkg).append("" targetProject="src"> ").append(newline); sb.append(" <property name="enableSubPackages" value="true"/> ").append(newline); sb.append(" <property name="trimStrings" value="true"/>").append(newline); sb.append(" </javaModelGenerator> ").append(newline); sb.append(" <sqlMapGenerator targetPackage="").append(mappingPkg).append("" targetProject="src">").append(newline); sb.append(" <property name="enableSubPackages" value="true"/>").append(newline); sb.append(" </sqlMapGenerator>").append(newline); sb.append(" <javaClientGenerator type="XMLMAPPER" targetPackage="").append(daoPkg).append("" targetProject="src"> ").append(newline); sb.append(" <property name="enableSubPackages" value="true"/> ").append(newline); sb.append(" </javaClientGenerator> ").append(newline); sb.append(genDbTable()).append(newline); sb.append(" </context>").append(newline); sb.append("</generatorConfiguration>").append(newline); return sb.toString(); } public static String genDbTable() { StringBuilder sb = new StringBuilder(); Connection conn = null; Statement stmt; //创建声明 try { Class.forName(jdbcDriver); conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); stmt = conn.createStatement(); ResultSet res = stmt.executeQuery("select * from user_tab_comments"); while(res.next()) { String tablename = res.getString(1); String tabletype = res.getString(2); if(tabletype.equals("TABLE")&&tablename.indexOf(tableKey)!=-1) { sb.append("<table tableName=""); sb.append(tablename); sb.append("" domainObjectName=""); sb.append(StringUtils.toCapitalizeCamelCase(tablename)); sb.append("" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>").append(newline); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return sb.toString(); } public static void main(String[] args) { System.out.println(genConfig()); } }
3/ 使用Java 代码 遍历 MYSQL,生成可用的配置
package com.panie.common.utils; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MybatisConfigAutoGen { public static String jdbcDriver = "com.mysql.jdbc.Driver"; public static String jdbcUrl = "jdbc:mysql://localhost:3306/blogdb?useUnicode=true&characterEncoding=utf-8"; public static String jdbcUser = "root"; public static String jdbcPassword = "123456"; public static String newline = " "; public static String tableKey = "sys_"; public static String driverJar = "mysql-connector-java-5.1.38.jar";//所使用驱动包 public static String entityPkg = "com.panie.modules.sys.entity"; public static String mappingPkg = "mappings.com.panie.sys.configure"; public static String daoPkg = "com.panie.modules.sys.dao"; public static String genConfig() { StringBuilder sb = new StringBuilder(); sb.append("<?xml version="1.0" encoding="UTF-8"?>").append(newline); sb.append("<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" ").append(newline); sb.append(""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> ").append(newline); sb.append("<generatorConfiguration>").append(newline); sb.append(" <classPathEntry location="").append(driverJar).append(""/> ").append(newline); sb.append(" <context id="DB2Tables" targetRuntime="MyBatis3">").append(newline); sb.append(" <commentGenerator>").append(newline); sb.append(" <property name="suppressDate" value="true"/>").append(newline); sb.append(" <property name="suppressAllComments" value="true"/> ").append(newline); sb.append(" </commentGenerator>").append(newline); sb.append(" <jdbcConnection driverClass="").append(jdbcDriver).append("" connectionURL="").append(jdbcUrl).append("" userId="").append(jdbcUser).append("" password="").append(jdbcPassword).append(""> ").append(newline); sb.append(" </jdbcConnection>").append(newline); sb.append(" <javaTypeResolver> ").append(newline); sb.append(" <property name="forceBigDecimals" value="false"/> ").append(newline); sb.append(" </javaTypeResolver>").append(newline); sb.append(" <javaModelGenerator targetPackage="").append(entityPkg).append("" targetProject="src"> ").append(newline); sb.append(" <property name="enableSubPackages" value="true"/> ").append(newline); sb.append(" <property name="trimStrings" value="true"/>").append(newline); sb.append(" </javaModelGenerator> ").append(newline); sb.append(" <sqlMapGenerator targetPackage="").append(mappingPkg).append("" targetProject="src">").append(newline); sb.append(" <property name="enableSubPackages" value="true"/>").append(newline); sb.append(" </sqlMapGenerator>").append(newline); sb.append(" <javaClientGenerator type="XMLMAPPER" targetPackage="").append(daoPkg).append("" targetProject="src"> ").append(newline); sb.append(" <property name="enableSubPackages" value="true"/> ").append(newline); sb.append(" </javaClientGenerator> ").append(newline); sb.append(genDbTable()).append(newline); sb.append(" </context>").append(newline); sb.append("</generatorConfiguration>").append(newline); return sb.toString(); } public static String genDbTable() { StringBuilder sb = new StringBuilder(); Connection conn = null; Statement stmt; //创建声明 try { Class.forName(jdbcDriver); conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); DatabaseMetaData metadata = conn.getMetaData(); ResultSet res = metadata.getTables("blogdb", null, null, null); while(res.next()) { String tablename = res.getString(3); String tabletype = res.getString(4); if(tabletype.equals("TABLE")&&tablename.toLowerCase().indexOf(tableKey)!=-1) { sb.append("<table tableName=""); sb.append(tablename); sb.append("" domainObjectName=""); sb.append(StringUtils.toCapitalizeCamelCase(tablename)); sb.append("" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>").append(newline); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return sb.toString(); } public static void main(String[] args) { System.out.println(genConfig()); } }