MyBatis-Plus 快速入門(mén)案例(小白教程)
一、引言
學(xué)習(xí)MyBatis-Plus前提需要掌握:數(shù)據(jù)庫(kù)相關(guān)操作、java等相關(guān)知識(shí),最好熟悉Mybatis。
那么本章就來(lái)講解快速搭建MyBatis-Plus開(kāi)發(fā)環(huán)境以及對(duì)數(shù)據(jù)庫(kù)實(shí)際操作。
二、準(zhǔn)備工作
步驟一:使用IDEA快速搭建SpringBoot項(xiàng)目,填寫(xiě)相關(guān)信息即可。

步驟二:引入所需要maven依賴,小編這里有使用lombok依賴,有不了解的小伙伴可以自行學(xué)習(xí)一下,很簡(jiǎn)單的。
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--mybatis plus 啟動(dòng)器--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> <!--mysql 驅(qū)動(dòng)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
步驟三:創(chuàng)建數(shù)據(jù)庫(kù)表,以及對(duì)應(yīng)的實(shí)體類
#創(chuàng)建用戶表
CREATE TABLE user (
id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主鍵',
name VARCHAR(30) DEFAULT NULL COMMENT '姓名',
age INT(11) DEFAULT NULL COMMENT '年齡',
email VARCHAR(50) DEFAULT NULL COMMENT '郵箱',
manager_id BIGINT(20) DEFAULT NULL COMMENT '直屬上級(jí)id',
create_time DATETIME DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
CONSTRAINT manager_fk FOREIGN KEY (manager_id)
REFERENCES user (id)
) ENGINE=INNODB CHARSET=UTF8;
#初始化數(shù)據(jù):
INSERT INTO user (id, name, age, email, manager_id
, create_time)
VALUES (1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL
, '2019-01-11 14:20:20'),
(1088248166370832385, '王天風(fēng)', 25, 'wtf@baomidou.com', 1087982257332887553
, '2019-02-05 11:12:22'),
(1088250446457389058, '李藝偉', 28, 'lyw@baomidou.com', 1088248166370832385
, '2019-02-14 08:31:16'),
(1094590409767661570, '張雨琪', 31, 'zjq@baomidou.com', 1088248166370832385
, '2019-01-14 09:15:15'),
(1094592041087729666, '劉紅雨', 32, 'lhm@baomidou.com', 1088248166370832385
, '2019-01-14 09:48:16');
import lombok.Data;
import java.util.Date;
/**
* @Auther: IT賤男
* @Date: 2019/6/10 14:35
* @Description:這里沒(méi)有Setter、Getter方法是因?yàn)樾【幨褂昧薂Data注解
*/
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
private Long managerId;
private Date createTime;
}
三、實(shí)際操作
以上準(zhǔn)備工作弄好了之后,首先我們需要先連接一個(gè)數(shù)據(jù)庫(kù),采用的是yml格式的。
spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 url: jdbc:mysql://127.0.0.1:3306/mdp?userSSL=false
接下來(lái)我們需要?jiǎng)?chuàng)建一個(gè)User對(duì)象的持久層接口,只需要繼承BaseMapper并且把User對(duì)象傳進(jìn)去即可。
com.example.demo.mapper 這個(gè)是我mapper的包路徑
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.model.User;
/**
* @Auther: IT賤男
* @Date: 2019/6/10 14:40
* @Description: User對(duì)象持久層
*/
public interface UserMapper extends BaseMapper<User> {
}
最后在啟動(dòng)類加上掃描mapper的注解,就可以了。
@SpringBootApplication
@MapperScan("com.example.demo.mapper.**")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
寫(xiě)完就測(cè)試來(lái)一波,在我們springboot測(cè)試類中查詢所有表中的數(shù)據(jù)。
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void contextLoads() {
// 查詢所有數(shù)據(jù)
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
// 數(shù)據(jù)庫(kù)中的數(shù)據(jù)默認(rèn)初始5條,則判斷查詢出來(lái)的集合數(shù)量是否等于5
Assert.assertEquals(5, users.size());
}
}

到此這篇關(guān)于MyBatis-Plus 快速入門(mén)案例(小白教程)的文章就介紹到這了,更多相關(guān)MyBatis-Plus 入門(mén)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java項(xiàng)目中讀取properties文件
本篇文章主要介紹了Java項(xiàng)目中讀取properties文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
springboot如何接收application/x-www-form-urlencoded類型的請(qǐng)求
這篇文章主要介紹了springboot如何接收application/x-www-form-urlencoded類型的請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java中實(shí)現(xiàn)分布式定時(shí)任務(wù)的方法
這篇文章主要介紹了Java中實(shí)現(xiàn)分布式定時(shí)任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Spring 重定向(Redirect)指南及相關(guān)策略問(wèn)題
本文介紹了在Spring中實(shí)現(xiàn)重定向的三種不同方法,在執(zhí)行這些重定向時(shí)如何處理/傳遞屬性以及如何處理HTTP POST請(qǐng)求的重定向。關(guān)于Spring 重定向(Redirect)指南的相關(guān)知識(shí)大家參考下本文2017-11-11
Open-Feign整合hystrix降級(jí)熔斷實(shí)戰(zhàn)記錄
這篇文章主要介紹了Open-Feign整合hystrix降級(jí)熔斷實(shí)戰(zhàn)記錄,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

