使用Springboot集成MybatisGenerator

世上唯一不能复制的是时间,唯一不能重演的是人生,唯一不劳而获的是年龄。该怎么走,过什么样的生活,全凭自己的选择和努力。人生很贵,请别浪费!与智者为伍,与良善者同行。使用Springboot集成MybatisGenerator,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

概述

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>

参考

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/221990.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!