MybatisX快速生成增刪改查的方法示例
MybatisX 是一款基于 IDEA 的快速開發(fā)插件,方便在使用mybatis以及mybatis-plus開始時簡化繁瑣的重復(fù)操作,提高開發(fā)速率。注意:idea得用最新的版本才能生效一些功能,我用的是2021.3版本的
1 安裝
file - settings - plugins - mybatisx

2 基本功能
搭建測試項(xiàng)目
- 導(dǎo)入依賴
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.1 xml跳轉(zhuǎn)
添加插件后在dao層會多一只戴紅色頭巾的小鳥,同樣在對應(yīng)xml文件方法前也會對應(yīng)一直戴藍(lán)色頭巾的小鳥,點(diǎn)擊即可在dao和xml文件之間跳轉(zhuǎn)

點(diǎn)擊上面的紅色小鳥可以跳轉(zhuǎn)到
SingerMapper.xml文件

點(diǎn)擊上面的藍(lán)色小鳥可以跳轉(zhuǎn)到
SingerDao.java
2.2 代碼生成
2.2.1 生成.xml中的sql語句頭
以前我們在開發(fā)中寫好接口后,還要到xml中寫對應(yīng)的xml方法,有了MybatisX后只用在dao中寫好對應(yīng)方法后,按Alt+Enter選擇自動生成就能自動在xml中生成對應(yīng)的映射方法

生成結(jié)果:

拓展:entity類建立映射:
<resultMap id="BaseResultMap" type="com.example.mybatisxtest.entity.Singer">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="sex" jdbcType="TINYINT" property="sex"/>
<result column="pic" jdbcType="VARCHAR" property="pic"/>
<result column="birth" jdbcType="TIMESTAMP" property="birth"/>
<result column="location" jdbcType="VARCHAR" property="location"/>
<result column="introduction" jdbcType="VARCHAR" property="introduction"/>
</resultMap>

2.2.2 根據(jù)數(shù)據(jù)庫表,自動生成增刪改查
- 添加數(shù)據(jù)庫
view - tool windows - database

導(dǎo)入數(shù)據(jù)庫:

填寫數(shù)據(jù)源配置:

選中表,右鍵生成

選擇配置(具體需要大家可以自行選擇,沒有就默認(rèn)即可)

生成結(jié)果:

選擇不同template,會得到不同效果
default all:

mybatis-plus2模板:

生成結(jié)果:

2.3 JPA提示和生成語句
MybatisX會根據(jù)實(shí)體字段寫出方法名

根據(jù)方法名生成對應(yīng)SQL

生成結(jié)果:
SingerMapper.java:
List selectByIntroduction(@Param(“introduction”) String introduction);
SingerMapper.xml:
<select id="selectByIntroduction" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from singer
where
introduction = #{introduction,jdbcType=VARCHAR}
</select>
到此這篇關(guān)于MybatisX快速生成增刪改查的方法示例的文章就介紹到這了,更多相關(guān)MybatisX生成增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 中 String format 和Math類實(shí)例詳解
這篇文章主要介紹了java 中 String format 和Math類實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
淺談Spring事務(wù)傳播行為實(shí)戰(zhàn)
這篇文章主要介紹了淺談Spring事務(wù)傳播行為實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java實(shí)現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼
在業(yè)務(wù)中,常常會需要在瀏覽器中預(yù)覽Word文檔,或者需要將Word文檔轉(zhuǎn)成HTML文件保存,本文主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)Word轉(zhuǎn)換成Html的相關(guān)方法,希望對大家有所幫助2024-02-02
Java中CompletableFuture?的詳細(xì)介紹
這篇文章主要介紹了Java中的CompletableFuture,通過創(chuàng)建?CompletableFuture?的對象的工廠方法展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
SpringBoot+Vue前后端分離實(shí)現(xiàn)請求api跨域問題
這篇文章主要介紹了SpringBoot+Vue前后端分離實(shí)現(xiàn)請求api跨域問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

