Mybatis官方代码生成器的使用
1.引入Mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
2.引入maven插件 --> 自动生成代码插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>src/main/resources/generator/generator-config.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>
</plugins>
</build>
3.创建生成器配置文件
generator-config.xml
位置: src/main/resources/generator/generator-config.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="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/2022_wikidev?characterEncoding=utf-8"
userId="2022_wikidev" // 对应username
password="317311">// 密码
</jdbcConnection>
<javaModelGenerator targetProject="src\main\java"
targetPackage="look.word.wiki.domain"/>
<sqlMapGenerator targetProject="src\main\resources"
targetPackage="mapper"/>
<javaClientGenerator targetProject="src\main\java"
targetPackage="look.word.wiki.mapper"
type="XMLMAPPER"/>
<table tableName="ebook"/>
</context>
</generatorConfiguration>
4.启动maven插件构建
1 点击Edit Configurations…
2. 点击+ 找到maven 点击
3 输入命令 保存即可
mybatis-generator:generate -e
4.启动插件即可
跟启动项目一样 注意 每次启动 都要去 配置文件修改表名哦
|