IDEA使用mybatis-generator自动生成工具

本文详细介绍如何使用MyBatis Generator插件自动生成Mapper、JavaBean及SQL映射文件,包括配置POM文件、mybatisGenerator.xml文件以及运行工具的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、pom文件添加plugin

<!--mybatis 自动生成工具-->
<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <configuration>
      <!-- mybatis 配置文件路径 -->
      <configurationFile>src/main/resources/mybatisGenerator.xml</configurationFile>
      <!-- 警告:覆盖式文件写入 -->
      <overwrite>true</overwrite>
      <!-- 打印日志 -->
      <verbose>true</verbose>
  </configuration>
  <executions>
      <execution>
          <id>Generate MyBatis Artifacts</id>
          <!-- mybatis-generator-maven-plugin生效的生命周期 -->
          <phase>deploy</phase>
          <goals>
              <goal>generate</goal>
          </goals>
      </execution>
  </executions>
  <dependencies>
      <!-- 为插件单独配置oracle驱动 -->
      <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>ojdbc6</artifactId>
          <version>11.2.0.3</version>
      </dependency>
      <dependency>
          <groupId>tk.mybatis</groupId>
          <artifactId>mapper</artifactId>
          <version>3.4.3</version>
      </dependency>
  </dependencies>
</plugin>

2、创建mybatisGenerator.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="cloudDiskTables" targetRuntime="MyBatis3"
             defaultModelType="flat">
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.example.mybatis.MyCommonMapper" />
            <!--caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true-->
            <property name="caseSensitive" value="true"/>
        </plugin>
        <!-- 生成 JavaBean 对象重写 toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        <!-- 生成 JavaBean 对象继承 Serializable 类 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <!-- 生成 JavaBean 对象重写 equals 和 hashCode 方法 -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
        <!-- 生成 JavaBean 对象的Setter方法,返回自身,实现setter方法的链式调用 -->
        <plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin"/>

        <commentGenerator>
            <property name="suppressDate" value="true" />
            <!-- 取消注释 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="root"
                        password="123456">
        </jdbcConnection>

        <!-- 配置表、Java属性的对应关系 -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 配置生成文件的位置 -->
        <javaModelGenerator targetPackage="com.example.bean" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- xml生成器 -->
        <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成dao -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.example.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 单独设置一张表的自动代码生成 -->
        <table tableName="USER" domainObjectName="User"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               enableUpdateByExample="false"
               selectByExampleQueryId="false">
            <!-- 配置插入一条记录后 获取到插入后的主键ID -->
            <generatedKey column="ID" sqlStatement="oracle" type="post" identity="true"/>
        </table>
    </context>
</generatorConfiguration>

3、点击运行mybatis-generator工具即可生成在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值