SpringBoot如何整合mybatis-generator-maven-plugin 1.4.0
SpringBoot實(shí)現(xiàn)整合mybatis-generator-maven-plugin 1.4.0
創(chuàng)建 Maven 工程
網(wǎng)上有很多教程且 Idea 可以直接創(chuàng)建 這里就不進(jìn)行
pom.xml 引入依賴和插件
pom中g(shù)eneralto-maven-plugs中必須指定mysql驅(qū)動,并且明確版本
<?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.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>top.orginly</groupId>
<artifactId>mall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- mysql8 驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- springboot的maven插件 -->
<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>
<configuration>
<!-- mybatis用于生成代碼的配置文件 如果配置文件名為generatorConfig.xml 則不需要配置 -->
<!-- <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>-->
<!-- 允許移動生成的文件 -->
<verbose>true</verbose>
<!-- 啟用覆蓋 -->
<overwrite>true</overwrite>
</configuration>
<!-- 引入插件所需要的依賴 -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
generatorConfig.xml 自動生成配置文件
table標(biāo)簽中需要指定tableName和生成的實(shí)體名字
<?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>
<!-- 是否生成注釋代時(shí)間戳-->
<property name="suppressDate" value="true"/>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫鏈接地址賬號密碼-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://172.17.0.2:3306/spring-boot-mall?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"
userId="root"
password="000">
</jdbcConnection>
<javaTypeResolver>
<!--該屬性可以控制是否強(qiáng)制DECIMAL和NUMERIC類型的字段轉(zhuǎn)換為Java類型的java.math.BigDecimal,默認(rèn)值為false,一般不需要配置。-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model類存放位置-->
<javaModelGenerator targetPackage="top.orginly.mall.model.pojo" targetProject="src/main/java">
<!--enableSubPackages:如果true,MBG會根據(jù)catalog和schema來生成子包。如果false就會直接用targetPackage屬性。默認(rèn)為false。-->
<property name="enableSubPackages" value="true"/>
<!--trimStrings:是否對數(shù)據(jù)庫查詢結(jié)果進(jìn)行trim操作,如果設(shè)置為true就會生成類似這樣public void setUsername(String username) {this.username = username == null ? null : username.trim();}的setter方法。默認(rèn)值為false。-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射xml文件存放位置-->
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao類存放位置(*Mapper.java)-->
<!-- 客戶端代碼,生成易于使用的針對Model對象和XML配置文件 的代碼
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象
type="MIXEDMAPPER",生成基于注解的Java Model 和相應(yīng)的Mapper對象
type="XMLMAPPER",生成SQLMap XML文件和獨(dú)立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="top.orginly.mall.model.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成對應(yīng)表及類名-->
<table tableName="mall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--useActualColumnNames:如果設(shè)置為true,那么MBG會使用從數(shù)據(jù)庫元數(shù)據(jù)獲取的列名作為生成的實(shí)體對象的屬性。 如果為false(默認(rèn)值),MGB將會嘗試將返回的名稱轉(zhuǎn)換為駝峰形式。 在這兩種情況下,可以通過 元素顯示指定,在這種情況下將會忽略這個(gè)(useActualColumnNames)屬性。-->
<property name="useActualColumnNames" value="true"/>
<!-- 數(shù)據(jù)庫表主鍵 可以不設(shè)置 -->
<!-- <generatedKey column="id" sqlStatement="Mysql" identity="true"/> -->
</table>
</context>
</generatorConfiguration>
運(yùn)行生成文件
這里推薦使用IDEA
我們只需要找到右邊側(cè)欄中的maven
依次找到 plugins–>mybatis-generaltor–>mybatis-generaltor:generaltor
之后雙擊即可,此時(shí)刷新一下項(xiàng)目就自動生成我們想要的,mapper和xml以及pojo

提示 BUILD SUCCESS 即生成成功!
[INFO] Scanning for projects... [INFO] [INFO] [INFO] Building mall 0.0.1-SNAPSHOT [INFO] [INFO] [INFO] [INFO] Connecting to the Database [INFO] Introspecting table mall_user [INFO] Introspecting table mall_category [INFO] Introspecting table mall_goods [INFO] Introspecting table mall_order [INFO] Introspecting table mall_order_goods [INFO] Introspecting table mall_cart [INFO] Generating Primary Key class for table mall_user [INFO] Generating Record class for table mall_user [INFO] Generating Mapper Interface for table mall_user [INFO] Generating SQL Map for table mall_user [INFO] Generating Record class for table mall_category [INFO] Generating Mapper Interface for table mall_category [INFO] Generating SQL Map for table mall_category [INFO] Generating Record class for table mall_goods [INFO] Generating Mapper Interface for table mall_goods [INFO] Generating SQL Map for table mall_goods [INFO] Generating Primary Key class for table mall_order [INFO] Generating Record class for table mall_order [INFO] Generating Mapper Interface for table mall_order [INFO] Generating SQL Map for table mall_order [INFO] Generating Record class for table mall_order_goods [INFO] Generating Mapper Interface for table mall_order_goods [INFO] Generating SQL Map for table mall_order_goods [INFO] Generating Record class for table mall_cart [INFO] Generating Mapper Interface for table mall_cart [INFO] Generating SQL Map for table mall_cart [INFO] Saving file UserMapper.xml [INFO] Saving file CategoryMapper.xml [INFO] Saving file GoodsMapper.xml [INFO] Saving file OrderMapper.xml [INFO] Saving file OrderGoodsMapper.xml [INFO] Saving file CartMapper.xml [INFO] Saving file UserKey.java [INFO] Saving file User.java [INFO] Saving file UserMapper.java [INFO] Saving file Category.java [INFO] Saving file CategoryMapper.java [INFO] Saving file Goods.java [INFO] Saving file GoodsMapper.java [INFO] Saving file OrderKey.java [INFO] Saving file Order.java [INFO] Saving file OrderMapper.java [INFO] Saving file OrderGoods.java [INFO] Saving file OrderGoodsMapper.java [INFO] Saving file Cart.java [INFO] Saving file CartMapper.java [INFO] [INFO] BUILD SUCCESS [INFO] [INFO] Total time: 2.193 s [INFO] Finished at: 2021-06-01T22:33:37+08:00 [INFO]

配置 mybatis 數(shù)據(jù)源及 mappers 路徑
編輯配置文件 /src/main/resources/application.properties
# 數(shù)據(jù)源名稱 spring.datasource.name=spring_boot_mall # 數(shù)據(jù)庫連接 url spring.datasource.url=jdbc:mysql://172.17.0.2:3306/spring-boot-mall?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSl=false;serverTimezone=Asia/Shanghai # 數(shù)據(jù)庫驅(qū)動類 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 用戶名 spring.datasource.username=root # 密碼 spring.datasource.password=000 # 設(shè)置Mybatis Mapper文件路徑 mybatis.config-location=classpath:mappers/*.xml
注意事項(xiàng)
報(bào)錯(cuò) dao類無法找到
需要在spring-boot 入口類/src/main/java/top/orginly/mall/MallApplication.java中添加注解@MapperScan(basePackages = "top.orginly.mall.model.dao")
指向mapper對應(yīng)的類
@SpringBootApplication
@MapperScan(basePackages = "top.orginly.mall.model.dao")
public class MallApplication {
public static void main(String[] args) {
SpringApplication.run(MallApplication.class, args);
}
}Service 實(shí)體類無法自動載入
類添加 @Service 注解后 出現(xiàn)找不到 Mapper 類無法找到

報(bào)錯(cuò)但不影響程序正常運(yùn)行但我們要解決掉,需要給 Mapper 添加 @Repository 注解告訴 IDEA 這是一個(gè)資源類

重新生成后運(yùn)行出錯(cuò)
必須先刪除 model 包和 mapper文件?。?!
到此這篇關(guān)于SpringBoot如何整合mybatis-generator-maven-plugin 1.4.0的文章就介紹到這了,更多相關(guān)SpringBoot整合mybatis-generator-maven-plugin 1.4.0內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA下載并大學(xué)生edu郵箱認(rèn)證免費(fèi)使用教程(圖文)
這篇文章主要介紹了IDEA下載并大學(xué)生edu郵箱認(rèn)證免費(fèi)使用教程(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄赼pache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解二)
這篇文章主要介紹了使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解二)的相關(guān)資料,需要的朋友可以參考下2016-03-03
spring?Cloud微服務(wù)阿里開源TTL身份信息的線程間復(fù)用
這篇文章主要為大家介紹了spring?Cloud微服務(wù)中使用阿里開源TTL身份信息的線程間復(fù)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對象接收
這篇文章主要介紹了Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對象接收,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Java 創(chuàng)建線程的兩個(gè)方法詳解及實(shí)例
這篇文章主要介紹了Java 創(chuàng)建線程的兩個(gè)方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
Springboot中@Transactional注解與異常處理機(jī)制方式
這篇文章主要介紹了Springboot中@Transactional注解與異常處理機(jī)制方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

