A, ActiveRecord brief
Active Record (the AR for short ) , is a model of the field pattern, characterized by a model class corresponds to a table in a relational database, an instance of the class corresponding to the model of a row of the table. The MP also has some support for the AR.
Open AR in MP, only class inherits the entity class and primary key designated Model implemented method
@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 class inheritance, the inheritance Employee entity can be seen that a method to support a series of CRUD operations
Second, the following are examples of the use 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("[email protected]"); 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("[email protected]"); 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); } }