/** * 设置用户配置的参数 */ @Override public void addConfigurationProperties(Properties properties) { super.addConfigurationProperties(properties); this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments")); }
public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
@Override public boolean validate(List<String> warnings) { boolean right = true; if (!StringUtility.stringHasValue(fileName)) { warnings.add("没有配置 \"fileName\" 文件名模板,因此不会生成任何额外代码!"); right = false; if (!StringUtility.stringHasValue(templatePath)) { warnings.add("没有配置 \"templatePath\" 模板路径,因此不会生成任何额外代码!"); right = false; URL resourceUrl = null; try { resourceUrl = ObjectFactory.getResource(templatePath); } catch (Exception e) { warnings.add("本地加载\"templatePath\" 模板路径失败,尝试 URL 方式获取!"); if (!StringUtility.stringHasValue(templateFormatterClass)) { templateFormatterClass = DEFAULT_TEMPLATEFORMATTER; warnings.add("没有配置 \"templateFormatterClass\" 模板处理器,使用默认的处理器!"); if (!StringUtility.stringHasValue(targetProject)) { errorCount++; warnings.add("没有配置 \"targetProject\" 路径!"); if (!StringUtility.stringHasValue(targetPackage)) { errorCount++; warnings.add("没有配置 \"targetPackage\" 路径!");
@Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { super.addFieldComment(field, introspectedTable, introspectedColumn); if (introspectedColumn.getRemarks() != null && !"".equals(introspectedColumn.getRemarks())) { field.addJavaDocLine("/**"); field.addJavaDocLine(" * " + introspectedColumn.getRemarks()); addJavadocTag(field, false); field.addJavaDocLine(" */"); } }
public CommentGenerator getCommentGenerator() { if (commentGenerator == null) { commentGenerator = ObjectFactory.createCommentGenerator(this); } return commentGenerator; }
public JavaFormatter getJavaFormatter() { if (javaFormatter == null) { javaFormatter = ObjectFactory.createJavaFormatter(this); } return javaFormatter; }
public XmlFormatter getXmlFormatter() { if (xmlFormatter == null) { xmlFormatter = ObjectFactory.createXmlFormatter(this); } return xmlFormatter; }
@Override public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { // innerClass.addJavaDocLine("/**"); //$NON-NLS-1$ // innerClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable()); // innerClass.addJavaDocLine(" * User: zhangqh6"); //$NON-NLS-1$ // innerClass.addJavaDocLine(" */"); //$NON-NLS-1$ super.addClassComment(innerClass, introspectedTable); }
ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(true); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null);
@Override public boolean validate(List<String> warnings) { boolean right = true; if (!StringUtility.stringHasValue(fileName)) { warnings.add("没有配置 \"fileName\" 文件名模板,因此不会生成任何额外代码!"); right = false; if (!StringUtility.stringHasValue(templatePath)) { warnings.add("没有配置 \"templatePath\" 模板路径,因此不会生成任何额外代码!"); right = false; URL resourceUrl = null; try { resourceUrl = ObjectFactory.getResource(templatePath); } catch (Exception e) { warnings.add("本地加载\"templatePath\" 模板路径失败,尝试 URL 方式获取!"); if (!StringUtility.stringHasValue(templateFormatterClass)) { templateFormatterClass = DEFAULT_TEMPLATEFORMATTER; warnings.add("没有配置 \"templateFormatterClass\" 模板处理器,使用默认的处理器!"); if (!StringUtility.stringHasValue(targetProject)) { errorCount++; warnings.add("没有配置 \"targetProject\" 路径!"); if (!StringUtility.stringHasValue(targetPackage)) { errorCount++; warnings.add("没有配置 \"targetPackage\" 路径!");
/** * 设置用户配置的参数 */ public void addConfigurationProperties(Properties properties) { //先调用父类方法保证父类方法可以正常使用 super.addConfigurationProperties(properties); //获取 suppressAllComments 参数值 suppressAllComments = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS)); //获取 addRemarkComments 参数值 addRemarkComments = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS)); }
@Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { super.addFieldComment(field, introspectedTable, introspectedColumn); if (introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().equals("")) { field.addJavaDocLine("/**"); field.addJavaDocLine(" * " + introspectedColumn.getRemarks()); addJavadocTag(field, false); field.addJavaDocLine(" */"); } }
public CommentGenerator getCommentGenerator() { if (commentGenerator == null) { commentGenerator = ObjectFactory.createCommentGenerator(this); } return commentGenerator; }
public JavaFormatter getJavaFormatter() { if (javaFormatter == null) { javaFormatter = ObjectFactory.createJavaFormatter(this); } return javaFormatter; }
public XmlFormatter getXmlFormatter() { if (xmlFormatter == null) { xmlFormatter = ObjectFactory.createXmlFormatter(this); } return xmlFormatter; }
public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generator/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for(String warning : warnings){ System.out.println(warning); } } }
@Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { super.addFieldComment(field, introspectedTable, introspectedColumn); if (introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().equals("")) { field.addJavaDocLine("/**"); field.addJavaDocLine(" * " + introspectedColumn.getRemarks()); addJavadocTag(field, false); field.addJavaDocLine(" */"); } }
public CommentGenerator getCommentGenerator() { if (commentGenerator == null) { commentGenerator = ObjectFactory.createCommentGenerator(this); } return commentGenerator; }
/** * @param arg * @throws Throwable */ public static void main(String[] arg) throws Throwable { List<String> warnings = new ArrayList<>(); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(MybatisGenerator.class.getResourceAsStream("/generator/mybatis/generatorConfig.xml")); DefaultShellCallback shellCallback = new DefaultShellCallback(true); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings); myBatisGenerator.generate(null); } }