作者:王建乐1前言在日常的开发工作中,我们经常会使用MybatisGenerator根据表结构生成相应的实体类和Mapper文件。但是在MybatisGenerator默认生成的代码中,注释并不是我们想要的,所以一般在Generator的配置文件中,都会设置不自动生成注释。问题是代码自动生成后,我们还得自己给类文件添加注释。如果生成的class少一点还好,但是如果生成的class文件很多,自己加注释就很麻烦了。通过重写MybatisGenerator的CommentGenerator接口,可以轻松生成你想要的评论,减少重复性工作。2使用Java方式执行MybatisGenerator2.1IDEA中新建Maven项目pom.xml中引入jar包4.0.0org.exampleMyGenerator<版本>1.0-SNAPSHOT88mysqlmysql-connector-java<版本>8.0.16</version>org.mybatis.generatormybatis-generator-core1.3.72.2创建generatorConfig.xml随便找个目录放到src/main/resources目录下2.3创建main方法并运行GeneratorpublicclassGenerator{publicstaticvoidmain(String[]args)throwsException{Listwarnings=newArrayList<>(2);ConfigurationParsercp=newConfigurationParser(警告);FileconfigFile=newFile("src/main/resources/generatorConfig.xml");配置config=cp.parseConfiguration(configFile);DefaultShellCallback回调=newDefaultShellCallback(true);MyBatisGenerator=newMyBatisGenerator(config,callback,warnings);myBatisGenerator.generate(null);}}运行main方法生成默认评论如下,不是我们想要的评论,所以一般配置为不生成评论:2.4实现CommentGenerator接口重写以下方法,自定义评论publicclassMySQLCommentGeneratorimplementsCommentGenerator{privatefinalProperties属性;publicMySQLCommentGenerator(){properties=newProperties();}@OverridepublicvoidaddConfigurationProperties(Propertiesproprties){//获取自定义属性this.properties.putAll(properties);}/***重写添加到实体类的注释*/@OverridepublicvoidaddModelClassComment(TopLevelClasstopLevelClass,IntrospectedTableintrospectedTable){Stringauthor=properties.getProperty("author");StringdateFormat=properties.getProperty("dateFormat","yyyy-MM-dd");SimpleDateFormatdateFormatter=newSimpleDateFormat(dateFormat);//获取表注释Stringremarks=introspectedTable.getRemarks();topLevelClass.addJavaDocLine("/**");topLevelClass.addJavaDocLine("*"+备注);topLevelClass.addJavaDocLine("*");topLevelClass.addJavaDocLine("*@author"+author);topLevelClass.addJavaDocLine("*@date"+dateFormatter.format(newDate()));topLevelClass.addJavaDocLine("*/");}/***重写为真实的添加到正文类字段的注释*/@OverridepublicvoidaddFieldComment(Fieldfield,IntrospectedTableintrospectedTable,IntrospectedColumnintrospectedColumn){//获取列注释Stringremarks=introspectedColumn.getRemarks();field.addJavaDocLine("/**");field.addJavaDocLine("*"+备注);field.addJavaDocLine("*/");}/***重写添加到实体类get方法的注释*/@OverridepublicvoidaddGetterComment(Methodmethod,IntrospectedTableintrospectedTable,IntrospectedColumnintrospectedColumn){//获取表注释Stringremarks=introspectedColumn.getRemarks();method.addJavaDocLine("/**");method.addJavaDocLine("*"+method.getName());method.addJavaDocLine("*/");}2.5修改generatorConfig.xml配置修改generatorConfig.xml文件中的commentGenerator如下,type属性选择自己的实现类运行main方法,生成评论如下:3使用Maven执行Mybatis在GeneratorPom.xml文件中添加如下配置。当需要导入生成器插件时,需要依赖实现CommentGenerator接口的jar包。必须先安装自己的jar包到本地仓库,否则会报找不到com.generator.MySQLCommentGenerator。其他配置同上。编译org.mybatis.generatormybatis-generator-maven-plugin1.4.0src/main/resources/generatorConfig.xmltruetruemysqlmysql-connector-java8.0.16org.exampleMyGenerator<版本>1.0-SNAPSHOT4源码分析查看执行MybatisGenerator的主要方法,主要分为两部分,解析指定的配置文件和调用生成java文件和Mapper文件的方法4.1解析指定的xml配置文件跟踪解析的方法xml文件cp.parseConfiguration(configFile)发现底层读取Document形式的xml文件,根据标签名解析各个标签属性,保存在Configuration实例中。解析commentGenerator标签的方法parseCommentGenerator(context,childNode)会获取commentGenerator标签的type属性值,即自定义的“com.generator.MySQLCommentGenerator”类放在Context实例中。4.2调用生成java文件和Mapper文件的方法。解析xml配置文件,获取Configuration实例。后续生成文件的工作会从Configuration实例中获取需要的数据。该文件生成方法的主要步骤为:1、连接数据库,查询表信息和列信息;2、生成文件内容;3、写入生成的文件。在生成文件内容时,会根据Context的type属性的反映,创建MySQLCommentGenerator实例,然后调用自定义生成注释的方法。如:为实体类文件生成注释,调用addModelClassComment方法生成字段注释,调用addFieldComment方法生成Get方法注释,调用addGetterComment方法,调用addModelClassComment、addFieldComment、addGetterComment等方法生成注释时,MySQLCommentGenerator类的所有方法被执行。这样就实现了生成自定义注解的功能。5小结通过自定义实现CommentGenerator接口,可以在自动生成的代码中添加我们想要的注释,可以省去自己添加注释的麻烦。和一般使用MybatisGenerator生成代码的方式一样,只是多实现一些接口。在Maven模式下运行时,pom.xml导入插件时需要依赖自己的jar包。