首先使用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=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--指定sql映射文件生成的位置--> <sqlMapGenerator targetPackage="com.api.pinpin.mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--指定dao接口生成的位置,mapper接口生成的位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.api.pinpin.dao" targetProject=".\src\main\java"> <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); }
第三步,在resouce目录下创建generatorConfig.xml文件:
配置如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "E:\ideaWorkspace/mybatis-generator-config_1_0.dtd"><generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="${jdbcUrl}" userId="${username}" password="${password}" > </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="com.api.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="com.api.mapper.census" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="mybatis.census" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--生成对应表及类名,不生成example相关mapper--> <table tableName="admin_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context></generatorConfiguration>
最后启动MbgTes类就可以生成!如图:
原文地址:https://www.cnblogs.com/studygithub5208868/p/11553147.html
时间: 2024-11-11 20:53:11