mybatis根据数据库表结构自动生成实体类,dao,mapper

mybatis根据数据库表结构自动生成实体类,dao,mapper

2023年7月24日发(作者:)

mybatis根据数据库表结构⾃动⽣成实体类,dao,mapper⾸先, pom需要引⼊ mysql mysql-connector-java 5.1.39 tor mybatis-generator-core 1.3.3 然后是mybatis⽣成器import tGenerator;import pectedColumn;import pectedTable;import .*;import ment;import onstants;import tyRegistry;import DateFormat;import ;import ties;import static ;/** * Mybatis 注释⽣成器 */public class MyCommentGenerator implements CommentGenerator { private Properties properties; private Properties systemPro; private boolean suppressDate; private boolean suppressAllComments; private String currentDateStr; public MyCommentGenerator() { super(); properties = new Properties(); systemPro = perties(); suppressDate = false; suppressAllComments = false; currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()); } public void addJavaFileComment(CompilationUnit compilationUnit) { // add no file level comments by default return; } /** * Adds a suitable comment to warn users that the element was generated, and * when it was generated. */ public void addComment(XmlElement xmlElement) { return; } public void addRootComment(XmlElement rootElement) { // add no document level comments by default return; } public void addConfigurationProperties(Properties properties) { (properties); suppressDate = isTrue(perty(T_GENERATOR_SUPPRESS_DATE)); suppressAllComments = isTrue(perty(T_GENERATOR_SUPPRESS_ALL_COMMENTS)); } /** * 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 */ protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) { aDocLine(" *"); StringBuilder sb = new StringBuilder(); (" * "); (_ELEMENT_TAG); if (markAsDoNotDelete) { (" do_not_delete_during_merge"); } String s = getDateString(); if (s != null) { (' '); (s); } aDocLine(ng()); } /** * This method 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() { String result = null; if (!suppressDate) { result = currentDateStr; } return result; } public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); aDocLine("/**"); (" * "); (lyQualifiedTable()); (" "); (getDateString()); aDocLine(ng()); aDocLine(" */"); } public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); aDocLine("/**");

(" * "); (lyQualifiedTable()); aDocLine(ng()); aDocLine(" */"); } public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); aDocLine("/**"); (" * "); (arks()); aDocLine(ng()); // addJavadocTag(field, false); aDocLine(" */"); } public void addFieldComment(Field field, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); aDocLine("/**"); (" * "); (lyQualifiedTable()); aDocLine(ng()); aDocLine(" */"); } public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { } public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; }

} public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } aDocLine("/**"); StringBuilder sb = new StringBuilder(); (" * "); (arks()); aDocLine(ng()); gth(0); (" * @return "); (ualColumnName()); (" "); (arks()); aDocLine(ng()); // addJavadocTag(method, false); aDocLine(" */"); } public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } aDocLine("/**"); StringBuilder sb = new StringBuilder(); (" * "); (arks()); aDocLine(ng()); Parameter parm = ameters().get(0); gth(0); (" * @param "); (e()); (" "); (arks()); aDocLine(ng()); // addJavadocTag(method, false); aDocLine(" */"); } public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); aDocLine("/**"); (" * "); (lyQualifiedTable()); aDocLine(ng()); gth(0); (" * @author "); (perty("")); (" "); (currentDateStr); // addJavadocTag(innerClass, markAsDoNotDelete); aDocLine(" */"); }}

然后是 ⽂件

最后是要执⾏的的类,执⾏这个类, 就能⽣成相应的⽂件了, 执⾏完可能需要更新下项⽬才能看出效果import sGenerator;import uration;import urationParser;import dConfigurationException;import serException;import tShellCallback;import ;import ption;import eption;import ist;import ;public class MyBatisGeneratorTest { public static void main(String[] args) { try { List warnings = new ArrayList(); boolean overwrite = true;

File configFile = new File("E:");

ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = onfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); te(null); } catch (SQLException e) { tackTrace(); } catch (IOException e) { tackTrace(); } catch (InterruptedException e) { tackTrace(); } catch (InvalidConfigurationException e) { tackTrace(); } catch (XMLParserException e) { tackTrace(); } }}

发布者:admin,转转请注明出处:http://www.yc00.com/news/1690192989a312378.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信