万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • MyBatis

  • Spring-MyBatis

  • MyBatis-Plus

    • MyBaits Plus简介
    • MyBaits Plus快速入门
    • MyBaits Plus CURD
    • MyBaits Plus配置
    • Wrapper条件构造器
    • ActiveRecord
      • 什么是ActiveRecord
      • 快速开始
    • MyBaits Plus插件
    • MyBaits Plus拓展
    • MyBaits Plus代码生成器
  • MyBatis
  • MyBatis-Plus
2022-12-24
目录

ActiveRecord

# ActiveRecord

ActiveRecord(简称AR)⼀直⼴受动态语⾔( PHP 、 Ruby 等)的喜爱,⽽ Java 作为准静态语⾔,对于 ActiveRecord 往往只能感叹其优雅,所以我们也在 AR 道路上进⾏了⼀定的探索,希望⼤家能够喜欢。

# 什么是ActiveRecord

ActiveRecord也属于ORM(对象关系映射)层,由Rails最早提出,遵循标准的ORM模型:表映射到记录,记录映射到对象,字段映射到对象属性。配合遵循的命名和配置惯例,能够很⼤程度的快速实现模型的操作,⽽且简洁易懂。

ActiveRecord的主要思想是:

  • 每⼀个数据库表对应创建⼀个类,类的每⼀个对象实例对应于数据库中表的⼀⾏记录;通常表的每个字段在类中都有相应的Field;
  • ActiveRecord同时负责把⾃⼰持久化,在ActiveRecord中封装了对数据库的访问,即CURD;
  • ActiveRecord是⼀种领域模型(Domain Model),封装了部分业务逻辑;

# 快速开始

在MP中,开启AR⾮常简单,只需要将实体对象继承Model即可。

# 继承Model

@Data
public class User extends Model<User>  {

    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;

}

# 调用CURD方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class MPARCURDTest {

    @Test
    public void testAR() {
        User user = new User();
        user.setName("ar.insert");
        user.setAge(18);
        user.setEmail("mp.insert@baomidou.com");

        // insert
        user.insert();
        // select all
        List<User> users = user.selectAll();
        // selectById
        user.selectById(1L);

        // update
        User updateUser = new User();
        updateUser.setName("mp.updateByConditionAR");
        updateUser.setAge(20);

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", 6);
        updateUser.update(wrapper);
        // ...mp的curd方法都支持
    }


}
上次更新: 5/30/2023, 11:23:39 PM
MyBaits Plus插件

MyBaits Plus插件→

最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式