generator反向工程使用

3 篇文章 0 订阅

有时候我们在写mybatis的dto或者xml的时候数据库字段特别多一些很简单的sql语句我们也不是很想写 这时候就可以引入generator反向工程帮我们去解决这个问题 话不多说直接上代码
先引入这个generator的插件

这里我们将数据库的配置直接放到xml里面了 注意这里的mysql配置一定要和
当前项目的mysql配置一致否则会报错
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!--这里指定插件对应的xml文件路径-->
                    <configurationFile>src/main/generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.47</version>
                    </dependency>
                </dependencies>
            </plugin>
<?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="MySqlTables" targetRuntime="MyBatis3">
    这里的注解生成器会有一个问题就是生成出来的对象注解都是英文写死的后续可以使用
    自定义的注解来解决这个问题
        <!-- 自定义注释生成器 -->
        <commentGenerator>
            <!-- 是否阻止生成注释 true:阻止  false:不阻止(默认) -->
            <property name="suppressAllComments" value="false"/>
            <!-- 是否阻止生成的注释包含时间戳 true:阻止 false:不阻止(默认)-->
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!--        数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/ds0?characterEncoding=utf-8"
                        userId="root"
                        password="">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类的包名和位置 -->
        <javaModelGenerator targetPackage="com.example.pojo" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成的xml映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="mybatis.item0" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao.item0" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--填写表的名字之后执行插件自动生成
        domainObjectName是指定生成的相关文件的基本名如果是xml或者mapper就是前缀
        如:generatorTableTestMapper-->
        <table tableName="generator_table" domainObjectName="generatorTableTest"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>

之后就在指定项目的plugin插件里面点击反向工程的执行即可

在这里插入图片描述

生成的相关文件 这里只展示一个pojo,可以看到确实注释很难看,我们可以通过自定义注释类来处理一下这个默认注释很难看的问题

public class GeneratorTable {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column generator_table.id
     *
     * @mbg.generated
     */
    private String id;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column generator_table.name
     *
     * @mbg.generated
     */
    private String name;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column generator_table.id
     *
     * @return the value of generator_table.id
     *
     * @mbg.generated
     */
    public String getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column generator_table.id
     *
     * @param id the value for generator_table.id
     *
     * @mbg.generated
     */
    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column generator_table.name
     *
     * @return the value of generator_table.name
     *
     * @mbg.generated
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column generator_table.name
     *
     * @param name the value for generator_table.name
     *
     * @mbg.generated
     */
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
}

我们先定义一个BaseCommon 实现 CommentGenerator重写所有的方法,之后在写一个MyCommentGenerator继承BaseCommon 主要还是为了方便重写方法我们着重贴一下MyCommentGenerator里面的代码,这里针对字段和类的注释做了修改

public class MyCommentGenerator extends BaseCommon {
    private Properties properties;

    public MyCommentGenerator() {
        this.properties = new Properties();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, 
    							IntrospectedTable introspectedTable) {
        String author = this.properties.getProperty("author");
        String dateFormat = this.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(" */");
    }
}

之后我们去到generator.xml里面设置注释使用我们自己的注释类

     <!--自定义注释生成器-->
    <commentGenerator type="com.pojo.MyCommentGenerator">
        <property name="author" value="ygliu"/>
        <property name="dateFormat" value="yyyy/MM/dd"/>
    </commentGenerator>

注意!!!

Cannot instantiate object of type com.pojo.MyCommentGenerator

可能你们会遇到上面非常坑的报错,自定义注释类确实写了,也确实路径没问题,但是一直报错没办法实例化这个也是折磨了我很久最后看了文章才发现原来generator的plugin有自己的classpath所以我们在项目里面写的代码路径是匹配不上的我们只能通过重开一个项目打包之后依赖这个jar或者maven来获取并且在pom的plugin里面进行配置,我这里直接将jar包打到lib文件夹里面然后依赖即可成功

<!--这里指定我们自定义的注解-->
<dependency>
   <groupId>test</groupId>
   <artifactId>testCommon</artifactId>
   <version>1.0-SNAPSHOT</version>
   <scope>system</scope>
   <systemPath>${basedir}/lib/testCommon-1.0-SNAPSHOT.jar</systemPath>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值