MyBatisPlus(簡稱MP)是基於MyBatis框架基礎上開發的增強型工具,旨在簡化開發、提高效率 - 無侵入:只做增強不做改變,不會對現有工程產生影響 - 強大的 CRUD 操作:內置通用 Mapper,少量配置即可實現單表CRUD 操作 - 支持 Lambda:編寫查詢條件無需擔心欄位寫錯... ...
一、MyBatisPlus簡介
1. 入門案例
問題導入
MyBatisPlus環境搭建的步驟?
1.1 SpringBoot整合MyBatisPlus入門程式
①:創建新模塊,選擇Spring初始化,並配置模塊相關基礎信息
②:選擇當前模塊需要使用的技術集(僅保留JDBC)
③:手動添加MyBatisPlus起步依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
註意事項1:由於mp並未被收錄到idea的系統內置配置,無法直接選擇加入
註意事項2:如果使用Druid數據源,需要導入對應坐標
④:製作實體類與表結構
(類名與表名對應,屬性名與欄位名對應)
create database if not exists mybatisplus_db character set utf8;
use mybatisplus_db;
CREATE TABLE user (
id bigint(20) primary key auto_increment,
name varchar(32) not null,
password varchar(32) not null,
age int(3) not null ,
tel varchar(32) not null
);
insert into user values(null,'tom','123456',12,'12345678910');
insert into user values(null,'jack','123456',8,'12345678910');
insert into user values(null,'jerry','123456',15,'12345678910');
insert into user values(null,'tom','123456',9,'12345678910');
insert into user values(null,'snake','123456',28,'12345678910');
insert into user values(null,'張益達','123456',22,'12345678910');
insert into user values(null,'張大炮','123456',16,'12345678910');
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
//自行添加getter、setter、toString()等方法
}
⑤:設置Jdbc參數(application.yml)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC
username: root
password: root
⑥:定義數據介面,繼承BaseMapper
package com.itheima.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<User> {
}
⑦:測試類中註入dao介面,測試功能
package com.itheima;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class Mybatisplus01QuickstartApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
List<User> userList = userDao.selectList(null);
System.out.println(userList);
}
}
2. MyBatisPlus概述
問題導入
通過入門案例製作,MyBatisPlus的優點有哪些?
2.1 MyBatis介紹
-
MyBatisPlus(簡稱MP)是基於MyBatis框架基礎上開發的增強型工具,旨在簡化開發、提高效率
-
官網:https