Active Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的 一个表,而模型类的一个实例对应表中的一行记录。
ActiveRecord 一直广受动态语言( PHP 、 Ruby 等)的喜爱,而 Java 作为准静态语言, 对于 ActiveRecord 往往只能感叹其优雅,所以 MyBatisPlus也在 AR 道路上进行了一定的探索 。
1、需要让实体类继承 Model 类且实现主键指定方法
public class Employee extends Model {
//..fields
//..getter and setter
/**
* 指定当前实体类的主键属性
*/
@Override
protected Serializable pkVal() {
return id;
}
}
测试新增:
@Test
public void testARInsert() {
Employee employee = new Employee();
employee.setLastName("宋老师");
employee.setEmail("[email protected]");
employee.setGender(1);
employee.setAge(35);
boolean result = employee.insert();
System.out.println("result:" +result );
}
控制台输出:
Preparing: INSERT INTO tbl_employee ( last_name, email, gender, age ) VALUES ( ?, ?, ?, ? )
Parameters: 宋老师(String), [email protected](String), 1(Integer), 35(Integer)
Updates: 1
result:true
测试修改:
@Test
public void testARUpdate() {
Employee employee = new Employee();
employee.setId(2);
employee.setLastName("宋老湿");
employee.setEmail("[email protected]");
employee.setGender(1);
employee.setAge(36);
boolean result = employee.updateById();
System.out.println("result:" +result );
}
控制台输出:
Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=?, age=? WHERE id=?
Parameters: 宋老湿(String), [email protected](String), 1(Integer), 36(Integer), 2(Integer)
Updates: 1
result:true
测试查询:
@Test
public void testARSelect() {
Employee employee = new Employee();
Employee result = employee.selectById(14);
System.out.println("result: " +result );
employee.setId(14);
Employee result1 = employee.selectById();
System.out.println("result: " +result1 );
List emps = employee.selectAll();
System.out.println(emps);
List emps1=
employee.selectList(new EntityWrapper().like("last_name", "老师"));
System.out.println(emps1);
Integer result2 = employee.selectCount(new EntityWrapper().eq("gender", 0));
System.out.println("result: " +result2 );
}
控制台输出:
selectById(14)方法:
Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE id=?
Parameters: 14(Integer)
Total: 0
result: null
selectById()方法:
Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE id=?
Parameters: 14(Integer)
Total: 0
result: null
selectAll()方法:
Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee
Parameters:
Total: 4
[Employee [id=2, lastName=宋老湿, [email protected], gender=1, age=36], Employee [id=7, lastName=王五, [email protected], gender=0, age=null], Employee [id=8, lastName=宋老师, [email protected], gender=1, age=35], Employee [id=9, lastName=宋老师, [email protected], gender=1, age=35]]
selectList方法:
Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (last_name LIKE ?)
Parameters: %老师%(String)
Total: 2
[Employee [id=8, lastName=宋老师, [email protected], gender=1, age=35], Employee [id=9, lastName=宋老师, [email protected], gender=1, age=35]]
selectCount方法:
Preparing: SELECT COUNT(1) FROM tbl_employee WHERE (gender = ?)
Parameters: 0(Integer)
Total: 1
result: 1
测试删除:
/**
* 注意: 删除不存在的数据 逻辑上也是属于成功的.
*/
@Test
public void testARDelete() {
Employee employee = new Employee();
boolean result = employee.deleteById(2);
System.out.println("result:" +result );
employee.setId(2);
boolean result1 = employee.deleteById();
System.out.println("result:" +result1 );
boolean result3 = employee.delete(new EntityWrapper().like("last_name", "小"));
System.out.println("result:" +result3 );
}
控制台输出:
deleteById(2)方法:
Preparing: DELETE FROM tbl_employee WHERE id=?
Parameters: 2(Integer)
result:true
deleteById()方法:
Preparing: DELETE FROM tbl_employee WHERE id=?
Parameters: 2(Integer)
Updates: 0
result:true
delete方法:
Preparing: DELETE FROM tbl_employee WHERE (last_name LIKE ?)
Parameters: %小%(String)
Updates: 0
result:true
测试分页:
@Test
public void testARPage() {
Employee employee = new Employee();
Page page = employee.selectPage(new Page<>(1, 1),
new EntityWrapper().like("last_name", "老"));
List emps = page.getRecords();
System.out.println(emps);
}
控制台输出:
Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee WHERE (last_name LIKE ?)
Parameters: %老%(String)
[Employee [id=8, lastName=宋老师, [email protected], gender=1, age=35]]