Java——mybatis逆向工程生成实体类

前言

        先赞后看,此生必赚!

        有时候,我们创建实体类需要跟数据库表里面的字段对应起来。

        假如一张表有数百个字段,那么手动去写实体类的话就比较麻烦,而且容易出错。

解决方案

        其实解决这个问题的方式有很多,本文介绍其中一种解决方案,通过mybatis的逆向工程生成实体类。本文使用的数据库是Oracle,MySQL只需要修改jar包以及generator.properties配置即可。

        可以从公众号【程序员高手之路】回复“逆向工程”免费获取源码!

Step1 修改pom文件

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

Step2 添加配置文件

2.1 generator.properties

jdbc.driverLocation=src/main/resources/lib/ojdbc8-12.2.0.1.jar
jdbc.orai18n=src/main/resources/lib/orai18n-21.1.0.0.jar
jdbc.driverClass=oracle.jdbc.OracleDriver
jdbc.connectionURL=jdbc:oracle:thin:@192.168.1.1:1521:orcl
jdbc.userId=test
jdbc.password=test

2.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>
    <properties resource="generator.properties"></properties>
    <classPathEntry location="${jdbc.driverLocation}"/>
    <classPathEntry location="${jdbc.orai18n}"/>
    <context id="oracleTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.userId}"
                password="${jdbc.password}">
        </jdbcConnection>

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

        <javaModelGenerator targetPackage="com.han.model" targetProject="src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.han.mapper" targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.han.mapper" targetProject="src\main\java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!--数据库表-->
        <table schema="" tableName="JC_DIC_JGGL"></table>
        <table tableName="JC_DIC_JGGL"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true">
        </table>

        <!-- 有些表的字段需要指定java类型
        <table schema="DRG" tableName="JC_DIC_JGGL" 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>

Step3 IDEA中新增运行配置

填写配置:mybatis-generator:generate

运行第三步的配置就行了

原文地址:https://www.cnblogs.com/shuhao66666/p/15196328.html