概述
Mybatis Generator 可以生成mybatis的模板代码,包括动态脚本、实体类、Mapper映射访问类。
Mybatis Generator 有多种使用方式,此处介绍一种线上环境比较用的多的场景,通过Maven插件使用。
使用方法如下:
- 通过核心jar包cmd使用。 例如
java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite
- 通过
Ant task
使用。
- 通过 java程序使用。
- 通过 Maven Plugin 使用。
前置条件
- JDK 1.8+
- SpringBoot 2.1+
- mybatis 3+
引入 MybatisGenerator
- 引入mybatis,用于后续的代码使用。
1 2 3 4 5
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
|
- MybatisGenerator Maven Plugin的引入和配置。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.1</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfigSqlLite.xml</configurationFile> </configuration> </plugin> </plugins> </build>
|
MBG配置使用
MBG配置有两种形式,一种是xml、一种是java代码。此处演示使用的是xml配置的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?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> <classPathEntry location="E:\repo\mvn-repo\org\xerial\sqlite-jdbc\3.40.1.0\sqlite-jdbc-3.40.1.0.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="org.sqlite.JDBC" connectionURL="jdbc:sqlite:E:\ws-research\backend\rssboot\src\main\resources\db\rssboot.db" userId="" password=""> </jdbcConnection>
<javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver>
<javaModelGenerator targetPackage="io.rainforest.rss.dao.po" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources/mybatis"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="io.rainforest.rss.dao.mapper" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator>
<table tableName="rss_follow" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="id" sqlStatement="MySql" identity="true" /> </table>
</context> </generatorConfiguration>
|
参考