1.什么是ActiveRecord(AR)?
ActiveRecord 是什么:
- 每一個數據庫表應該對應創建一個實體類,類的每一個對象的實例對應于數據庫中表的一行記錄; 通常表的每個字段在類中都有相應的方法Field;
- ActiveRecord 負責把自己持久化. 在 ActiveRecord 中封裝了對數據庫的訪問,通過對象自己實現 CRUD,實現優雅的數據庫操作。
- ActiveRecord 也封裝了部分業務邏輯??梢宰鳛闃I務對象使用。
2.通過AR實現CRUD
首先創建一張表。
創建一個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>
在核心配置文件中,配置數據庫相關的連接信息。
- #配置數據庫的相關連接信息
- 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
- #配置對應的日志信息
- mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
創建一個實體類,要使用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類中提供了數據庫相關的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類的源碼中看到,這其中定義了大量關于CRUD操作的方法。
創建一個mapper接口。這里雖然不使用 mapper,但也需要定義這個它,MP 通過 mapper 獲取到表的結構;不定義時,MP 報錯無法獲取表的結構信息。
- 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下的相關注解。
- 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);
- //調用實體類對象自己的方法,完成對象自身到數據庫的添加操作
- boolean flag=dept.insert();
- System.out.println("insert的結果:" + flag);
- }
1.2 update
- @Test
- void testDeptUpdate() {
- Dept dept=new Dept();
- dept.setId(1);
- dept.setName("研發部");
- dept.setMobile("99999999999");
- dept.setManager(2);
- //調用實體類對象自己的方法,完成對象自身到數據庫的更新操作
- boolean flag=dept.updateById();
- System.out.println("update的結果:" + flag);
- }
1.3 delete
- @Test
- void testDeptDelete() {
- Dept dept=new Dept();
- boolean result = dept.deleteById(2);
- System.out.println("delete的結果:" + result);
- }
- @Test
- void testDeptDelete2() {
- Dept dept=new Dept();
- dept.setId(2);
- boolean result = dept.deleteById();
- System.out.println("delete的結果:" + result);
- }
1.4 select
- @Test
- void testSelect() {
- Dept dept=new Dept();
- dept.setId(3);
- Dept dept1 = dept.selectById();
- System.out.println("select的結果:" + dept1);
- }
- @Test
- void testSelect2() {
- Dept dept=new Dept();
- Dept dept1 = dept.selectById(3);
- System.out.println("select的結果:" + dept1);
- }
- @Test
- void testSelect3() {
- Dept dept=new Dept();
- List<Dept> deptList=dept.selectAll();
- deptList.forEach( dept1 -> {
- System.out.println(dept1);
- });
- }
到此這篇關于MyBatis-Plus使用ActiveRecord(AR)實現CRUD的文章就介紹到這了,更多相關MyBatis-Plus實現CRUD內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_43823808/article/details/118405197