服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - MyBatis-Plus使用ActiveRecord(AR)实现CRUD

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

2021-09-27 09:20张起灵-小哥 Java教程

本文将结合实例代码,介绍MyBatis-Plus使用ActiveRecord(AR)实现CRUD,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧

1.什么是ActiveRecord(AR)?

ActiveRecord 是什么:

  • 每一个数据库表应该对应创建一个实体类,类的每一个对象的实例对应于数据库中表的一行记录; 通常表的每个字段在类中都有相应的方法Field;
  • ActiveRecord 负责把自己持久化. 在 ActiveRecord 中封装了对数据库的访问,通过对象自己实现 CRUD,实现优雅的数据库操作。
  • ActiveRecord 也封装了部分业务逻辑。可以作为业务对象使用。

2.通过AR实现CRUD

首先创建一张表。

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

创建一个SpringBoot工程,在pom文件中添加依赖。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5.  
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11.  
  12. <dependency>
  13. <groupId>mysql</groupId>
  14. <artifactId>mysql-connector-java</artifactId>
  15. <scope>runtime</scope>
  16. <version>5.1.9</version>
  17. </dependency>
  18.  
  19. <dependency>
  20. <groupId>com.baomidou</groupId>
  21. <artifactId>mybatis-plus-boot-starter</artifactId>
  22. <version>3.0.5</version>
  23. </dependency>

在核心配置文件中,配置数据库相关的连接信息。

  1. #配置数据库的相关连接信息
  2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
  4. spring.datasource.username=root
  5. spring.datasource.password=12345678
  6.  
  7. #配置对应的日志信息
  8. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

创建一个实体类,要使用AR,那么实体类就必须继承MP框架中的Model这个类。

  1. package com.szh.mybatisplus.entity;
  2.  
  3. import com.baomidou.mybatisplus.annotation.IdType;
  4. import com.baomidou.mybatisplus.annotation.TableId;
  5. import com.baomidou.mybatisplus.extension.activerecord.Model;
  6.  
  7. /**
  8. * 使用AR,要求实体类必须继承MP框架中的Model类
  9. * Model类中提供了数据库相关的CRUD操作
  10. */
  11. public class Dept extends Model<Dept> {
  12.  
  13. @TableId(value = "id",type = IdType.AUTO)
  14. private Integer id;
  15. private String name;
  16. private String mobile;
  17. private Integer manager;
  18.  
  19. //getter and setter
  20. //toString
  21. }

可以从Model类的源码中看到,这其中定义了大量关于CRUD操作的方法。

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

创建一个mapper接口。这里虽然不使用 mapper,但也需要定义这个它,MP 通过 mapper 获取到表的结构;不定义时,MP 报错无法获取表的结构信息。

  1. package com.szh.mybatisplus.mapper;
  2.  
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import com.szh.mybatisplus.entity.Dept;
  5.  
  6. /**
  7. *
  8. */
  9. public interface DeptMapper extends BaseMapper<Dept> {
  10. }

在SpringBoot项目的启动入口类上方,添加@MapperScan注解,确保可以扫描到MyBatis、MP下的相关注解。

  1. package com.szh.mybatisplus;
  2.  
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
  7. @SpringBootApplication
  8. @MapperScan(value = "com.szh.mybatisplus.mapper")
  9. public class Application {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(Application.class, args);
  13. }
  14.  
  15. }

1.1 insert

  1. @Test
  2. void testDeptInsert() {
  3. Dept dept=new Dept();
  4. dept.setName("销售部");
  5. dept.setMobile("12345678900");
  6. dept.setManager(1);
  7.  
  8. //调用实体类对象自己的方法,完成对象自身到数据库的添加操作
  9. boolean flag=dept.insert();
  10. System.out.println("insert的结果:" + flag);
  11. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

1.2 update

  1. @Test
  2. void testDeptUpdate() {
  3. Dept dept=new Dept();
  4. dept.setId(1);
  5. dept.setName("研发部");
  6. dept.setMobile("99999999999");
  7. dept.setManager(2);
  8.  
  9. //调用实体类对象自己的方法,完成对象自身到数据库的更新操作
  10. boolean flag=dept.updateById();
  11. System.out.println("update的结果:" + flag);
  12. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

1.3 delete

  1. @Test
  2. void testDeptDelete() {
  3. Dept dept=new Dept();
  4. boolean result = dept.deleteById(2);
  5. System.out.println("delete的结果:" + result);
  6. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

  1. @Test
  2. void testDeptDelete2() {
  3. Dept dept=new Dept();
  4. dept.setId(2);
  5. boolean result = dept.deleteById();
  6. System.out.println("delete的结果:" + result);
  7. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

1.4 select

  1. @Test
  2. void testSelect() {
  3. Dept dept=new Dept();
  4. dept.setId(3);
  5. Dept dept1 = dept.selectById();
  6. System.out.println("select的结果:" + dept1);
  7. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

  1. @Test
  2. void testSelect2() {
  3. Dept dept=new Dept();
  4. Dept dept1 = dept.selectById(3);
  5. System.out.println("select的结果:" + dept1);
  6. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

  1. @Test
  2. void testSelect3() {
  3. Dept dept=new Dept();
  4. List<Dept> deptList=dept.selectAll();
  5. deptList.forEach( dept1 -> {
  6. System.out.println(dept1);
  7. });
  8. }

MyBatis-Plus使用ActiveRecord(AR)实现CRUD

到此这篇关于MyBatis-Plus使用ActiveRecord(AR)实现CRUD的文章就介绍到这了,更多相关MyBatis-Plus实现CRUD内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_43823808/article/details/118405197

延伸 · 阅读

精彩推荐