springboot如何集成mybatis官方生成器

2024-03-14,

这篇文章主要介绍springboot如何集成mybatis官方生成器,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

引入习惯plugin

<!-- mybatis generator 自动生成代码插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.7</version>
				<configuration>
					<configurationFile>src/main/resources/generator/generator-config.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>8.0.22</version>
					</dependency>
				</dependencies>
			</plugin>

resource下插件代码生成器配置

创建generator文件夹,并创建generator-config.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>
    <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">

        <!-- 自动检查关键字,为关键字增加反引号 -->
        <property name="autoDelimitKeywords" value="true"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!--覆盖生成 XML 文件-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <!-- 生成的实体类添加 toString()方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 不生成注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://rm-bp1tja0cb2r7t5il5ko.mysql.rds.aliyuncs.com:3306/wiki_dev?serverTimezone=Asia/Shanghai"
                        userId="wiki_dev"
                        password="Wiki677877">
        </jdbcConnection>

        <!-- domain 类的位置 -->
        <javaModelGenerator targetProject="src\main\java"
                            targetPackage="cool.tsy.wiki.domain"/>

        <!-- mapper xml的位置 -->
        <sqlMapGenerator targetProject="src\main\resources"
                         targetPackage="mapper"/>

        <!-- mapper类的位置 -->
        <javaClientGenerator targetProject="src\main\java"
                             targetPackage="cool.tsy.wiki.mapper"
                             type="XMLMAPPER"/>

        <table tableName="demo" domainObjectName="Demo"/>
        <table tableName="ebook"/>
        <table tableName="category"/>
        <table tableName="doc"/>
        <table tableName="content"/>
        <table tableName="user"/>
        <table tableName="ebook_snapshot"/>
    </context>
</generatorConfiguration>

添加maven命令

点击edit config

选择添加maven命令

输入这样一条命令

mybatis-generator:generate -e

新建一张数据库表进行测试 点击运行

如下图所示,完成代码生成

以上是“springboot如何集成mybatis官方生成器”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注本站行业资讯频道!

《springboot如何集成mybatis官方生成器.doc》

下载本文的Word格式文档,以方便收藏与打印。