Mybatis generator配置

本文介绍如何使用 MyBatis Generator 自动生成 Mapper 接口和 XML 文件,包括配置数据库连接、目标包路径等,并展示了如何通过 Maven 插件集成到项目中。

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

使用Mybatis generator批量产生mapper代码文件

在用spring boot添加mybatis时,可以使用mybatis generator自动产生代码.

  • 修改<classPathEntry>中数据库链接驱动
  • <jdbcConnection>中配置数据库链接
  • <javaModelGenerator>中配置目标包路径和文件夹路径
  • <sqlMapGenerator>中配置产生的sql map文件存放路径和包
  • <javaClientGenerator>中配置产生的接口代码
  • 配置需要扫描的表<table>
  • 运行
java -jar mybatis-generator-core-x.x.x.jar -configfile /tmp/generatorConfig.xml -overwrite

模板文件

<?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="/home/chenyu/.m2/repository/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar" />

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://127.0.0.1/wt"
        userId="wt"
        password="wt">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <javaModelGenerator targetPackage="com.example.demo.model2" targetProject="/src/main/java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <sqlMapGenerator targetPackage="mapping"  targetProject="/src/main/resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper"  targetProject="/src/main/java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>


    <table tableName="user_info" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <generatedKey column="uid" sqlStatement="select replace(uuid(), '-', '')"></generatedKey>
    </table>
    <!-- <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table> -->

  </context>
</generatorConfiguration>
添加mvn运行环境
   <project ...>
     ...
     <build>
       ...
       <plugins>
        ...
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5</version>
        </plugin>
        ...
      </plugins>
      ...
    </build>
    ...
  </project>

运行方式

mvn mybatis-generator:generate
# 或者
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

如果需要在编译时自动运行,应该做如下配置

  <project ...>
     ...
     <build>
       ...
       <plugins>
        ...
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5</version>
          <executions>
            <execution>
              <id>Generate MyBatis Artifacts</id>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        ...
      </plugins>
      ...
    </build>
    ...
  </project>

在添加使用mapper.xml文件后,需要添加配置文件,让spring组件扫描*.xml文件

mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.example.demo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值