用maven插件自动生成mybatis代码

本文介绍如何在SpringMVC+MyBatis项目中使用MyBatis Generator自动生成代码,包括配置pom.xml及generatorConfig.xml,以及通过Maven命令生成Java实体类、Mapper接口和XML映射文件等。

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

1、在springmvc+mybatis项目的pom.xml文件中加如下内容,添加之后,maven会自动下载相关jar包,时间较长,需要耐心等待~~

<build>
    <plugins> 
       <plugin> 
       <groupId>org.mybatis.generator</groupId> 
       <artifactId>mybatis-generator-maven-plugin</artifactId> 
       <version>1.3.2</version> 
          <configuration>  
            <verbose>true</verbose>  
            <overwrite>true</overwrite>  
          </configuration>  
       </plugin> 
    </plugins>  
</build>
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2、创建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>
    <classPathEntry 
      location="D:\maven\mvnRespo\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar" />
    <context id="MysqlTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://127.0.0.1:3306/mystudy" userId="root"
            password="123456">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- targetProject:自动生成代码的位置 -->  
        <javaModelGenerator targetPackage="com.yinkgh.provider.model"
            targetProject="src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->    
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格  -->   
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="com.yinkgh.provider.dao"
            targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.yinkgh.provider.dao" targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->  
        <table schema="dispatch" tableName="user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
        </table>
        <table schema="dispatch" tableName="employee" domainObjectName="Employee"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
        </table>
    </context>
</generatorConfiguration> 

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

3、生成方式
mvn mybatis-generator:generate
选中pom.xml运行Run As
截图如下:
这里写图片描述

Goals里填写如下内容
这里写图片描述

4、生成的项目结构
这里写图片描述

5、对应的数据库中表
这里写图片描述
employee表
这里写图片描述
user表
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值