摘要:最近做項(xiàng)目,使用的是,為了偷懶,我自然而然的想到了使用來生成數(shù)據(jù)庫表對(duì)應(yīng)的實(shí)體代碼和代碼。
最近做項(xiàng)目,ORM 使用的是 MyBatis,為了偷懶,我自然而然的想到了使用 MyBatis Generator(MBG)來生成數(shù)據(jù)庫表對(duì)應(yīng)的實(shí)體代碼和 Mapper 代碼。于是做了如下的配置(對(duì) MBG 配置不熟悉的同學(xué)可以參考 Mybatis Generator最完整配置詳解):
數(shù)據(jù)庫建庫建表的代碼:
CREATE SCHEMA `db_test` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ; CREATE TABLE `db_test`.`t_user` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT "用戶 ID", `username` VARCHAR(30) NULL COMMENT "用戶名稱", `password` VARCHAR(20) NULL COMMENT "用戶密碼", `birthday` DATE NULL COMMENT "用戶生日", PRIMARY KEY (`id`), UNIQUE INDEX `username_UNIQUE` (`username` ASC) ) COMMENT = "用戶";
開開心心,執(zhí)行命令,開始生成代碼:
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
然后查看生成的 Java 實(shí)體類:
看著這個(gè)注釋,讓我有點(diǎn)糾結(jié)啊 —— 為什么不是數(shù)據(jù)庫中每個(gè)字段對(duì)應(yīng)的注釋呢?查找相關(guān)資料,得知 MBG 生成的是由 org.mybatis.generator.api.CommentGenerator 來控制的。這是一個(gè)接口,MBG 的默認(rèn)實(shí)現(xiàn)類是 org.mybatis.generator.internal.DefaultCommentGenerator。當(dāng)你在 generatorConfig.xml 中配置了 commentGenerator 標(biāo)簽,那么默認(rèn)狀態(tài)下,生成注釋的工作,將由 DefaultCommentGenerator來完成。 所以我們來查看下這個(gè) DefaultCommentGenerator 的源碼:
public class DefaultCommentGenerator implements CommentGenerator { // 屬性,即配置在 commentGenerator 標(biāo)簽之內(nèi)的 Property 標(biāo)簽 private Properties properties; // 是否不生成日期 private boolean suppressDate; // 是否不生成注釋 private boolean suppressAllComments; // 是否添加數(shù)據(jù)庫內(nèi)的注釋 private boolean addRemarkComments; // 日期的格式 private SimpleDateFormat dateFormat; public DefaultCommentGenerator() { super(); properties = new Properties(); suppressDate = false; suppressAllComments = false; addRemarkComments = false; } @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); } } // 其他代碼 ... }
addRemarkComments 這個(gè)屬性,看來就是用來生成數(shù)據(jù)庫注釋用的 —— 好開心,那把它設(shè)置為 true 試試:
...
運(yùn)行命令:
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
數(shù)據(jù)庫注釋倒是拿到了,但是生成的一堆其他信息,看著實(shí)在是太扎眼了。查看源碼,發(fā)現(xiàn)這些內(nèi)容已經(jīng)寫死在 DefaultCommentGenerator 中了,沒有辦法自定義。
自己動(dòng)手豐衣足食,我們?yōu)樯恫蛔约簩憘€(gè)類實(shí)現(xiàn) CommentGenerator 接口,然后自定義自己想要的注釋呢。查看 commentGenerator 的 DTD,發(fā)現(xiàn)正好 commentGenerator 有個(gè) type 屬性,可以用來指定自己的注釋實(shí)現(xiàn)類:
觀察 CommentGenerator 接口,發(fā)現(xiàn)里面的方法非常多,不僅包含了生成 Java 實(shí)體注釋對(duì)應(yīng)的方法,還包括了生成 XML 中注釋的方法。所以我們先寫一個(gè)默認(rèn)的實(shí)現(xiàn)類,實(shí)現(xiàn)CommentGenerator 接口,但不做任何操作 —— 因?yàn)?DefaultCommentGenerator 本文已經(jīng)存在了,為了避免混淆,就叫它EmptyCommentGenerator 吧。然后定義我們自己的注釋類,MySQLCommentGenerator,繼承 EmptyCommentGenerator,重寫我們需要的方法:
public class MySQLCommentGenerator extends EmptyCommentGenerator { private Properties properties; public MySQLCommentGenerator() { properties = new Properties(); } @Override public void addConfigurationProperties(Properties properties) { // 獲取自定義的 properties this.properties.putAll(properties); } @Override public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { String author = properties.getProperty("author"); String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd"); SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat); // 獲取表注釋 String remarks = introspectedTable.getRemarks(); topLevelClass.addJavaDocLine("/**"); topLevelClass.addJavaDocLine(" * " + remarks); topLevelClass.addJavaDocLine(" *"); topLevelClass.addJavaDocLine(" * @author " + author); topLevelClass.addJavaDocLine(" * @date " + dateFormatter.format(new Date())); topLevelClass.addJavaDocLine(" */"); } @Override public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { // 獲取列注釋 String remarks = introspectedColumn.getRemarks(); field.addJavaDocLine("/**"); field.addJavaDocLine(" * " + remarks); field.addJavaDocLine(" */"); } }
因?yàn)槲覀儸F(xiàn)在要使用到我們自己自定義的 CommentGenerator ,所以我們 通過代碼的方式來操作 MBG:
public class Generator { public static void main( String[] args ) throws Exception { Listwarnings = new ArrayList<>(); File configFile = new File("generatorConfig.xml"); 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); } }
然后配置 generatorConfig.xml設(shè)置我們自己的注釋生成器:
... ...
完整的 Maven 項(xiàng)目在 我的 GitHub?,F(xiàn)在,我們運(yùn)行主類 Generator,成功生成了數(shù)據(jù)庫中的注釋:
等等,好像有點(diǎn)不對(duì)勁!
類的注釋怎么沒有了!
想來應(yīng)該是 JDBC 連接 MySQL 的時(shí)候需要添加什么屬性才能獲取表的注釋,上網(wǎng)查詢,發(fā)現(xiàn)是 useInformationSchema,需要將其設(shè)置為 true(看來 MBG 給自己的 DefaultCommentGenerator 開了小灶):
... ...
然后再次運(yùn)行主類 Generator:
成功的生成了類注釋和字段注釋~
我這里并沒有處理注釋是多行文本的情況 —— 留給有興趣的讀者吧~
小項(xiàng)目地址:https://github.com/mizhoux/mbg-comment
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/77265.html
摘要:全自動(dòng)代碼生成器項(xiàng)目地址代碼模版位于下可根據(jù)需要自行修改第一個(gè)模版是根據(jù)編寫的若不適合可以自行修改模版或選擇其他模版配置文件在下創(chuàng)建文件隨意例如可創(chuàng)建多個(gè)配置文件內(nèi)容如下填入數(shù)據(jù)庫配置以及生成代碼的包名模版文件映射用于自定義生成 全自動(dòng)代碼生成器mybatis-generator 項(xiàng)目地址 https://github.com/GitHub-Laziji/mybatis-generat...
摘要:很多項(xiàng)目不寫文檔,即使寫文檔,對(duì)于開發(fā)人員來說也是非常痛苦的。無法保證及時(shí)更新。是基于注解的文檔生成工具。讓文檔的閱讀者享受到等同于手寫文檔的體驗(yàn)。將信息的獲取和生成區(qū)分開?;谠淖⑨專M可能的生成簡(jiǎn)介的文檔。 設(shè)計(jì)初衷 節(jié)約時(shí)間 Java 文檔一直是一個(gè)大問題。 很多項(xiàng)目不寫文檔,即使寫文檔,對(duì)于開發(fā)人員來說也是非常痛苦的。 不寫文檔的缺點(diǎn)自不用多少,手動(dòng)寫文檔的缺點(diǎn)也顯而易見:...
摘要:一的官方資料官方文檔源碼二介紹大致的意思是可以幫助所有版本的和以上版本的生成代碼。其中目前最新的版本可以使用。指定生成一系列對(duì)象的環(huán)境。定義了生成的注釋形式。與生成的實(shí)體相關(guān)。生成接口和類以達(dá)到輕易使用生成的模型和映射文件的目的。 一:MyBatis Generator的官方資料 MyBatis Generator官方文檔github源碼:MyBatis Generator (MBG)...
摘要:每個(gè)微服務(wù)僅關(guān)注于完成一件任務(wù)并很好地完成該任務(wù)。在一個(gè)微服務(wù)的開發(fā)過程中很可能只關(guān)注對(duì)單表的操作。本文將說到在的項(xiàng)目中如何去配置形式和配置類形式和使用以及生成代碼的兩種方式形式和注解形式,在中更推薦去使用注解的形式。 介紹 Mybatis Generator(MBG)是Mybatis的一個(gè)代碼生成工具。MBG解決了對(duì)數(shù)據(jù)庫操作有最大影響的一些CRUD操作,很大程度上提升開發(fā)效率。如果...
摘要:地址簡(jiǎn)單說明這是一個(gè)的腳手架項(xiàng)目,方便老鳥使用,新手學(xué)習(xí)。然后我們?cè)谥屑尤脒@張表里還有很多配置,你可以直接使用我的默認(rèn)配置,往上面添加即可。結(jié)語當(dāng)然我這里很多細(xì)節(jié)沒有講到,僅僅是簡(jiǎn)單的使用了一下,希望各位有心的讀者可以自己動(dòng)手搭建一下。 Github地址 https://github.com/1994/ssm-scaffold.git 簡(jiǎn)單說明 這是一個(gè)Spring4+Mybatis3...
閱讀 2929·2021-11-25 09:43
閱讀 2375·2021-11-24 09:39
閱讀 2765·2021-09-23 11:51
閱讀 1440·2021-09-07 10:11
閱讀 1483·2019-08-27 10:52
閱讀 1964·2019-08-26 12:13
閱讀 3387·2019-08-26 11:57
閱讀 1421·2019-08-26 11:31