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

2022-07-22,,,

目录
  • 1.什么是activerecord(ar)?
  • 2.通过ar实现crud

1.什么是activerecord(ar)?

activerecord 是什么:

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

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注解,确保可以扫描到mybatis、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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《MyBatis-Plus使用ActiveRecord(AR)实现CRUD.doc》

下载本文的Word格式文档,以方便收藏与打印。