spring boot 集成mybatis generator代码生成器

1.在父pom中加入mybatis generator代码生成器插件

<!-- 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/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.37</version>
					</dependency>
				</dependencies>
			</plugin>

2.在指定的文件路径下添加配置文件

<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>

在公共模块server的resources文件下新建目录generator添加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>
    <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.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/courseimooc"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- domain类的位置 -->
        <javaModelGenerator targetProject="src\main\java"
                            targetPackage="com.course.server.domain"/>

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

        <!-- mapper类的位置 -->
        <javaClientGenerator targetProject="src\main\java"
                             targetPackage="com.course.server.mapper"
                             type="XMLMAPPER" />

        <!--        <table tableName="test" domainObjectName="Test"/>-->
        <!--        <table tableName="chapter" domainObjectName="Chapter"/>-->
        <!--        <table tableName="section" domainObjectName="Section"/>-->
        <!--        <table tableName="course" domainObjectName="Course"/>-->
        <!--        <table tableName="course_content" domainObjectName="CourseContent"/>-->
        <!--        <table tableName="course_content_file" domainObjectName="CourseContentFile"/>-->
        <!--        <table tableName="teacher" domainObjectName="Teacher"/>-->
        <!--        <table tableName="file" domainObjectName="File"/>-->
        <!--        <table tableName="user" domainObjectName="User"/>-->
        <!--        <table tableName="resource" domainObjectName="Resource"/>-->
        <!--        <table tableName="role" domainObjectName="Role"/>-->
        <!--        <table tableName="role_resource" domainObjectName="RoleResource"/>-->
        <!--        <table tableName="role_user" domainObjectName="RoleUser"/>-->
        <!--        <table tableName="member" domainObjectName="Member"/>-->
        <!--        <table tableName="sms" domainObjectName="Sms"/>-->
        <table tableName="test" domainObjectName="Test"/>
    </context>
</generatorConfiguration>

3.添加Maven命令
在Edit Configuration中
在这里插入图片描述

之后可以运行一下。
如果报错
有如下可能:
1.命令写错
正确写法:mybatis-generator:generate -e
2.父pom文件没有添加mybatis generator插件

4.解决代码生成重复的问题
解决方法一: 手动删除mapper.xml
解决方案二:在generatorConfig.xml配置文件中添加

    <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

注:该UnmergeableXmlMappersPlugin类在1.3.7版本才出现

5.example的使用
在这里插入图片描述
给类是代码生成器生成的
Example可以帮我们写入where ,order by等,熟练掌握,可以极大地提高单表的开发效率

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值