目 录CONTENT

文章目录

mybatis-generator自动生成代码

在水一方
2021-12-15 / 0 评论 / 0 点赞 / 182 阅读 / 3,422 字 / 正在检测是否收录...

前言

对于单表的增删改查可以通过mybatis-generator来进行自动生成代码可以大大节省时间本文就来介绍一下

mybatis-generator有三种用法:命令行、eclipse插件、maven插件。个人觉得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用。这里采用maven插件的方式来进行

1 在pom.xml中添加plugin

<dependency>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-core</artifactId>
   <version>1.3.6</version>
</dependency>

2 添加插件和mysql驱动

<plugin>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-maven-plugin</artifactId>
   <version>1.3.2</version>
<configuration>
// 配置文件的位置
 <configurationFile>src/main/resources/mapper/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId> // 这里千万
不要忘记加入mysql驱动
  <version>8.0.15</version>
  </dependency>
</dependencies>
</plugin>

2 配置文件generatorConfig.xml

<generatorConfiguration>
    <!--<classPathEntry  location="D:\software\maven\responsetory\mysql\mysql-connector-java\8.0.20\mysql-connector-java-8.0.20.jar"/>-->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
<!--        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="XXX" userId="XXX" password="XXX"/>-->
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="XXX" userId="XXX" password="XXX"/>
        <!--生成model类存放位置-->
        <javaModelGenerator targetPackage="com.petrochina.e7.pcep.mnt.model" targetProject="D:\workspace\pcep-ems-ei\pcep-ems-ei-mnt\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="D:\workspace\pcep-ems-ei\pcep-ems-ei-mnt\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
          <!--生成Mapper类存放位置-->
        <javaClientGenerator targetPackage="com.petrochina.e7.pcep.mnt.mapper" targetProject="D:\workspace\pcep-ems-ei\pcep-ems-ei-mnt\src\main\java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <table tableName="XXXX" domainObjectName="XXXXX">

            <columnOverride column="ID" javaType="Integer"></columnOverride>

        </table>
    </context>
</generatorConfiguration>

这也是一个很大的问题,由于之前做的笔记直接保存为pdf的格式,再次回顾放放到markdown中发现都没法看了,后续真的要引起重视了

参考博客:

https://blog.csdn.net/qq_40307945/article/details/81351302

0

评论区