MyBatisPlus-5-ActiveRecord

给你一囗甜甜゛ 提交于 2019-11-29 06:39:38

ActiveRecord(活动记录)

1.1 说明

Active Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。

1.2 使用 AR 模式

让实体类继承 Model 类且实现主键指定方法,即可开启 AR 之旅,例:

public class Employee extends Model<Employee> {
    
    // ...fields

    /**
     * 指定当前实体类的主键属性
     */
    @Override
    protected Serializable pkVal() {
        return id;
    }
}

1.3 基本 CRUD

1.3.1 AR 插入操作

    @Test
    public void testARInsert() {
        Employee employee = new Employee();
        employee.setLastName("宋老师");
        employee.setEmail("sls@163.com");
        employee.setGender(1);
        employee.setAge(35);

        boolean result = employee.insert();
        System.out.println("result:" + result);
    }

1.3.2 AR 修改操作

    @Test
    public void testARUpdate() {
        Employee employee = new Employee();
        employee.setId(7);
        employee.setLastName("宋老湿");
        employee.setEmail("sls@163.com");
        employee.setGender(1);
        employee.setAge(36);

        boolean result = employee.updateById();
        System.out.println("result:" + result);
    }

1.3.3 AR 查询操作

    @Test
    public void testARSelect() {
        Employee employee = new Employee();

        Employee result = employee.selectById(7);
        System.out.println(result);

        employee = new Employee();
        employee.setId(7);
        result = employee.selectById();
        System.out.println(result);

        employee = new Employee();
        List<Employee> emps = employee.selectAll();
        System.out.println(emps);

        emps = employee.selectList(new EntityWrapper<Employee>().like("last_name", "老师"));
        System.out.println(emps);

        int res = employee.selectCount(new EntityWrapper<Employee>().eq("gender", 0));
        System.out.println("result: " + res);
    }

1.3.4 AR 删除操作

    @Test
    public void testARDelete() {
        Employee employee = new Employee();

        boolean result = employee.deleteById(7);
        System.out.println("result:" + result);

        employee = new Employee();
        employee.setId(9);
        result = employee.deleteById();
        System.out.println("result:" + result);

        result = employee.delete(new EntityWrapper<Employee>().like("last_name", "小"));
        System.out.println(result);
    }

注意: 删除不存在的数据 逻辑上也是属于成功的

1.3.5 AR 分页复杂操作

    @Test
    public void testARPage() {
        Employee employee = new Employee();

        Page<Employee> page = employee.selectPage(new Page<>(1, 1),
                new EntityWrapper<Employee>()
                        .like("last_name", "老"));

        List<Employee> emps = page.getRecords();

        System.out.println(emps);
    }

1.4 本质是调用的 Mybatis 对应的方法

Model.class

    ...
    
    public Page<T> selectPage(Page<T> page, Wrapper<T> wrapper) {
        Map<String, Object> map = new HashMap();
        wrapper = SqlHelper.fillWrapper(page, wrapper);
        map.put("ew", wrapper);
        List<T> tl = this.sqlSession().selectList(this.sqlStatement(SqlMethod.SELECT_PAGE), map, page);
        page.setRecords(tl);
        return page;
    }
    
    ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!