mybatisX插件的使用及打包成配置
裝mybatisX插件;
idea連接數(shù)據(jù)庫;
點(diǎn)擊mybatisx-generator,設(shè)置自己裝mybatisX插件;
idea連接數(shù)據(jù)庫;
點(diǎn)擊mybatisx-generator,設(shè)置自己要的包和類;
如果要把自己的配置設(shè)置成一個(gè)自定義模板:
1、 使用idea鏈接數(shù)據(jù)庫
要的包和類;
如果要把自己的配置設(shè)置成一個(gè)自定義模板:
一、使用idea鏈接數(shù)據(jù)庫


二、安裝mybatis-X插件
File–>Settings–>Plugins–>Marketplace,=,搜索MyBatisX–>install

三、生成代碼內(nèi)容
3.1 配置本地驅(qū)動(dòng)包



3.2 側(cè)邊欄打開數(shù)據(jù)庫,選擇要生成代碼的表格,在表名上右擊,點(diǎn)擊MybatisX-Generator

3.3 設(shè)置類名生成規(guī)則及生成代碼的路徑
路徑的生成按照物理路徑是: module path --》 base path --》base package
(這里注意如果想實(shí)體類帶Entity后綴,在extra class suffix一欄填上Entity即可)

3.4 生成代碼

template:
- custom-model-swagger:生成實(shí)體文件,屬性上會(huì)自動(dòng)增加swagger的相關(guān)注解,
- default-all:生成實(shí)體文件、xml文件和dao層接口文件, 默認(rèn)會(huì)生成常用的增刪改查到的方法
- mybatis-plus3:生成實(shí)體文件、xml文件、dao層接口文件、 service層接口文件和service層接口實(shí)現(xiàn)文件
3.4.1 mybatis-plus3案例


3.4.2 效果代碼
實(shí)體類:

mapper接口:

service層:


xml文件:

四、配置文件
4.1 默認(rèn)的mybatis-plus3模板
模板文件包含:

4.1.1 .meta.xml以及xx.ftl概覽

從上圖中可以看到,此模板生成了四個(gè)文件,加上我們的entity,一共是5個(gè)。
serviceInterface.ftl:

serviceImpl.ftl:
package ${baseInfo.packageName};
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${tableClass.fullClassName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import ${mapperInterface.packageName}.${mapperInterface.fileName};
<#if baseService??&&baseService!="">
import ${baseService};
<#list baseService?split(".") as simpleName>
<#if !simpleName_has_next>
<#assign serviceSimpleName>${simpleName}</#assign>
</#if>
</#list>
</#if>
import org.springframework.stereotype.Service;
/**
* @author ${author!}
* @description 針對(duì)表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的數(shù)據(jù)庫操作Service實(shí)現(xiàn)
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
*/
@Service
public class ${baseInfo.fileName} extends ServiceImpl<${mapperInterface.fileName}, ${tableClass.shortClassName}>
implements ${serviceInterface.fileName}{
}
mapperInterface.ftl:
package ${mapperInterface.packageName};
import ${tableClass.fullClassName};
<#if tableClass.pkFields??>
<#list tableClass.pkFields as field><#assign pkName>${field.shortTypeName}</#assign></#list>
</#if>
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author ${author!}
* @description 針對(duì)表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的數(shù)據(jù)庫操作Mapper
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
* @Entity ${tableClass.fullClassName}
*/
public interface ${mapperInterface.fileName} extends BaseMapper<${tableClass.shortClassName}> {
}
mapperXml.ftl:

4.2 我的mybatis-plus3模板
要點(diǎn):把service改為repository,作為倉儲(chǔ)層,而不是業(yè)務(wù)層。

.meta.xml:
<?xml version="1.0" encoding="utf-8" ?>
<templates>
<template>
<property name="configName" value="repositoryInterface"/>
<property name="configFile" value="repositoryInterface.ftl"/>
<property name="fileName" value="${domain.fileName}Repository"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.domain.gateway.db"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="repositoryImpl"/>
<property name="configFile" value="repositoryImpl.ftl"/>
<property name="fileName" value="${domain.fileName}RepositoryImpl"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.infrastructure.repository"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="mapperInterface"/>
<property name="configFile" value="mapperInterface.ftl"/>
<property name="fileName" value="${domain.fileName}Mapper"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.infrastructure.repository.mybatis.mapper"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="mapperXml"/>
<property name="configFile" value="mapperXml.ftl"/>
<property name="fileName" value="${domain.fileName}Mapper"/>
<property name="suffix" value=".xml"/>
<property name="packageName" value="mapper"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="src/main/resources"/>
</template>
</templates>
mapperInterface.ftl:略,和4.1相同;
mapperXml.ftl:略,和4.1相同;
repositoryInterface.ftl:
package ${baseInfo.packageName};
import ${tableClass.fullClassName};
<#if baseService??&&baseService!="">
import ${baseService};
<#list baseService?split(".") as simpleName>
<#if !simpleName_has_next>
<#assign serviceSimpleName>${simpleName}</#assign>
</#if>
</#list>
</#if>
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author ${author!}
* @description 針對(duì)表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的數(shù)據(jù)庫操作Service
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
*/
public interface ${baseInfo.fileName} extends IService<${tableClass.shortClassName}> {
}
其實(shí)和4.1也是一樣的,不同的是.meta.xml里的參數(shù)變了:

repositoryImpl.ftl:
package ${baseInfo.packageName};
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${tableClass.fullClassName};
import ${repositoryInterface.packageName}.${repositoryInterface.fileName};
import ${mapperInterface.packageName}.${mapperInterface.fileName};
<#if baseService??&&baseService!="">
import ${baseService};
<#list baseService?split(".") as simpleName>
<#if !simpleName_has_next>
<#assign serviceSimpleName>${simpleName}</#assign>
</#if>
</#list>
</#if>
import org.springframework.stereotype.Service;
/**
* @author ${author!}
* @description 針對(duì)表【${tableClass.tableName}<#if tableClass.remark?has_content>(${tableClass.remark!})</#if>】的數(shù)據(jù)庫操作Service實(shí)現(xiàn)
* @createDate ${.now?string('yyyy-MM-dd HH:mm:ss')}
*/
@Service
public class ${baseInfo.fileName} extends ServiceImpl<${mapperInterface.fileName}, ${tableClass.shortClassName}> implements ${repositoryInterface.fileName}{
}
同上,和4.1也是一樣的,不同的兩點(diǎn):
- 是.meta.xml里的參數(shù)變了;
- import里的導(dǎo)入類全路徑引用變了;


五、打包配置,一鍵給其他人復(fù)用

以我的【mybatis-plus3-20230824】為例:

其他人把壓縮包解壓到對(duì)應(yīng)的位置即可。
到此這篇關(guān)于mybatisX插件的使用及打包成配置的文章就介紹到這了,更多相關(guān)mybatisX插件使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
詳解MyBatis-Plus Wrapper條件構(gòu)造器查詢大全
這篇文章主要介紹了詳解MyBatis-Plus Wrapper條件構(gòu)造器查詢大全,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法
這篇文章主要介紹了Spring異常實(shí)現(xiàn)統(tǒng)一處理的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
SpringBoot中@ComponentScan注解過濾排除不加載某個(gè)類的3種方法
這篇文章主要給大家介紹了關(guān)于SpringBoot中@ComponentScan注解過濾排除不加載某個(gè)類的3種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07
Java實(shí)現(xiàn)對(duì)字符串中的數(shù)值進(jìn)行排序操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)對(duì)字符串中的數(shù)值進(jìn)行排序操作,涉及java字符串與數(shù)組的相互轉(zhuǎn)換以及數(shù)組排序相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
java時(shí)間和字符串之間相互轉(zhuǎn)換幾種方法
這篇文章主要介紹了java時(shí)間和字符串之間相互轉(zhuǎn)換的幾種方法,還詳細(xì)解釋了DateTimeFormatter的三種解析模式,并比較了yyyy和uuuu在嚴(yán)格模式下的區(qū)別,需要的朋友可以參考下2025-03-03

