1.需求
KenhoMBGPlus期待的功能是通过传入参数可以指定数据库进行模板的新增。
2.问题分析
在开源代码中生成的表和相关信息是已经写在配置文件中,因此在工具中我们要使配置是可以通过用户自定义的,其次就是自定义的配置如何和配置文件相绑定。
3.解决问题
3.1 配置文件自定义
关于配置文件自定义,在前一篇博客KenhoMBGPlus SpringBoot模板生成工具(一)-构建项目中已经把源码修改为Spring Boot的启动形式,因此我们可以通过前端页面进行传参。查看源码我们能发现我们可以发现在ShellRunner中下面的代码段对我们传入的配置文件进行解析。
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configurationFile);
因此在分析之后我们对config部分参数进行重设置
TableRelateDetail tableinfo = (TableRelateDetail) ThreadLocalUtil.get("tableinfo");
JDBCConnectionConfiguration jdbcConnectionConfiguration = config.getContexts().get(0).getJdbcConnectionConfiguration();
/**
* 重新设置密码和账号
*/
jdbcConnectionConfiguration.setPassword(tableinfo.getPassword());
jdbcConnectionConfiguration.setUserId(tableinfo.getUsername());
jdbcConnectionConfiguration.setConnectionURL("jdbc:mysql://" + tableinfo.getDatabaseurl() + "?characterEncoding=utf8&useSSL=false");
jdbcConnectionConfiguration.setDriverClass("com.mysql.jdbc.Driver");
/**
* 重新设置服务包名
*/
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = config.getContexts().get(0).getSqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetPackage(tableinfo.getServerPackage());
/**
* 重新设置对象包名
*/
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = config.getContexts().get(0).getJavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetPackage(tableinfo.getObjectPackage());
/**
* 设置表名
*/
TableConfiguration tableConfiguration = config.getContexts().get(0).getTableConfigurations().get(0);
tableConfiguration.setTableName(tableinfo.getTableName());
tableConfiguration.setDomainObjectName(tableinfo.getObjectName());
实现前端配置指定生成对象
下一篇博文我会通过源码构成文件的代码结合FreeMark实现我们的最终目标。