mybatis generator插件系列--注释插件 (为实体类生成数据库字段注释)

我们都知道mybatis generator自动生成的注释没什么实际作用,而且还增加了代码量。如果能将注释从数据库中捞取到,不仅能很大程度上增加代码的可读性,而且减少了后期手动加注释的工作量。

1、首先定义注释生成插件

MyCommentGenerator.java 

package com.ilovey.mybatis.comment;


import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.internal.DefaultCommentGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * mybatis generator生成注释插件
 * <p>
 * Created by huhaichao on 2017/5/15.
 */
public class MyCommentGenerator extends DefaultCommentGenerator {
    private Properties properties;
    private Properties systemPro;
    private boolean suppressDate;
    private boolean suppressAllComments;
    private String currentDateStr;

    public MyCommentGenerator() {
        super();
        properties = new Properties();
        systemPro = System.getProperties();
        suppressDate = false;
        suppressAllComments = false;
        currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }


    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        if (suppressAllComments) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        field.addJavaDocLine("/**");
        sb.append(" * ");
        sb.append(introspectedColumn.getRemarks());
        field.addJavaDocLine(sb.toString().replace("\n", " "));
        field.addJavaDocLine(" */");
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }

    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }

    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {

    }

    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {

    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {

    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
    }


}

2、然后为mybatisgenerator配置插件

<?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>
    <context id="context1">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
    
        <!-- 使用自定义的插件 -->
        <commentGenerator type="com.ilovey.mybatis.comment.MyCommentGenerator">

        </commentGenerator>


        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF8"
         userId="root" password="123456">
        </jdbcConnection>
        
        <javaModelGenerator targetPackage="com.ilovey.biz.entity.base"
         targetProject="ilovey.biz/src/main/java"/>
        <sqlMapGenerator targetPackage="com.ilovey.biz.mapper.base"
         targetProject="ilovey.biz/src/main/resources"/>
        <javaClientGenerator targetPackage="com.ilovey.biz.mapper.base"
         targetProject="ilovey.biz/src/main/java" type="XMLMAPPER"/>
        
        <table tableName="us_user_info"  domainObjectName="UsUserInfo">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>


    </context>
</generatorConfiguration>

 

3、生成效果

package com.ilovey.biz.entity.base;

import java.io.Serializable;
import java.util.Date;

public class UsUserInfo implements Serializable {
    /**
     * 
     */
    private Integer id;

    /**
     * 用户id
     */
    private Integer userId;

    /**
     * 昵称
     */
    private String nickName;

    /**
     * 头像
     */
    private String headImage;

    /**
     * 手机号码
     */
    private String mobile;

    /**
     * 性别(0保密,1男,2女)
     */
    private Integer sex;

    /**
     * 地区
     */
    private Integer region;

    /**
     * 个性签名
     */
    private String signature;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    //setter 和 getter方法省略
}

这里虽然消灭看不懂的英文注释,改为友好的中文注释,但是生成的getter/setter方法仍然很长,经常还要生成toString/hashCode/Equals等方法,其实我们只需要几个属性定义了,却不得不生成几倍的代码,大家有没有因此而头痛呢?

是不是想说有什么方法可以解决吗,请参考我的接下里的文章《mybatis generator插件系列--lombok插件 (减少百分之九十bean代码)

本文所有代码及demo:https://gitee.com/chaocloud/generator-demo.git

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿超的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值