广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot整合mybatis-generator插件流程详细讲解
  • 111
分享到

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

Python 官方文档:入门教程 => 点击学习

摘要

mybatis-generator 插件 mybatis 相关依赖 <dependency> <groupId>org.mybatis.spring.

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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot整合mybatis-generator插件流程详细讲解

本文链接: https://www.lsjlt.com/news/194095.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作