mybatis-generator-maven-plugin是mybatis 官方發(fā)布的maven插件,能夠自動(dòng)生成代碼,在項(xiàng)目開(kāi)發(fā)前期設(shè)計(jì)完數(shù)據(jù)庫(kù)庫(kù)后,需要大量編寫(xiě)實(shí)體類(lèi),使用maven插件省時(shí)省力,快速高效,來(lái)看看怎么自動(dòng)生成代碼.
碼云參考代碼下載地址:下載
在實(shí)驗(yàn)過(guò)多個(gè)idea第三方插件后嘗試無(wú)果,最終還是選擇官方maven插件,成功插入數(shù)據(jù).mabatis官方文檔地址能不能打開(kāi)看緣分,來(lái)看看詳細(xì)的使用教程吧.
1. 在pom文件中引入pom插件,引入dependency和plugin
<?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 https://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>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>blog</name>
<description>Spring Boot blog</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2. 編寫(xiě)配置文件generatorConfig.xml
在resources目錄下新建配置文件generatorConfig.xml編寫(xiě)數(shù)據(jù)庫(kù)映射配置,包括需要映射的文件目錄,以及需要映射的數(shù)據(jù)庫(kù)表.來(lái)看下我的配置.
<?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="testTables" targetRuntime="MyBatis3Simple">
<commentGenerator>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)、連接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1/springboot?serverTimezone=UTC&nullCatalogMeansCurrent=true" userId="root"
password="root">
</jdbcConnection>
<!-- 默認(rèn)false,把JDBC DECIMAL 和 NUMERIC 類(lèi)型解析為 Integer,為 true時(shí)把JDBC DECIMAL 和
NUMERIC 類(lèi)型解析為java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO類(lèi)的位置 -->
<javaModelGenerator targetPackage="com.springboot.blog.entity"
targetProject="./src/main/java">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數(shù)據(jù)庫(kù)返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置xml -->
<sqlMapGenerator targetPackage="mapping"
targetProject="./src/main/resources">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.springboot.blog.mapper"
targetProject="./src/main/java">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定數(shù)據(jù)庫(kù)表 -->
<table schema="springboot" tableName="article"></table>
<table schema="springboot" tableName="article_tag"></table>
<table schema="springboot" tableName="blogroll"></table>
<table schema="springboot" tableName="category"></table>
<table schema="springboot" tableName="comment"></table>
<table schema="springboot" tableName="nav"></table>
<table schema="springboot" tableName="tag"></table>
<!-- 有些表的字段需要指定java類(lèi)型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
3. 執(zhí)行maven命令生成代碼
執(zhí)行mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
生成代碼,或者直接點(diǎn)擊生成代碼
image.png
4. 執(zhí)行成功結(jié)果
等待執(zhí)行成功后,會(huì)生成相應(yīng)的文件,包括實(shí)體,mapper以及xml配置,image.png
5.測(cè)試生成的代碼是否可用.
以文章為例,我們?cè)贁?shù)據(jù)庫(kù)中手動(dòng)插入一條測(cè)試數(shù)據(jù).
然后找到mapper/ArticleMapper 在類(lèi)上添加@Mapper
注解,自動(dòng)生成的代碼不帶注解.
image.png
6.編寫(xiě)測(cè)試articleservice以及impl實(shí)現(xiàn)類(lèi)是否可用.
public interface ArticleService {
int deleteByPrimaryKey(Integer id);
int insert(Article record);
Article selectByPrimaryKey(Integer id);
List<Article> selectAll();
int updateByPrimaryKey(Article record);
}
@Service
public class ArticleServiceImpl implements ArticleService {
@Resource//@Autowired或者使用
private ArticleMapper mapper;
@Override
public int deleteByPrimaryKey(Integer id) {
return 0;
}
@Override
public int insert(Article record) {
return 0;
}
@Override
public Article selectByPrimaryKey(Integer id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public List<Article> selectAll() {
return null;
}
@Override
public int updateByPrimaryKey(Article record) {
return 0;
}
}
7.編寫(xiě)測(cè)試articlecontroller.
@RestController
@RequestMapping("article")
public class ArticleController {
@Autowired
private ArticleService service;
/**
* 通過(guò)主鍵查詢單條數(shù)據(jù)
*
* @param id 主鍵
* @return 單條數(shù)據(jù)
*/
@GetMapping("selectOne")
public Article selectOne(Integer id) {
return this.service.selectByPrimaryKey(id);
}
}
image.png
請(qǐng)求地址:http://127.0.0.1:8080/article/selectOne?id=1
image.png
請(qǐng)求成功.