Mybatis逆向工程-mybatis generator

1. 修改pom.xml文件

在pom.xml文件中添加依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>

2. 增加gengratorConfig.xml文件,放在src/main/resources下。

<?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="DB2Tables" targetRuntime="MyBatis3">
    <commentGenerator>
    	<!-- 是否去除自动生成的注释 -->
    	<property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!-- Mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
        connectionURL="jdbc:mysql://39.99.202.26:3306/jwnming?useSSL=false&amp;serverTimezone=UTC"
        userId="root"
        password="mysql123">
    </jdbcConnection>
    <!-- Oracle数据库
	    <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
	        connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
	        userId="yycg"
	        password="yycg">
	    </jdbcConnection> 
    -->
	
	<!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时
	把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
    <javaTypeResolver >
		<property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
	
	<!-- targetProject:生成POJO类的位置 -->
    <javaModelGenerator targetPackage="com.lili.springboot.entity" targetProject=".srcmainjava">
		<!-- enableSubPackages:是否让schema作为包的后缀 -->
		<property name="enableSubPackages" value="false" />
		<!-- 从数据库返回的值被清理前后的空格 -->
		<property name="trimStrings" value="true" />
    </javaModelGenerator>
    
	<!-- targetProject:mapper映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mappers"  targetProject=".srcmain
esources">
		<!-- enableSubPackages:是否让schema作为包的后缀 -->
		<property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    
	<!-- targetProject:mapper接口生成的的位置 -->
	<javaClientGenerator type="XMLMAPPER" targetPackage="com.lili.springboot.mapper"  targetProject=".srcmainjava">
		<!-- enableSubPackages:是否让schema作为包的后缀 -->
		<property name="enableSubPackages" value="false" />
    </javaClientGenerator>
    
	<!-- 指定数据表 -->
	<table schema="" tableName="jwnming"
	  enableInsert="true"
                enableDeleteByPrimaryKey="true"
                enableUpdateByPrimaryKey="true"
                enableSelectByPrimaryKey="true"

                enableDeleteByExample="true"
                enableUpdateByExample="true"
                enableSelectByExample="true"
                enableCountByExample="true"
	
	></table>
    <!-- 有些表的字段需要指定java类型 
    <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>

3. 运行插件的两种方式(eclipse):使用main方法运行和使用插件运行

a.使用main方法
b.使用插件:右键项目->runAs->Run Configurations
name:随便起个名字
Base directory:项目路径,
Goals:插件
最后运行Run,刷新项目。如果要重新生成要先删除之前的。

原文地址:https://www.cnblogs.com/linanana/p/14024274.html