首先使用Spring Initializr快速创建方式,如图所示:
第二步,在src下创建MBG.xml配置文件
MBG.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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/pinpin"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--指定Javabean生成的位置-->
<javaModelGenerator targetPackage="com.api.pinpin.bean" targetProject=".srcmainjava">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--指定sql映射文件生成的位置-->
<sqlMapGenerator targetPackage="com.api.pinpin.mapper" targetProject=".srcmain esources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--指定dao接口生成的位置,mapper接口生成的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.api.pinpin.dao" targetProject=".srcmainjava">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--指定每个表的生成策略-->
<table tableName="admin_group" domainObjectName="admin_group"></table>
<table tableName="admin_log" domainObjectName="admin_log"></table>
<table tableName="category" domainObjectName="category"></table>
<table tableName="coupon" domainObjectName="coupon"></table>
<table tableName="coupon_accept" domainObjectName="coupon_accept"></table>
<table tableName="deliver" domainObjectName="deliver"></table>
<table tableName="deliver_lincense" domainObjectName="deliver_lincense"></table>
<table tableName="deliver_task" domainObjectName="deliver_task"></table>
<table tableName="food" domainObjectName="food"></table>
<table tableName="food_category" domainObjectName="food_category"></table>
<table tableName="mysite" domainObjectName="mysite"></table>
<table tableName="notice" domainObjectName="notice"></table>
<table tableName="order" domainObjectName="order"></table>
<table tableName="order_comment" domainObjectName="order_comment"></table>
<table tableName="order_detail" domainObjectName="order_detail"></table>
<table tableName="order_food" domainObjectName="order_food"></table>
<table tableName="order_process" domainObjectName="order_process"></table>
<table tableName="order_refund" domainObjectName="order_refund"></table>
<table tableName="pay" domainObjectName="pay"></table>
<table tableName="shop" domainObjectName="shop"></table>
<table tableName="shop_activity" domainObjectName="shop_activity"></table>
<table tableName="shop_collect" domainObjectName="shop_collect"></table>
<table tableName="shop_info" domainObjectName="shop_info"></table>
<table tableName="shop_license" domainObjectName="shop_license"></table>
<table tableName="shop_log" domainObjectName="shop_log"></table>
<table tableName="user" domainObjectName="user"></table>
<table tableName="user_address" domainObjectName="user_address"></table>
</context>
</generatorConfiguration>
对应自己创建的数据库表:
在pom.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatisplus.version>2.2.0</mybatisplus.version>
<mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 实体类使用Data注解,不必要生成的get、set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--springboot-JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--spring boot 监控框架 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<!--mybatis plus start-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!--mybatis-plus end-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--配置druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!--连接mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis逆向工程插件 -->
<!--mybaits逆向工程-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
编写测试类:MbgTes
public class MbgTest {
public static void main(String args[]) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/MBG.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
最后启动MbgTes类就可以生成!如图: