使用Springboot集成MybatisGenerator

概述

Mybatis Generator 可以生成mybatis的模板代码,包括动态脚本、实体类、Mapper映射访问类。
Mybatis Generator 有多种使用方式,此处介绍一种线上环境比较用的多的场景,通过Maven插件使用。

使用方法如下:

  1. 通过核心jar包cmd使用。 例如 java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite
  2. 通过 Ant task 使用。
  3. 通过 java程序使用。
  4. 通过 Maven Plugin 使用。

前置条件

  • JDK 1.8+
  • SpringBoot 2.1+
  • mybatis 3+

引入 MybatisGenerator

  1. 引入mybatis,用于后续的代码使用。
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
  1. MybatisGenerator Maven Plugin的引入和配置。
<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.1</version>
            <configuration>
                <!-- 指定配置文件地址-->
                <configurationFile>${basedir}/src/main/resources/generatorConfigSqlLite.xml</configurationFile>
            </configuration>
        </plugin>
    </plugins>
</build>

MBG配置使用

MBG配置有两种形式,一种是xml、一种是java代码。此处演示使用的是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>
    <!-- 指定数据库连接池用到的依赖 -->
    <classPathEntry location="E:\repo\mvn-repo\org\xerial\sqlite-jdbc\3.40.1.0\sqlite-jdbc-3.40.1.0.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 指定 数据库链接配置 -->
        <jdbcConnection driverClass="org.sqlite.JDBC"
                        connectionURL="jdbc:sqlite:E:\ws-research\backend\rssboot\src\main\resources\db\rssboot.db"
                        userId=""
                        password="">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 指定javaBean生成的位置 -->
        <javaModelGenerator targetPackage="io.rainforest.rss.dao.po"
                            targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaModelGenerator>

        <!--指定sql映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources/mybatis">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 指定dao接口生成的位置,mapper接口 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="io.rainforest.rss.dao.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- table指定每个表的生成策略 -->
        <table tableName="rss_follow" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="MySql" identity="true" />
        </table>

    </context>
</generatorConfiguration>

参考

posted @ 2023-02-20 17:27  落叶微风  阅读(19)  评论(0编辑  收藏  举报  来源