SpringBoot整合mybatis-generator插件流程详细讲解

 更新时间:2023年02月03日 10:19:38   作者:TryMyBestTo  
这篇文章主要介绍了SpringBoot整合mybatis-generator插件流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

mybatis-generator 插件

mybatis 相关依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.15</version>
</dependency>

mybatis-generator 插件,自动生成mybatis所需要的 dao、bean、mapper.xml文件。

<build>
    <plugins>
        <!-- mybatis 插件 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
            <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
            </configuration>
        </plugin>
    </plugins>
</build>

创建generatorConfig.xml 文件,是这个插件的配置文件

需要第八行<classPathEntry location=“项目mysql-connection-java-版本.jar 包的绝对路径”

<?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>
  <properties resource="application.properties"/>
  <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
  <classPathEntry location="E:\Java\IDEA\code\mall\src\main\resources\lib\mysql-connector-java-8.0.25.jar" />
  <context id="MysqlTables" targetRuntime="MyBatis3">
    <property name="autoDelimitKeywords" value="true"/>
    <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <!-- 是否生成注释 -->
    <commentGenerator>
      <!-- 是否生成注释代时间戳 -->
      <property name="suppressDate" value="true"/>
      <!-- 是否去除自动生成的注释 true:是 : false:否 -->
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--数据库链接地址账号密码-->
    <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
      connectionURL="${spring.datasource.url}"
      userId="${spring.datasource.username}"
      password="${spring.datasource.password}">
      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>
    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
         NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver>
      <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 生成实体类地址 这里需要你改动,其中 targetPackage 需要根据你自己创建的目录进行改动 -->
    <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}"
      targetProject="src/main/java">
      <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
      <property name="enableSubPackages" value="true"/>
      <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
      <property name="trimStrings" value="true"/>
      <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
      <property name="immutable" value="true"/>
    </javaModelGenerator>
    <!--生成mapper映射文件存放位置-->
    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!--生成Dao类存放位置-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="${generator.mappers}"
      targetProject="src/main/java">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!--生成对应表及类名-->
    <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 -->
    <table schema="root" tableName="imooc_mall_cart" domainObjectName="Cart"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_category" domainObjectName="Category" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order" domainObjectName="Order" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order_item" domainObjectName="OrderItem"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_product" domainObjectName="Product" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_user" domainObjectName="User" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
  </context>
</generatorConfiguration>

然后修改 application.properties 文件,增加一点配置项。

当然,也可以选择直接在 generatorConfig.xml 文件中书写,但想要运行整个项目,也是必须配置mysql的。

#mysql配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
#mybatis
#指定mapper.xml文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#设置pojo类型别名
mybatis.type-aliases-package=com.daxiong.mall.pojo
#Generator配置
#生成pojo位置
generator.javaModel-targetPackage=com.daxiong.mall.pojo
#生成接口位置
generator.mappers=com.daxiong.mall.mapper

启动插件生成文件:打开maven工具栏-》选择当前项目名-》Plugins-》mybatis-generator -》双击运行 mybatis-generator:generate 。

此时,便会**自动生成**mybatis所需要的 dao、bean、mapper.xml文件

自动生成的mapper接口示例:

// 根据 主索引字段 删除
int deleteByPrimaryKey(Integer id);
// 插入
int insert(Order record);
// 可选择性插入(值为null的字段不插入,使用默认值)
int insertSelective(Order record);
// 根据 主索引字段 查询
Order selectByPrimaryKey(Integer id);
// 可选择性更新(值为null的字段不插入,使用默认值)
int updateByPrimaryKeySelective(Order record);
// 更新
int updateByPrimaryKey(Order record);

通知MyBatis,mapper接口存放的位置:

方法一:为 主启动类添加注解:

@MapperScan(basePackages = “com.daxiong.mall.mapper”):告诉 ,mapper 接口放在了哪里。

方法二:为为每个 mapper 接口添加注解:

@Mapper:让此接口在Spring Ioc 初始化时生成 bean,以便其他类注入使用。

到此这篇关于SpringBoot整合mybatis-generator插件流程详细讲解的文章就介绍到这了,更多相关SpringBoot整合mybatis-generator内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 44条Java代码优化建议

    44条Java代码优化建议

    代码优化的最重要的作用应该是:避免未知的错误。因此,在写代码的时候,从源头开始注意各种细节,权衡并使用最优的选择,将会很大程度上避免出现未知的错误,从长远看也极大的降低了工作量
    2018-03-03
  • Java学习教程之定时任务全家桶

    Java学习教程之定时任务全家桶

    这篇文章主要给大家介绍了关于Java学习教程之定时任务全家桶的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Java使用JNA调用DLL文件

    Java使用JNA调用DLL文件

    JNA(Java Native Access)是一个在 Java 中调用本地代码的开源框架,提供了一种简单、高效的方式来访问本地动态链接库,下面我们来看看Java如何使用JNA调用DLL文件吧
    2024-12-12
  • Mybatis一对多和多对一处理的深入讲解

    Mybatis一对多和多对一处理的深入讲解

    Mybatis可以通过关联查询实现,关联查询是几个表联合查询,只查询一次,通过在resultMap里面的association,collection节点配置一对一,一对多的类就可以完成,这篇文章主要给大家介绍了关于Mybatis一对多和多对一处理的相关资料,需要的朋友可以参考下
    2021-09-09
  • SpringBoot3整合MyBatis出现异常:Property 'sqlSessionFactory'or 'sqlSessionTemplate' are required

    SpringBoot3整合MyBatis出现异常:Property 'sqlSessionFactory&a

    这篇文章主要介绍了SpringBoot3整合MyBatis报错:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required,其实不是个大问题,只是自己编码时遇到了,然后总结总结分享一下,如果有遇到类似问题的,可以参考一下
    2022-11-11
  • Java线程变量ThreadLocal源码分析

    Java线程变量ThreadLocal源码分析

    ThreadLocal用来提供线程内部的局部变量,不同的线程之间不会相互干扰,这种变量在多线程环境下访问时能保证各个线程的变量相对独立于其他线程内的变量,在线程的生命周期内起作用,可以减少同一个线程内多个函数或组件之间一些公共变量传递的复杂度
    2022-08-08
  • SpringBoot导出PDF的四种实现方法详解

    SpringBoot导出PDF的四种实现方法详解

    在Spring Boot应用程序中实现PDF导出功能,可以选择多种库和技术栈,本文为大家整理了四种常见的方法,感兴趣的小伙伴可以参考一下
    2025-02-02
  • JAVA利用接口实现多继承问题的代码实操演示

    JAVA利用接口实现多继承问题的代码实操演示

    Java语言并不支持多继承,这是由于多继承会带来许多复杂的问题,例如"菱形问题"等,下面这篇文章主要给大家介绍了关于JAVA利用接口实现多继承问题的相关资料,需要的朋友可以参考下
    2024-03-03
  • AOP之事务管理<aop:advisor>的两种配置方式

    AOP之事务管理<aop:advisor>的两种配置方式

    这篇文章主要介绍了AOP之事务管理<aop:advisor>的两种配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • JAVA后台转换成树结构数据返回给前端的实现方法

    JAVA后台转换成树结构数据返回给前端的实现方法

    这篇文章主要介绍了JAVA后台转换成树结构数据返回给前端的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03

最新评论