最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus使用ActiveRecord(AR)實(shí)現(xiàn)CRUD

 更新時(shí)間:2021年07月05日 15:31:51   作者:張起靈-小哥  
本文將結(jié)合實(shí)例代碼,介紹MyBatis-Plus使用ActiveRecord(AR)實(shí)現(xiàn)CRUD,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1.什么是ActiveRecord(AR)?

ActiveRecord 是什么:

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

2.通過(guò)AR實(shí)現(xiàn)CRUD

首先創(chuàng)建一張表。

創(chuàng)建一個(gè)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ù)庫(kù)相關(guān)的連接信息。

#配置數(shù)據(jù)庫(kù)的相關(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
 
#配置對(duì)應(yīng)的日志信息
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

創(chuàng)建一個(gè)實(shí)體類,要使用AR,那么實(shí)體類就必須繼承MP框架中的Model這個(gè)類。

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,要求實(shí)體類必須繼承MP框架中的Model類
 * Model類中提供了數(shù)據(jù)庫(kù)相關(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)建一個(gè)mapper接口。這里雖然不使用 mapper,但也需要定義這個(gè)它,MP 通過(guò) mapper 獲取到表的結(jié)構(gòu);不定義時(shí),MP 報(bào)錯(cuò)無(wú)法獲取表的結(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項(xiàng)目的啟動(dòng)入口類上方,添加@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í)體類對(duì)象自己的方法,完成對(duì)象自身到數(shù)據(jù)庫(kù)的添加操作
        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í)體類對(duì)象自己的方法,完成對(duì)象自身到數(shù)據(jù)庫(kù)的更新操作
        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)實(shí)現(xiàn)CRUD的文章就介紹到這了,更多相關(guān)MyBatis-Plus實(shí)現(xiàn)CRUD內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

万全县| 股票| 娄底市| 永川市| 通城县| 木里| 三明市| 塘沽区| 长宁区| 金乡县| 伊通| 霞浦县| 洛宁县| 招远市| 红河县| 东城区| 定兴县| 阿合奇县| 宜丰县| 双柏县| 榆中县| 阿克苏市| 锦州市| 凉山| 仁布县| 隆子县| 西和县| 灵山县| 句容市| 卢湾区| 观塘区| 大足县| 密山市| 延长县| 罗甸县| 云龙县| 绥化市| 唐山市| 沁阳市| 五寨县| 江安县|