效果:html
首先要講idea的模式改爲eclipse的模式,也能夠使用idea的模式就是該快捷鍵的操做。java
而後就是改下mybati自動生成的配置文件了。mysql
<?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> <!-- 參考文檔:中文 http://generator.sturgeon.mopaas.com/afterRunning.html,原文http://www.mybatis.org/generator/ --> <!-- 示例table:create table mybatis_user(user_name CHAR(15) not null,user_password CHAR(15) not null,user_email VARCHAR(20) not null unique,user_birthday DATETIME,user_age INT,user_salary DECIMAL(10,2),primary key(user_name))engine=innodb default charset=utf8; --> <!-- 依賴包直接定義在mybatis-generator-maven-plugin的dependencies中 --> <!-- <classPathEntry location="./lib/mysql-connector-java-5.1.39-bin.jar" /> <classPathEntry location="./lib/dolphin-mybatis-generator-1.1.0.jar" / > --> <context id="default" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8" /> <!-- 替換Example爲Criteria --> <!--該插件經過重命名由MBG生成的生成的示例類來演示初始化方法的用法。--> <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$" /> <property name="replaceString" value="Criteria" /> </plugin> <!-- 表格對應Java bean類型實現java.io.Serializable接口,添加默認的serialVersionUID --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"> <property name="suppressJavaInterface " value="true" /> </plugin> <!-- 表格對應Java bean類型增長toString方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 增長分頁支持 --> <plugin type="com.freetmp.mbg.plugin.page.MySqlPaginationPlugin" /> <!-- 生成分庫分表的掃表語句,項目須要依賴lo-widgets-datasource --> <plugin type="cn.openlo.mbg.plugins.ShardingExamplePlugin" /> <!-- jdbc鏈接定義 --> <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://101.201.144.63:3306/db_lotest?useUnicode=true&characterEncoding=UTF-8" userId="louser" password="lolo10" /> --> <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver"--> <!--connectionURL="jdbc:mysql://172.16.50.138:3306/vfc?useUnicode=true&characterEncoding=UTF-8"--> <!--userId="vfc_rw" password="vfc&_dange9" />--> <commentGenerator type="cn.openlo.dao.CustomComment"> <property name="suppressDate" value="false"/> <!--<property name="suppressAllComments" value="true"/>--> <property name="addRemarkComments" value="true"></property> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/vfc_demo?useUnicode=true&characterEncoding=UTF-8" userId="root" password="root"> <property name="useInformationSchema" value="true"></property> </jdbcConnection> <!-- 強制數據庫小數類型爲java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="true" /> </javaTypeResolver> <!-- 表格對應Java bean類型生成,結果爲$TABLE_NAME$.java和$TABLE_NAME$Criteria.java --> <javaModelGenerator targetPackage="cn.openlo.tutorial.mybatis.dto" targetProject="./src/main/java/"> <property name="constructorBased" value="false" /> <!-- 指定Java bean公共父類 --> <property name="rootClass" value="cn.openlo.gear.dataobject.PersistentDTOSupport" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sql語句生成,結果爲$TABLE_NAME$Mapper.xml --> <sqlMapGenerator targetPackage="cn.openlo.tutorial.mybatis.sqlmap" targetProject="./src/main/java/" /> <!-- 應用客戶端生成,結果爲$TABLE_NAME$Mapper.java --> <javaClientGenerator targetPackage="cn.openlo.tutorial.mybatis.client" targetProject="./src/main/java/" type="XMLMAPPER"> <!-- 指定客戶端公共接口 --> <property name="rootInterface" value="cn.openlo.dataobject.DAO" /> </javaClientGenerator> <!-- 如下定義須要生成的表 --> <table schema="db_lotest" tableName="business_license_info" /> <table schema="db_lotest" tableName="business_scope" /> <table schema="db_lotest" tableName="document" /> <table schema="db_lotest" tableName="enterprise_product_info" /> <table schema="db_lotest" tableName="legal_person_bank_account" /> <table schema="db_lotest" tableName="listed_company_info" /> <table schema="db_lotest" tableName="management_info" /> <table schema="db_lotest" tableName="other_perple_info" /> <table schema="db_lotest" tableName="partnership" /> <table schema="db_lotest" tableName="private_owners_info" /> <table schema="db_lotest" tableName="shareholder_info" /> <table schema="db_lotest" tableName="tax_info" /> <table schema="db_lotest" tableName="vfc_account_info" /> <table schema="db_lotest" tableName="vfc_guarantee_info" /> <table schema="db_lotest" tableName="vfc_legal_person_base_info" /> <table schema="db_lotest" tableName="vfc_nature_person_base_info" /> <table schema="db_lotest" tableName="vfc_np_detail_info" /> <table schema="db_lotest" tableName="vfc_np_other_info" /> <table schema="db_lotest" tableName="work_detaild_info" /> <table schema="db_lotest" tableName="work_info" /> <table schema="db_lotest" tableName="user" /> </context> </generatorConfiguration>
package cn.openlo.dao; import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.config.MergeConstants; import org.mybatis.generator.config.PropertyRegistry; import org.mybatis.generator.internal.DefaultCommentGenerator; import org.mybatis.generator.internal.util.StringUtility; import javax.xml.bind.DatatypeConverter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; import java.util.Set; import static org.mybatis.generator.internal.util.StringUtility.isTrue; /** * * */ public class CustomComment implements CommentGenerator { private Properties properties; private boolean suppressDate; private boolean suppressAllComments; /** If suppressAllComments is true, this option is ignored. */ private boolean addRemarkComments; private SimpleDateFormat dateFormat; public CustomComment() { super(); properties = new Properties(); suppressDate = false; suppressAllComments = false; addRemarkComments = false; } @Override public void addJavaFileComment(CompilationUnit compilationUnit) { // add no file level comments by default } /** * Adds a suitable comment to warn users that the element was generated, and when it was generated. * * @param xmlElement * the xml element */ @Override public void addComment(XmlElement xmlElement) { if (suppressAllComments) { return; } xmlElement.addElement(new TextElement("<!--")); //$NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" WARNING - "); //$NON-NLS-1$ sb.append(MergeConstants.NEW_ELEMENT_TAG); xmlElement.addElement(new TextElement(sb.toString())); xmlElement .addElement(new TextElement( " This element is automatically generated by MyBatis Generator, do not modify.")); //$NON-NLS-1$ String s = getDateString(); if (s != null) { sb.setLength(0); sb.append(" This element was generated on "); //$NON-NLS-1$ sb.append(s); sb.append('.'); xmlElement.addElement(new TextElement(sb.toString())); } xmlElement.addElement(new TextElement("-->")); //$NON-NLS-1$ } @Override public void addRootComment(XmlElement rootElement) { // add no document level comments by default } @Override public void addConfigurationProperties(Properties properties) { this.properties.putAll(properties); suppressDate = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE)); suppressAllComments = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS)); addRemarkComments = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS)); String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT); if (StringUtility.stringHasValue(dateFormatString)) { dateFormat = new SimpleDateFormat(dateFormatString); } } /** * This method adds the custom javadoc tag for. You may do nothing if you do not wish to include the Javadoc tag - * however, if you do not include the Javadoc tag then the Java merge capability of the eclipse plugin will break. * * @param javaElement * the java element * @param markAsDoNotDelete * the mark as do not delete */ protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) { // javaElement.addJavaDocLine(" *"); //$NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" * "); //$NON-NLS-1$ sb.append(MergeConstants.NEW_ELEMENT_TAG); if (markAsDoNotDelete) { sb.append(" do_not_delete_during_merge"); //$NON-NLS-1$ } // String s = getDateString(); // if (s != null) { // sb.append(' '); // sb.append(s); // } // javaElement.addJavaDocLine(sb.toString()); } /** * Returns a formated date string to include in the Javadoc tag * and XML comments. You may return null if you do not want the date in * these documentation elements. * * @return a string representing the current timestamp, or null */ protected String getDateString() { if (suppressDate) { return null; } else if (dateFormat != null) { return dateFormat.format(new Date()); } else { return new Date().toString(); } } @Override public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerClass.addJavaDocLine("/**"); //$NON-NLS-1$ innerClass .addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); innerClass.addJavaDocLine(" * This database table remark "+introspectedTable.getRemarks()); innerClass.addJavaDocLine(sb.toString()); addJavadocTag(innerClass, false); innerClass.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerClass.addJavaDocLine("/**"); //$NON-NLS-1$ innerClass .addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); innerClass.addJavaDocLine(" * This database table remark "+introspectedTable.getRemarks()); innerClass.addJavaDocLine(sb.toString()); addJavadocTag(innerClass, markAsDoNotDelete); innerClass.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (suppressAllComments || !addRemarkComments) { return; } topLevelClass.addJavaDocLine("/**"); //$NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" * database table:"); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); topLevelClass.addJavaDocLine(sb.toString()); String remarks = introspectedTable.getRemarks(); if (addRemarkComments && StringUtility.stringHasValue(remarks)) { topLevelClass.addJavaDocLine(" * Database Table Remarks:"+remarks); //$NON-NLS-1$ // String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$ // for (String remarkLine : remarkLines) { // topLevelClass.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$ // } } // topLevelClass.addJavaDocLine(" *"); //$NON-NLS-1$ // topLevelClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$ // addJavadocTag(topLevelClass, true); topLevelClass.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$ innerEnum .addJavaDocLine(" * This enum was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This enum corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); innerEnum.addJavaDocLine(sb.toString()); addJavadocTag(innerEnum, false); innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } field.addJavaDocLine("/**"); //$NON-NLS-1$ String remarks = introspectedColumn.getRemarks(); if (addRemarkComments && StringUtility.stringHasValue(remarks)) { field.addJavaDocLine(" * Database Column Remarks:"+remarks); //$NON-NLS-1$ // String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$ // for (String remarkLine : remarkLines) { // field.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$ // } } // field.addJavaDocLine(" *"); //$NON-NLS-1$ // field.addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" * database column: "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); field.addJavaDocLine(sb.toString()); addJavadocTag(field, false); field.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addFieldComment(Field field, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); field.addJavaDocLine("/**"); //$NON-NLS-1$ field .addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This field corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); field.addJavaDocLine(sb.toString()); addJavadocTag(field, false); field.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); //$NON-NLS-1$ method .addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This method corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); //$NON-NLS-1$ // method .addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This method returns the value of the database column "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName());//$NON-NLS-1$ method.addJavaDocLine(" * 獲取"+introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); // method.addJavaDocLine(" *"); //$NON-NLS-1$ sb.setLength(0); sb.append(" * @return the value of "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); //$NON-NLS-1$ // method.addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This method sets the value of the database column "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(" * 設置"+introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); // method.addJavaDocLine(" *"); //$NON-NLS-1$ Parameter parm = method.getParameters().get(0); sb.setLength(0); sb.append(" * @param "); //$NON-NLS-1$ sb.append(parm.getName()); sb.append(" the value for "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); //$NON-NLS-1$ } @Override public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$ String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$ method.addAnnotation(getGeneratedAnnotation(comment)); } @Override public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) { imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$ String comment = "Source field: " //$NON-NLS-1$ + introspectedTable.getFullyQualifiedTable().toString() + "." //$NON-NLS-1$ + introspectedColumn.getActualColumnName(); method.addAnnotation(getGeneratedAnnotation(comment)); } @Override public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$ String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$ field.addAnnotation(getGeneratedAnnotation(comment)); } @Override public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) { imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$ String comment = "Source field: " //$NON-NLS-1$ + introspectedTable.getFullyQualifiedTable().toString() + "." //$NON-NLS-1$ + introspectedColumn.getActualColumnName(); field.addAnnotation(getGeneratedAnnotation(comment)); } @Override public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) { imports.add(new FullyQualifiedJavaType("javax.annotation.Generated")); //$NON-NLS-1$ String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString(); //$NON-NLS-1$ innerClass.addAnnotation(getGeneratedAnnotation(comment)); } private String getGeneratedAnnotation(String comment) { StringBuilder buffer = new StringBuilder(); buffer.append("@Generated("); //$NON-NLS-1$ if (suppressAllComments) { buffer.append('\"'); } else { buffer.append("value=\""); //$NON-NLS-1$ } buffer.append(MyBatisGenerator.class.getName()); buffer.append('\"'); if (!suppressDate && !suppressAllComments) { buffer.append(", date=\""); //$NON-NLS-1$ buffer.append(DatatypeConverter.printDateTime(Calendar.getInstance())); buffer.append('\"'); } if (!suppressAllComments) { buffer.append(", comments=\""); //$NON-NLS-1$ buffer.append(comment); buffer.append('\"'); } buffer.append(')'); return buffer.toString(); } }
package cn.openlo.dao; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.ProgressCallback; import org.mybatis.generator.api.VerboseProgressCallback; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.util.ArrayList; import java.util.List; /** * < */ public class GeneratorTest { // private final Logger logger = LoggerFactory.getLogger(this.getClass()); public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<>(); boolean overwrite = true; File configFile = new File("D:\\idea_workspace\\openlo-gear\\src\\main\\resources\\mybatisGen\\generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); ProgressCallback progressCallback = new VerboseProgressCallback(); myBatisGenerator.generate(progressCallback); } }
快捷鍵是: Shift +Ctrl +空格sql