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

2 基本功能
搭建測試項目
- 導入依賴
<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跳轉
添加插件后在dao層會多一只戴紅色頭巾的小鳥,同樣在對應xml文件方法前也會對應一直戴藍色頭巾的小鳥,點擊即可在dao和xml文件之間跳轉

點擊上面的紅色小鳥可以跳轉到
SingerMapper.xml文件

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

生成結果:

拓展: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

導入數(shù)據(jù)庫:

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

選中表,右鍵生成

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

生成結果:

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

mybatis-plus2模板:

生成結果:

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

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

生成結果:
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>
到此這篇關于MybatisX快速生成增刪改查的方法示例的文章就介紹到這了,更多相關MybatisX生成增刪改查內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java 中 String format 和Math類實例詳解
這篇文章主要介紹了java 中 String format 和Math類實例詳解的相關資料,需要的朋友可以參考下2017-06-06
SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題
這篇文章主要介紹了SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06

