上篇文章有区别地分析了generator配置文件,但是不知读者发现没有,网上几乎所有文章贴出的配置信息中只包含一个table元素,即使是官网也是如此。但是在实际开发过程中,一个项目基本不可能只有一个数据表,那项目如果涉及几十张数据表或者上百张数据表,配置文件怎么写?手写?Oh,NO!本文主要介绍一个自动生成配置文件的方法,直接贴代码,直观!拿去吧!

/**
 * 用于生成自动代码配置文件
 * User: Administrator
 * Date: 14-9-10
 * Time: 上午10:27
 * To change this template use File | Settings | File Templates.
 */
public class GeneratorConfigFile {

/**
 * @param args
 */
//驱动程序就是之前在classpath中配置的JDBC的驱动程序的JAR 包中
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
//连接地址是由各个数据库生产商单独提供的,所以需要单独记住
public static final String DBURL = "jdbc:mysql://localhost:3306/test";
//连接数据库的用户名
public static final String DBUSER = "dbuser";
//连接数据库的密码
public static final String DBPASS = "dbpass";

public static final String LINE_SEPARATOR = System.getProperty("line.separator");

public static void main(String[] args) throws Exception{

    StringBuffer content = new StringBuffer(1024);

    //xml文件编码和版本信息
    content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    //文件命名空间
    content.append("<!DOCTYPE generatorConfiguration PUBLIC").append(LINE_SEPARATOR);
    content.append("        \"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN\"").append(LINE_SEPARATOR);
    content.append("        \"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd\">").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);

    //配置文件主标签generatorConfiguration
    content.append("<generatorConfiguration>").append(LINE_SEPARATOR);
    //加载数据库驱动包
    content.append("    <!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->").append(LINE_SEPARATOR);
    content.append("    <classPathEntry location=\"E:\\repository\\mysql\\mysql-connector-java\\5.1.9\\mysql-connector-java-5.1.9.jar\"/>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    //context标签配置
    content.append("    <context id=\"DB2Tables\" targetRuntime=\"MyBatis3\">").append(LINE_SEPARATOR);
    //注释设置
    content.append("        <commentGenerator>").append(LINE_SEPARATOR);
    content.append("            <!-- 是否去除自动生成的注释 true:是; false:否 -->").append(LINE_SEPARATOR);
    content.append("            <property name=\"suppressAllComments\" value=\"true\"/>").append(LINE_SEPARATOR);
    content.append("            <property name=\"suppressDate\" value=\"true\"/>").append(LINE_SEPARATOR);
    content.append("        </commentGenerator>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    //数据连接信息配置
    content.append("        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->").append(LINE_SEPARATOR);
    content.append("        <jdbcConnection driverClass=\"com.mysql.jdbc.Driver\" connectionURL=\"jdbc:mysql://localhost:3306/test\"").append(LINE_SEPARATOR);
    content.append("                        userId=\"root\" password=\"123456\">").append(LINE_SEPARATOR);
    content.append("        </jdbcConnection>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    //数值转换配置
    content.append("        <!--  默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer").append(LINE_SEPARATOR);
    content.append("                 true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal").append(LINE_SEPARATOR);
    content.append("             -->").append(LINE_SEPARATOR);
    content.append("        <javaTypeResolver>").append(LINE_SEPARATOR);
    content.append("            <property name=\"forceBigDecimals\" value=\"false\"/>").append(LINE_SEPARATOR);
    content.append("        </javaTypeResolver>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    //代码生成位置配置
    content.append("        <javaModelGenerator targetPackage=\"com.sean.model\" targetProject=\"E:\\gitwork\\code-generator\\src\\main\\java\">").append(LINE_SEPARATOR);
    content.append("            <!-- enableSubPackages:是否让schema作为包的后缀 -->").append(LINE_SEPARATOR);
    content.append("            <property name=\"enableSubPackages\" value=\"false\"/>").append(LINE_SEPARATOR);
    content.append("            <!-- 从数据库返回的值被清理前后的空格  -->").append(LINE_SEPARATOR);
    content.append("            <property name=\"trimStrings\" value=\"true\"/>").append(LINE_SEPARATOR);
    content.append("        </javaModelGenerator>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    content.append("        <sqlMapGenerator targetPackage=\"sqlmap\" targetProject=\"E:\\gitwork\\code-generator\\src\\main\\resources\">").append(LINE_SEPARATOR);
    content.append("            <property name=\"enableSubPackages\" value=\"false\"/>").append(LINE_SEPARATOR);
    content.append("        </sqlMapGenerator>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);
    content.append("        <javaClientGenerator type=\"XMLMAPPER\" targetPackage=\"com.sean.mapper\" targetProject=\"E:\\gitwork\\code-generator\\src\\main\\java\">").append(LINE_SEPARATOR);
    content.append("            <property name=\"enableSubPackages\" value=\"false\"/>").append(LINE_SEPARATOR);
    content.append("        </javaClientGenerator>").append(LINE_SEPARATOR);
    content.append(LINE_SEPARATOR);

    Connection con = null; //表示数据库的连接对象
    Statement stmt = null;
    ResultSet result = null; //表示接收数据库的查询结果
    Class.forName(DBDRIVER); //1、使用CLASS 类加载驱动程序
    con = DriverManager.getConnection(DBURL, DBUSER, DBPASS); //2、连接数据库
    stmt = con.createStatement(); //3、Statement 接口需要通过Connection 接口进行实例化操作
    result = stmt.executeQuery("show tables");

    String tableName = "";
    String domainObjectName = "";
    while(result.next()) {
        tableName = result.getString(1);
        domainObjectName = CamelCaseUtils.toCapitalizeCamelCase(tableName);
//            content.append("        <table tableName=\"" + tableName + "\" domainObjectName=\"" + domainObjectName + "\"/>").append(LINE_SEPARATOR);
        content.append("        <table tableName=\"" + tableName + "\" domainObjectName=\"" + domainObjectName + "\" enableCountByExample=\"false\"").append(LINE_SEPARATOR);
        content.append("               enableUpdateByExample=\"false\" enableDeleteByExample=\"false\" enableSelectByExample=\"false\"").append(LINE_SEPARATOR);
        content.append("               selectByExampleQueryId=\"false\"/>").append(LINE_SEPARATOR);
        System.out.println("数据表" + tableName + "配置成功!");
    }

    result.close();
    con.close(); // 4、关闭数据库

    //关闭context标签
    content.append("    </context>").append(LINE_SEPARATOR);
    //关闭主标签generatorConfiguration
    content.append("</generatorConfiguration>");

    //生成xml配置文件
    FileWriter writer = new FileWriter("src/main/resources/generatorConfig.xml");
    writer.write(content.toString());
    writer.flush();
    writer.close();

}
}
文章作者:admin
本文链接:http://javatech.wang/index.php/archives/30/
版本所有 ©转载时必须以链接形式注明作者和原始出处