mybatis-generator 逆向工程
1、 构建springboot项目
项目结构如下:
2、导包
添加如下:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<!--指定generatorConfig.xml文件-->
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</plugin>
3、创建generatorConfig.xml文件
3.1 新建文件,文件名为generatorConfig.xml
选择resources下的generator文件,选择new -> Mybatis -> new file,命名为generatorConfig.xml
3.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>
<!--mysql驱动包-->
<!--如果在pom.xml中的<plugin>标签中添加了mysql驱动,这里可以不用配置-->
<classPathEntry location="D:\setup\soft\maven\apache-maven-3.6.3\repository\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar"/>
<!--
targetRuntime="MyBatis3Simple":生成简单版的CRUD
MyBatis3:豪华版
-->
<context id="context" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- jdbcConnection:指定如何连接到目标数据库 -->
<jdbcConnection userId="root" password="123456" driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db?serverTimezone=GMT%2B8&useUnicode=true&useSSL=false&characterEncoding=utf8"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- javaModelGenerator:指定javaBean的生成策略
targetPackage="test.model":目标包名
targetProject="\MBGTestProject\src":目标工程
-->
<javaModelGenerator targetPackage="com.aion.mybatisgeneratordemo.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- sqlMapGenerator:sql映射生成策略: -->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- javaClientGenerator:指定mapper接口所在的位置 -->
<javaClientGenerator targetPackage="com.aion.mybatisgeneratordemo.mapper" type="XMLMAPPER" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定要逆向分析哪些表:根据表要创建javaBean -->
<table schema="Account" tableName="account" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="Order" tableName="orders" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<!-- <table tableName="orders" schema="Order" />-->
</context>
</generatorConfiguration>