MyBatis-Plus入門Demo詳解

一.簡介:

引用官方文檔(本文主要參考官方文檔示例):

MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。

愿景

我們的愿景是成為 MyBatis 最好的搭檔,就像 魂斗羅中的 1P、2P,基友搭配,效率翻倍。(更多文檔介紹請點擊進入查看)

ZDkKjU.png

二.SpringBoot與MyBatis-plus的整合

這里我們使用SpringBoot引入依賴,當然非SpringBoot項目的引入也是一樣的,為了統一,這里不做過多累述.正如官方所說,mybatis-plus在mybatis的基礎上只做增強不做改變,因此其與spring的整合亦非常簡單。只需把mybatis的依賴換成mybatis-plus的依賴,再把sqlSessionFactory換成mybatis-plus的即可。接下來看具體操作:

1.pom.xml
<?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 http://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.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.hmoe</groupId><artifactId>mybatis-plus-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis-plus-demo</name><description>Demo project for Spring Boot</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></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--druid==>阿里巴巴數據庫連接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.25</version></dependency><!--2)spring dao層依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.14.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.3.14.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.35</version></dependency><!--mp的依賴--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.1.2</version></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.在 application.yml 配置文件中添加 H2 數據庫的相關配置:
# DataSource Config
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/test03username: rootpassword: test# Logger Config
logging:level:com.baomidou.mybatisplus.samples.quickstart: debug
3.數據庫建表語句
DROP TABLE IF EXISTS user;CREATE TABLE user
(id BIGINT(20) NOT NULL COMMENT '主鍵ID',name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',age INT(11) NULL DEFAULT NULL COMMENT '年齡',email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',PRIMARY KEY (id)
);DELETE FROM user;INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
4.編寫實體類 User.java(此處使用了 Lombok 簡化代碼)
@Data
public class User {private Long id;private String name;private Integer age;private String email;
}
5.編寫Mapper類 UserMapper.java
public interface UserMapper extends BaseMapper<User> {}
6.添加測試類,進行功能測試:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {@Autowiredprivate UserMapper userMapper;@Testpublic void testSelect() {System.out.println(("----- selectAll method test ------"));List<User> userList = userMapper.selectList(null);Assert.assertEquals(5, userList.size());userList.forEach(System.out::println);}}

測試結果如下:

ZDniRA.png

轉載于:https://www.cnblogs.com/charlypage/p/11148113.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392800.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392800.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392800.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

RHEL 5基礎篇—常見系統啟動類故障

常見系統啟動類故障 在linux系統的啟動過程中&#xff0c;涉及到MBR主引導記錄、GRUB啟動菜單、系統初始化配置文件inittab等各方面&#xff0c;其中任何一個環節出現故障都有可能會導致系統啟動失敗。因此一定要注意做好相關文件的備份工作。 1、MBR扇區故障 MBR引導記錄位…

hcharts生成圖表

借助hcharts插件&#xff0c;可以很方便地在模板頁面中生成圖表。類似插件還有echarts。 補充。。。 轉載于:https://www.cnblogs.com/Forever77/p/11144346.html

css empty_何時使用:empty和:blank CSS偽選擇器

css emptyI made a terrible mistake when I tweeted about :empty and :blank a while ago. I said that :empty wasn’t useful, and :blank is much more useful than :empty.不久前我在Twitter上發布:empty和:blank時&#xff0c;我犯了一個嚴重的錯誤。 我說過:empty沒用&…

浙江大學計算機系統結構,高級計算機體系結構-浙江大學計算機系統結構室.pdf...

高級計算機體系結構-浙江大學計算機系統結構室高級計算機體系結構陳文智 浙江大學計算機學院chenwzzju.edu.cn2014年9月11.1 計算機技術發展綜述(1)?1946年: 在二次世界大戰期間研制成功的世界上第一臺電子計算機ENIAC(Electronic Numerical Intergrator andCalculator)正式對…

PVS 6.1 Configuring Services Failed

好久沒有更新了&#xff0c;嘿嘿&#xff0c;更新一個。 項目中遇到一個問題&#xff0c;PVS安裝到最后一步報錯&#xff0c;如下圖&#xff1a; 環境&#xff1a;PVS 6.1&#xff0c;數據庫是SQL Server 2005 SP4 查了一下文檔&#xff0c;PVS 6.1支持SQL Server 2005 SP4 排查…

javascript動態創建table

function createTable(parentNode,headres,datas){//創建表格var table document.createElement("table");//將表格追加到父容器中parentNode.appendChild(table);//設置table的樣式table.cellSpacing 0;table.cellPadding 0;table.border "1px";//創建…

leetcode 234. 回文鏈表(快慢指針+鏈表倒置)

請判斷一個鏈表是否為回文鏈表。 示例 1: 輸入: 1->2 輸出: false 示例 2: 輸入: 1->2->2->1 輸出: true 代碼 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val x; }* }*/…

面試小問題——Object中有哪些常用方法?

一、equals方法 Object類中的equals方法用于檢測一個對象是否等于另外一個對象。Java語言規范要求equals方法具有下面的特性&#xff1a; &#xff08;1&#xff09;自反性&#xff1a;對于任何非空引用x&#xff0c;x.equals(x)應該返回true &#xff08;2&#xff09;對稱性&…

職稱計算機證書 評中級職稱,軟考證書如何申請評職稱及職稱申請流程的詳細介紹...

我們很多考友參加軟考。比如信息系統項目管理師和系統集成項目管理工程師考試&#xff0c;目的都是為了評職稱&#xff0c;那么在拿到軟考證書后&#xff0c;很多人最關心的一個問題就是關于職稱評聘問題&#xff0c;今天就以軟考證書如何申請評職稱及職稱申請流程的詳細介紹&a…

播客51:媽媽可以編碼的創始人埃里卡·彼得森(Erica Peterson)

On todays episode of the freeCodeCamp.org podcast, Abbey Rennemeyer chats with Erica Peterson, a founder, entrepreneur, and mother of two who lives and works in Pittsburg, Pennsylvania.在freeCodeCamp.org播客的今天節目中&#xff0c;Abbey Rennemeyer與Erica P…

leetcode 1024. 視頻拼接(dp/貪心)

你將會獲得一系列視頻片段&#xff0c;這些片段來自于一項持續時長為 T 秒的體育賽事。這些片段可能有所重疊&#xff0c;也可能長度不一。 視頻片段 clips[i] 都用區間進行表示&#xff1a;開始于 clips[i][0] 并于 clips[i][1] 結束。我們甚至可以對這些片段自由地再剪輯&am…

java實現時鐘方法匯總

import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask;import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; //第一種比較…

js中注冊標識符流程

注冊分為三個階段&#xff1a;分別是注冊階段&#xff0c;函數處理階段&#xff0c;變量處理階段&#xff1b;這三個階段有先后順序的。&#xff08;注&#xff1a;這三個階段的名字沒有權威性&#xff0c;是作者為了方便記憶自己起的名字&#xff09; 注冊階段的特征 1.此時不…

jsp論壇網站模版_網站關鍵詞優化怎么做

說到網站關鍵詞優化&#xff0c;大多企業都很陌生&#xff0c;建站公司說的關鍵詞優化頭頭是道。跟聽天書似的&#xff0c;51商務網小編為大家總結的網站優化方法希望可以幫到大家&#xff0c;首先要說的是做網站優化第一點就是要有耐心&#xff0c;如果很長時間沒有收錄的話&a…

feature功能_利用feature-u V1釋放基于功能的JS開發的強大功能

feature功能This article is an introduction to a new JS library called feature-u, that facilitates feature-based development in your React project.本文是對新的JS庫(稱為feature-u )的介紹&#xff0c;該庫促進了React項目中基于功能的開發 。 Note: On 8/14/2018 f…

虛擬實驗工場大學計算機實驗報告答案,虛擬實驗實驗報告 - 實驗報告 - 書業網.doc...

虛擬實驗實驗報告 - 實驗報告 - 書業網虛擬實驗實驗報告 - 實驗報告 - 書業網篇一&#xff1a;虛擬實驗報告第一章 文獻綜述1.1 丙酮酸脫氫酶概述丙酮酸脫氫酶復合體(Pyruvate Dehydrogenase Complex)催化丙酮酸不可逆的氧化脫羧轉化成乙酰輔酶A。該復合體是糖酵解的關鍵限速酶…

【筆記】一些linux實用函數技巧【原創】

函數返回的是函數的地址 kallsyms_lookup_name() 本文轉自張昺華-sky博客園博客&#xff0c;原文鏈接&#xff1a;http://www.cnblogs.com/sky-heaven/p/5191491.html&#xff0c;如需轉載請自行聯系原作者

leetcode 845. 數組中的最長山脈

我們把數組 A 中符合下列屬性的任意連續子數組 B 稱為 “山脈”&#xff1a; B.length > 3 存在 0 < i < B.length - 1 使得 B[0] < B[1] < … B[i-1] < B[i] > B[i1] > … > B[B.length - 1] &#xff08;注意&#xff1a;B 可以是 A 的任意子數組…

【Lintcode】018.Subsets II

題目&#xff1a; Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subset…

多線程1

1-1 進程 程序是靜止的&#xff0c;運行中的程序就是進程。進程的三個特征&#xff1a; 動態性 &#xff1a; 進程是運行中的程序&#xff0c;要動態的占用內存&#xff0c;CPU和網絡等資源。獨立性 &#xff1a; 進程與進程之間是相關獨立的&#xff0c;彼此有自己的獨立內存區…