Mybatis --- mybatis-generator组件

初衷

玩掘金应该有一段时间了吧,可是历来没有在上面写过任何东西.以前有维护过一个我的博客,原本想继续更新,但每逢迁移,前先后后不少事情,本身感受比较麻烦,干脆之后就来这里笔记本身的思考和学习吧.php

为何要学习mybatis-generator

官方已经提供了咱们比较好用的mybatis-generator插件,why咱们还要折腾?我思考了下得出了如下三个缘由:java

  1. 企业开发中咱们每每须要更方便,更快速的工具
  2. 提高自我对mybatis的学习,丰富自我装装逼,哈哈^^

本文重点

1.将mybatis-generator 集成到maven中 2.自定义生成器,生成实体的注释(原生的注释太啰嗦)mysql

环境

1.spring-boot-2.1.1.RELEASEgit

2.mybatis-generator-1.3.5github

封装本身的mybatis-generator插件(打成公共jar包的方式)

1.建立Spring Maven项目,添加Pom依赖spring

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.5</version>
</dependency>
复制代码

2.由于咱们是以公共jar的方式提供generator插件, 因此删除Spring-Boot启动类,同时Pom中添加 Maven打包插件sql

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
            <execution>
                <id>attach-source</id>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
复制代码

3.由于咱们是以jar包的形式被引入其余项目,因此pom中能够配置私服,deploy到私服.至于私服怎么搭建这里就再也不累赘,这里就以本地搭建的私服为例数据库

<distributionManagement>
    <repository>
        <id>ov-release</id>
        <name>Release Repository of ov</name>
        <url>http://localhost:8081/repository/ov-release/</url>
    </repository>
    <snapshotRepository>
        <id>ov-snapshot</id>
        <name>Snapshot Repository of ov</name>
        <url>http://localhost:8081/repository/ov-snapshot/</url>
    </snapshotRepository>
</distributionManagement>
复制代码

4.实现本身的CommentGenerator 查看mybatis-generator的源码会发现,咱们生成实体的注释都是经过CommentGenerator接口来作的,而框架已经为咱们提供了默认的也就是DefaultCommentGenerator(注释不满意), 因为咱们对框架默认产生的注释不满意, 因此接下来咱们实现本身的CommentGenerator(由以上分析咱们能够extends DefaultCommentGenerator || implements CommentGenerator), 这里咱们就采用实现的方式吧apache

5.定义CommentGeneratorAdaptor实现: 做为适配空实现CommentGeneratorapi

public class CommentGeneratorAdaptor implements CommentGenerator {
    @Override
    public void addConfigurationProperties(Properties properties) {

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean b) {

    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

    }

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

    }

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

    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {

    }

    @Override
    public void addComment(XmlElement xmlElement) {

    }

    @Override
    public void addRootComment(XmlElement xmlElement) {

    }
}
复制代码

6.定义CommentGeneratorWrapper实现

public class CommentGeneratorWrapper extends CommentGeneratorAdaptor {

    private Properties properties;

    public CommentGeneratorWrapper() {
        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(" *");
        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("/** " + remarks + " */");
    }
}
复制代码

7.打包,发布到私服

mvn clean install deploy
复制代码

项目使用

此时咱们就能够在业务项目中使用咱们刚才打包的mybatis-generator了, 对了,忘记说明一点,在此以前咱们打成的generator maven坐标为:

<dependency>
    <groupId>com.github.ov</groupId>
    <artifactId>generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
复制代码

1.咱们在业务项目中引入坐标

<dependency>
    <groupId>com.github.ov</groupId>
    <artifactId>generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
复制代码

2.在Maven中配置插件以Maven的形式运行

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <dependencies>
        <dependency>
            <groupId>com.github.ov</groupId>
            <artifactId>generator</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--connector driver jar-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</plugin>
复制代码

3.在资源文件夹下面建立generatorConfig.xml

<?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>
    <!--指定特定数据库的jdbc驱动jar包的位置 -->
    <!--<classPathEntry location="mysql-connector-java-5.1.37-bin.jar"/>-->

    <context id="XMBG-01" targetRuntime="MyBatis3">
        <!-- 生成的 Java 文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 格式化 Java 代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化 XML 代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

        <!-- 自定义注释生成器 -->
        <commentGenerator type="com.github.ov.generator.CommentGeneratorWrapper">
            <property name="author" value="JOSE"/>
            <property name="dateFormat" value="yyyy-MM-dd"/>
        </commentGenerator>

        <!-- 配置数据库链接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://ip:3306/spring_cloud?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;useSSL=false" userId="xxx" password="xxx">
            <property name="useInformationSchema" value="true" />
        </jdbcConnection>

        <!--默认false Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC. -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体的位置 -->
        <javaModelGenerator targetPackage="com.github.product.model.po" targetProject="MAVEN">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>

        <!-- 生成 Mapper 接口的位置 -->
        <sqlMapGenerator targetPackage="com.github.product.dao" targetProject="MAVEN">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成 Mapper XML 的位置 -->
        <javaClientGenerator targetPackage="com.github.product.dao" type="XMLMAPPER" targetProject="MAVEN">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 设置数据库的表名和实体类名 -->
        <table tableName="product_info" domainObjectName="ProductInfo" mapperName="ProductInfoDao" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="JDBC" identity="true" />
        </table>
    </context>
</generatorConfiguration>
复制代码

4.至此咱们运行generator插件 || mvn命令运行就会生成咱们的持久层类, 在target/ 目录下,也会发现注释是咱们数据库的注释

mvn mybatis-generator:generate
复制代码

5.运行结果:

6.注意:咱们自定义的CommentGenerator已经要打成包引入,否则会找不到此类, 固然这是Maven形式运行; 固然你也能够采用Main方法的形式运行, 只不过我感受在企业开发中这个更方便

相关文章
相关标签/搜索