MyBatis-Plus使用ActiveRecord(AR)實現(xiàn)CRUD
1.什么是ActiveRecord(AR)?
ActiveRecord 是什么:
- 每一個數(shù)據(jù)庫表應(yīng)該對應(yīng)創(chuàng)建一個實體類,類的每一個對象的實例對應(yīng)于數(shù)據(jù)庫中表的一行記錄; 通常表的每個字段在類中都有相應(yīng)的方法Field;
- ActiveRecord 負責(zé)把自己持久化. 在 ActiveRecord 中封裝了對數(shù)據(jù)庫的訪問,通過對象自己實現(xiàn) CRUD,實現(xiàn)優(yōu)雅的數(shù)據(jù)庫操作。
- ActiveRecord 也封裝了部分業(yè)務(wù)邏輯??梢宰鳛闃I(yè)務(wù)對象使用。
2.通過AR實現(xiàn)CRUD
首先創(chuàng)建一張表。

創(chuàng)建一個SpringBoot工程,在pom文件中添加依賴。
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
在核心配置文件中,配置數(shù)據(jù)庫相關(guān)的連接信息。
#配置數(shù)據(jù)庫的相關(guān)連接信息 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=12345678 #配置對應(yīng)的日志信息 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
創(chuàng)建一個實體類,要使用AR,那么實體類就必須繼承MP框架中的Model這個類。
package com.szh.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
/**
* 使用AR,要求實體類必須繼承MP框架中的Model類
* Model類中提供了數(shù)據(jù)庫相關(guān)的CRUD操作
*/
public class Dept extends Model<Dept> {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String name;
private String mobile;
private Integer manager;
//getter and setter
//toString
}
可以從Model類的源碼中看到,這其中定義了大量關(guān)于CRUD操作的方法。

創(chuàng)建一個mapper接口。這里雖然不使用 mapper,但也需要定義這個它,MP 通過 mapper 獲取到表的結(jié)構(gòu);不定義時,MP 報錯無法獲取表的結(jié)構(gòu)信息。
package com.szh.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.szh.mybatisplus.entity.Dept;
/**
*
*/
public interface DeptMapper extends BaseMapper<Dept> {
}
在SpringBoot項目的啟動入口類上方,添加@MapperScan注解,確??梢話呙璧組yBatis、MP下的相關(guān)注解。
package com.szh.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(value = "com.szh.mybatisplus.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1.1 insert
@Test
void testDeptInsert() {
Dept dept=new Dept();
dept.setName("銷售部");
dept.setMobile("12345678900");
dept.setManager(1);
//調(diào)用實體類對象自己的方法,完成對象自身到數(shù)據(jù)庫的添加操作
boolean flag=dept.insert();
System.out.println("insert的結(jié)果:" + flag);
}


1.2 update
@Test
void testDeptUpdate() {
Dept dept=new Dept();
dept.setId(1);
dept.setName("研發(fā)部");
dept.setMobile("99999999999");
dept.setManager(2);
//調(diào)用實體類對象自己的方法,完成對象自身到數(shù)據(jù)庫的更新操作
boolean flag=dept.updateById();
System.out.println("update的結(jié)果:" + flag);
}


1.3 delete
@Test
void testDeptDelete() {
Dept dept=new Dept();
boolean result = dept.deleteById(2);
System.out.println("delete的結(jié)果:" + result);
}

@Test
void testDeptDelete2() {
Dept dept=new Dept();
dept.setId(2);
boolean result = dept.deleteById();
System.out.println("delete的結(jié)果:" + result);
}

1.4 select
@Test
void testSelect() {
Dept dept=new Dept();
dept.setId(3);
Dept dept1 = dept.selectById();
System.out.println("select的結(jié)果:" + dept1);
}

@Test
void testSelect2() {
Dept dept=new Dept();
Dept dept1 = dept.selectById(3);
System.out.println("select的結(jié)果:" + dept1);
}

@Test
void testSelect3() {
Dept dept=new Dept();
List<Dept> deptList=dept.selectAll();
deptList.forEach( dept1 -> {
System.out.println(dept1);
});
}

到此這篇關(guān)于MyBatis-Plus使用ActiveRecord(AR)實現(xiàn)CRUD的文章就介紹到這了,更多相關(guān)MyBatis-Plus實現(xiàn)CRUD內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的SpringMVC中控制器返回XML數(shù)據(jù)問題
這篇文章主要介紹了Java的SpringMVC中控制器返回XML數(shù)據(jù)問題,控制器是處理HTTP請求的組件,它們接收來自客戶端的請求,并將其轉(zhuǎn)換為適當(dāng)?shù)捻憫?yīng),這些響應(yīng)可以是動態(tài)生成的?HTML?頁面,也可以是JSON或XML格式的數(shù)據(jù),需要的朋友可以參考下2023-07-07
Java多線程Callable和Future接口區(qū)別
這篇文章主要介紹了Java多線程Callable和Future接口區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
SpringKafka消息發(fā)布之KafkaTemplate與事務(wù)支持功能
通過本文介紹的基本用法、序列化選項、事務(wù)支持、錯誤處理和性能優(yōu)化技術(shù),開發(fā)者可以構(gòu)建高效可靠的Kafka消息發(fā)布系統(tǒng),事務(wù)支持特性尤為重要,它確保了在分布式環(huán)境中的數(shù)據(jù)一致性,感興趣的朋友一起看看吧2025-04-04
詳解spring boot jpa整合QueryDSL來簡化復(fù)雜操作
這篇文章主要介紹了詳解spring boot jpa整合QueryDSL來簡化復(fù)雜操作,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Java實現(xiàn)的簡單字符串反轉(zhuǎn)操作示例
這篇文章主要介紹了Java實現(xiàn)的簡單字符串反轉(zhuǎn)操作,結(jié)合實例形式分別描述了java遍歷逆序輸出以及使用StringBuffer類的reverse()方法兩種字符串反轉(zhuǎn)操作技巧,需要的朋友可以參考下2018-08-08
Spring?Security中如何獲取AuthenticationManager對象
有時需要使用AuthenticationManager(以下簡稱Manager)對象,可是這個對象不是Bean,沒有直接保存在Spring的Bean庫中,那么如何獲取Spring Security中的這個對象呢,需要的朋友可以參考下2022-11-11
mybatis plus實體類中字段映射mysql中的json格式方式
這篇文章主要介紹了mybatis plus實體類中字段映射mysql中的json格式方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring @ComponentScan注解使用案例詳細講解
@ComponentScan注解的作用可以簡述為:將項目中所有被@Component注解直接或者間接標記的類---->組裝成BeanDefinition---->然后以key=beanName, value=BeanDefinition的形式存儲,為后續(xù)生成bean對象做準備2023-03-03
Java異常繼承結(jié)構(gòu)解析_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java異常繼承結(jié)構(gòu)解析的相關(guān)知識,需要的朋友可以參考下2017-04-04

