7. ActiveRecord
Active Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。ActiveRecord 一直广受动态语言( PHP 、 Ruby 等)的喜爱,而 Java 作为准静态语言,对于 ActiveRecord 往往只能感叹其优雅,所以 MP 也在 AR 道路上进行了一定的探索,仅仅需要让实体类继承 Model 类且实现主键指定方法,即可开启 AR 之旅(JFinal 也有AR相关的操作)。接下来看具体代码:
7.1. entity
@Data
public class Student extends Model<Student> {
/**
* 主键ID
*/
private String id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
//复写pkVal,返回主键属性
@Override
protected Serializable pkVal() {
return this.id;
}
}
注:实体类继承Model类,重写pkVal方法。
7.2. mapper
public interface StudentMapper extends BaseMapper<Student> {
}
注:虽然AR模式用不到该接口,但是一定要定义,否则使用AR时会报空指针异常。
7.3. 使用AR
7.3.1. AR插入操作
@Test
public void insertTest() {
Student student = new Student();
student.setName("刘武");
student.setAge(5);
System.out.println(student.insert());
}
打印的SQL语句:
INSERT INTO STUDENT ( ID, NAME, AGE ) VALUES ( '201907251416560478106192891', '张三', 5 );
------------------------------------------------------------------------------------------------------------------------
注:可以看到我们并不需要注入mapper接口,不过正如刚才所说,不使用但还是要定义,否则会报错。AR操作是通过对象本身调用相关方法,比如要insert一个user,那就用这个user调用insert方法即可。返回值为布尔类型,由上图可看到返回了true,是操作成功的。
7.3.2. AR删除操作
@Test
public void deleteTest() {
Student student = new Student();
student.setId("7");
System.out.println(student.deleteById());
System.out.println(student.deleteById("8"));
System.out.println(new Student().setId("9").deleteById());
System.out.println(new Student().delete(new QueryWrapper<Student>().lambda().eq(Student::getName, "Sandy")));
}
打印的SQL语句:
DELETE
FROM STUDENT
WHERE ID='7';
------------------------------------------------------------------------------------------------------------------------
DELETE
FROM STUDENT
WHERE ID='8';
------------------------------------------------------------------------------------------------------------------------
DELETE
FROM STUDENT
WHERE ID='9';
------------------------------------------------------------------------------------------------------------------------
DELETE
FROM STUDENT
WHERE NAME = 'Sandy';
------------------------------------------------------------------------------------------------------------------------
注:这里介绍了两种删除方法,代码中已有注释说明。需要注意的是,删除数据库中不存在的数据,结果也是true
。
7.3.3. AR更新操作
@Test
public void updateTest() {
System.out.println(new Student().setId("1").setName("李四").updateById());
System.out.println(new Student().update(new UpdateWrapper<Student>().lambda().set(Student::getAge, 3).eq(Student::getId, 2)));
}
打印的SQL语句:
UPDATE STUDENT SET NAME='李四'
WHERE ID='1';
------------------------------------------------------------------------------------------------------------------------
UPDATE STUDENT SET AGE=3
WHERE ID = 2;
------------------------------------------------------------------------------------------------------------------------
7.3.4. AR查询操作
@Test
public void selectTest() {
System.out.println(new Student().setId("3").selectById().getName());
Student student = new Student().selectOne(new QueryWrapper<Student>().lambda().eq(Student::getId, 2));
System.out.println("Name:"+student.getName());
System.out.println("Age:"+student.getAge());
}
打印的SQL语句:
SELECT ID,NAME,AGE
FROM STUDENT
WHERE ID='3';
------------------------------------------------------------------------------------------------------------------------
SELECT ID,NAME,AGE
FROM STUDENT
WHERE ID = 2;
------------------------------------------------------------------------------------------------------------------------
7.3.5. AR分页查询
@Test
public void pageTest(){
Student user = new Student();
IPage<Student> studentIPage = user.selectPage(new Page<Student>(1, 4),new QueryWrapper<Student>().like("name", "刘"));
List<Student> users = studentIPage.getRecords();
System.out.println(users);
}
打印的SQL语句:
SELECT COUNT(1)
FROM STUDENT
WHERE name LIKE '%刘%';
------------------------------------------------------------------------------------------------------------------------
SELECT ID,NAME,AGE
FROM STUDENT
WHERE name LIKE '%刘%'
LIMIT 0,4;
------------------------------------------------------------------------------------------------------------------------