码迷,mamicode.com
首页 > 其他好文 > 详细

浅谈MyBatis-Plus学习之ActiveRecord

时间:2020-01-10 00:39:03      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:extend   prot   条件查询   条件   system   like   tis   一个   tar   

一、ActiveRecord简单介绍

Active Record(简称AR),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。而MP也对AR有一定支持。
在MP中开启AR,仅需要实体类继承Model类并实现主键指定方法

@TableName("tbl_employee")
public class Employee extends Model<Employee>{
    private Integer id;
    private String lastName;   
    private String email;
    private Integer gender;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", age="
                + age + "]";
    }
    /**
     * 指定实体的主键属性
     */
    @Override
    protected Serializable pkVal() {
        // TODO Auto-generated method stub
        return id;
    }
}

继承Model类后,可以看出Employee实体继承的方法,从而支持一系列的CRUD操作

技术图片

 二、以下是ActiveRecord的使用例子

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class TestActiveRecord {
    @Autowired
    private EmployeeMapper employeeMapper;
    
    /**
     * AR插入操作
     */
    @Test
    public void testARInsert() {
        Employee employee = new Employee();
        employee.setLastName("Mary");
        employee.setEmail("mary@121.com");
        employee.setGender(0);
        employee.setAge(30);
        
        boolean res = employee.insert();
        System.out.println(res);
    } 
    
    /**
     * AR修改操作
     */
    @Test
    public void testARUpdate() {
        Employee employee = new Employee();
        employee.setId(15);
        employee.setLastName("MaryNew");
        employee.setEmail("maryNew@121.com");
        employee.setGender(0);
        
        boolean res = employee.updateById();
        System.out.println(res);
    }
    
    /**
     * AR查询操作
     */
    @Test
    public void testARSelect() {
        Employee employee = new Employee();
        employee.setId(15);
        Employee emp = employee.selectById();
        
        System.out.println(emp);
    }
    
    /**
     * AR查询所有操作
     */
    @Test
    public void testARSelectAll() {
        Employee employee = new Employee();
        List<Employee> emps = employee.selectAll();
        
        System.out.println(emps);
    }
    
    /**
     * AR条件查询操作
     */
    @Test
    public void testARSelectListWrapper() {
        Employee employee = new Employee();
        List<Employee> emps = employee.selectList(new EntityWrapper<Employee>()
                                                  .eq("gender", 0)
                                                  .like("last_name", "a")
                                                 );
        
        System.out.println(emps);
    }
    
    /**
     * AR条件查询数量操作
     */
    @Test
    public void testARSelectCountWrapper() {
        Employee employee = new Employee();
        Integer count = employee.selectCount(new EntityWrapper<Employee>()
                                             .eq("gender", 0)
                                             .like("last_name", "a")
                                            );
        
        System.out.println(count);
    }
    
    /**
     * AR删除操作
     */
    @Test
    public void testARDelete() {
        Employee employee = new Employee();
        employee.setId(9);
        boolean b = employee.deleteById();
        
        System.out.println(b);
    }
    
    /**
     * AR根据条件删除操作
     */
    @Test
    public void testARDeleteWrapper() {
        Employee employee = new Employee();
        boolean b = employee.delete(new EntityWrapper<>()
                        .eq("gender", 1)
                        .like("last_name", "r"));
        
        System.out.println(b);
    }
    
    /**
     * AR根据条件分页操作
     */
    @Test
    public void testARSelectPage() {
        Employee employee = new Employee();
        Page<Employee> page = employee.selectPage(new Page<Employee>(1, 2), 
                            new EntityWrapper<Employee>()
                            .eq("gender", 0));
        List<Employee> records = page.getRecords();
        
        System.out.println("records: " + records);
    }
}

浅谈MyBatis-Plus学习之ActiveRecord

标签:extend   prot   条件查询   条件   system   like   tis   一个   tar   

原文地址:https://www.cnblogs.com/jayhou/p/9824232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!