SpringBoot如何整合mybatis-generator-maven-plugin 1.4.0

 更新时间:2023年01月12日 11:41:16   作者:白日醒梦  
这篇文章主要介绍了SpringBoot整合mybatis-generator-maven-plugin 1.4.0的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

SpringBoot实现整合mybatis-generator-maven-plugin 1.4.0

创建 Maven 工程

网上有很多教程且 Idea 可以直接创建 这里就不进行

pom.xml 引入依赖和插件

pom中generalto-maven-plugs中必须指定mysql驱动,并且明确版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.orginly</groupId>
    <artifactId>mall</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mall</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <!-- mysql8 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.24</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- springboot的maven插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!-- mybatis用于生成代码的配置文件 如果配置文件名为generatorConfig.xml 则不需要配置 -->
                    <!-- <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>-->
                    <!-- 允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 启用覆盖 -->
                    <overwrite>true</overwrite>
                </configuration>
                <!-- 引入插件所需要的依赖 -->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.24</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

generatorConfig.xml 自动生成配置文件

table标签中需要指定tableName和生成的实体名字

<?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="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否生成注释代时间戳-->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://172.17.0.2:3306/spring-boot-mall?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"
                        userId="root"
                        password="000">
        </jdbcConnection>
        <javaTypeResolver>
            <!--该属性可以控制是否强制DECIMAL和NUMERIC类型的字段转换为Java类型的java.math.BigDecimal,默认值为false,一般不需要配置。-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="top.orginly.mall.model.pojo" targetProject="src/main/java">
            <!--enableSubPackages:如果true,MBG会根据catalog和schema来生成子包。如果false就会直接用targetPackage属性。默认为false。-->
            <property name="enableSubPackages" value="true"/>
            <!--trimStrings:是否对数据库查询结果进行trim操作,如果设置为true就会生成类似这样public void setUsername(String username) {this.username = username == null &#63; null : username.trim();}的setter方法。默认值为false。-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射xml文件存放位置-->
        <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置(*Mapper.java)-->
        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
            type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
            type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
            type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="top.orginly.mall.model.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--生成对应表及类名-->
        <table tableName="mall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!--useActualColumnNames:如果设置为true,那么MBG会使用从数据库元数据获取的列名作为生成的实体对象的属性。 如果为false(默认值),MGB将会尝试将返回的名称转换为驼峰形式。 在这两种情况下,可以通过 元素显示指定,在这种情况下将会忽略这个(useActualColumnNames)属性。-->
            <property name="useActualColumnNames" value="true"/>
            <!-- 数据库表主键 可以不设置 -->
             <!-- <generatedKey column="id" sqlStatement="Mysql" identity="true"/> -->
        </table>
       
    </context>
</generatorConfiguration>

运行生成文件

这里推荐使用IDEA
我们只需要找到右边侧栏中的maven
依次找到 plugins–>mybatis-generaltor–>mybatis-generaltor:generaltor
之后双击即可,此时刷新一下项目就自动生成我们想要的,mapper和xml以及pojo

提示 BUILD SUCCESS 即生成成功!

[INFO] Scanning for projects...
[INFO] 
[INFO] [INFO] Building mall 0.0.1-SNAPSHOT
[INFO] [INFO] 
[INFO] [INFO] Connecting to the Database
[INFO] Introspecting table mall_user
[INFO] Introspecting table mall_category
[INFO] Introspecting table mall_goods
[INFO] Introspecting table mall_order
[INFO] Introspecting table mall_order_goods
[INFO] Introspecting table mall_cart
[INFO] Generating Primary Key class for table mall_user
[INFO] Generating Record class for table mall_user
[INFO] Generating Mapper Interface for table mall_user
[INFO] Generating SQL Map for table mall_user
[INFO] Generating Record class for table mall_category
[INFO] Generating Mapper Interface for table mall_category
[INFO] Generating SQL Map for table mall_category
[INFO] Generating Record class for table mall_goods
[INFO] Generating Mapper Interface for table mall_goods
[INFO] Generating SQL Map for table mall_goods
[INFO] Generating Primary Key class for table mall_order
[INFO] Generating Record class for table mall_order
[INFO] Generating Mapper Interface for table mall_order
[INFO] Generating SQL Map for table mall_order
[INFO] Generating Record class for table mall_order_goods
[INFO] Generating Mapper Interface for table mall_order_goods
[INFO] Generating SQL Map for table mall_order_goods
[INFO] Generating Record class for table mall_cart
[INFO] Generating Mapper Interface for table mall_cart
[INFO] Generating SQL Map for table mall_cart
[INFO] Saving file UserMapper.xml
[INFO] Saving file CategoryMapper.xml
[INFO] Saving file GoodsMapper.xml
[INFO] Saving file OrderMapper.xml
[INFO] Saving file OrderGoodsMapper.xml
[INFO] Saving file CartMapper.xml
[INFO] Saving file UserKey.java
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] Saving file Category.java
[INFO] Saving file CategoryMapper.java
[INFO] Saving file Goods.java
[INFO] Saving file GoodsMapper.java
[INFO] Saving file OrderKey.java
[INFO] Saving file Order.java
[INFO] Saving file OrderMapper.java
[INFO] Saving file OrderGoods.java
[INFO] Saving file OrderGoodsMapper.java
[INFO] Saving file Cart.java
[INFO] Saving file CartMapper.java
[INFO] [INFO] BUILD SUCCESS
[INFO] [INFO] Total time:  2.193 s
[INFO] Finished at: 2021-06-01T22:33:37+08:00
[INFO] 

配置 mybatis 数据源及 mappers 路径

编辑配置文件 /src/main/resources/application.properties

# 数据源名称
spring.datasource.name=spring_boot_mall
# 数据库连接 url
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/spring-boot-mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSl=false;serverTimezone=Asia/Shanghai
# 数据库驱动类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 用户名
spring.datasource.username=root
# 密码
spring.datasource.password=000

# 设置Mybatis Mapper文件路径
mybatis.config-location=classpath:mappers/*.xml

注意事项

报错 dao类无法找到

需要在spring-boot 入口类/src/main/java/top/orginly/mall/MallApplication.java中添加注解
@MapperScan(basePackages = "top.orginly.mall.model.dao")
指向mapper对应的类

@SpringBootApplication
@MapperScan(basePackages = "top.orginly.mall.model.dao")
public class MallApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallApplication.class, args);
    }

}

Service 实体类无法自动载入

类添加 @Service 注解后 出现找不到 Mapper 类无法找到

报错但不影响程序正常运行但我们要解决掉,需要给 Mapper 添加 @Repository 注解告诉 IDEA 这是一个资源类

重新生成后运行出错

必须先删除 model 包和 mapper文件!!!

到此这篇关于SpringBoot如何整合mybatis-generator-maven-plugin 1.4.0的文章就介绍到这了,更多相关SpringBoot整合mybatis-generator-maven-plugin 1.4.0内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详细图解Java中字符串的初始化

    详细图解Java中字符串的初始化

    字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串,下面这篇文章主要给大家介绍了Java中字符串初始化的相关资料,需要的朋友可以参考下
    2021-08-08
  • Springboot 多module打包方案示例详解

    Springboot 多module打包方案示例详解

    在使用Springboot进行多模块项目开发时,可能会遇到模块间依赖不存在的问题,本文介绍了两种主要的解决方案,这些方法有助于生成可执行的jar包,并确保模块间正确依赖,适用于多层项目结构中的有效管理和打包,感兴趣的朋友跟随小编一起看看吧
    2023-07-07
  • Spring Security用户定义 

    Spring Security用户定义 

    这篇文章主要介绍了Spring Security用户定义,大家都知道 Spring Security的用户定义有很多方式,其实主要有两种,基于内存的和基于数据库的,下面我给大家简单介绍一下这两种方式,需要的朋友可以参考下
    2022-02-02
  • Spring 依赖查找的使用小结

    Spring 依赖查找的使用小结

    在不同的编程框架和容器中,依赖查找的方式可能会有所不同,本文主要介绍了Spring依赖查找的使用小结,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Spring中Bean对象的定义、注册和获取流程分析

    Spring中Bean对象的定义、注册和获取流程分析

    这篇文章主要介绍了Spring中Bean对象的定义、注册和获取流程分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • java 排序算法之快速排序

    java 排序算法之快速排序

    这篇文章主要介绍了java 排序算法之快速排序,文中通过图片和代码讲解相关知识非常详细,大家如果有需要的话可以参考一下这篇文章
    2021-09-09
  • 详解Spring中singleton bean如何同时服务多个请求

    详解Spring中singleton bean如何同时服务多个请求

    这篇文章主要介绍了详解Spring中singleton bean如何同时服务多个请求
    2023-02-02
  • Spring Boot 多数据源处理事务的思路详解

    Spring Boot 多数据源处理事务的思路详解

    这篇文章主要介绍了Spring Boot 多数据源如何处理事务,本文单纯就是技术探讨,要从实际应用中来说的话,我并不建议这样去玩分布式事务、也不建议这样去玩多数据源,毕竟分布式事务主要还是用在微服务场景下,对Spring Boot 多数据源事务相关知识感兴趣的朋友参考下本文
    2022-06-06
  • IDEA远程管理docker镜像及容器服务的实现

    IDEA远程管理docker镜像及容器服务的实现

    本文主要介绍了IDEA远程管理docker镜像及容器服务的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • My eclipse 端口占用(9360)问题解决办法

    My eclipse 端口占用(9360)问题解决办法

    这篇文章主要介绍了My eclipse 工程发布时出现端口占用问题解决办法的相关资料,需要的朋友可以参考下
    2016-12-12

最新评论