public class MybatisplusARTest{
@Test
public void insert(){
User user = new User();
user.setName("一点红");
user.setAge(27);
user.setEmail("877673096@qq.com");
user.setManagerId(1000001);
boolean insert = user.insert(user);
System.out.println("insert = " + insert);
}
@Test
public void selectById(){
User user = new User();
User userSelect = user.selectById(10005);
System.out.println(userSelect);
}
@Test
public void selectById2(){
User user = new User();
user.setId(10005);
User userSelect = user.selectById();
System.out.println(userSelect);
}
@Test
public void updateById(){
User user = new User();
user.setId(10005);
user.setName("雪花飘");
boolean userUpdate = user.updateById();
System.out.println(userUpdate);
}
@Test
public void deleteById(){
User user = new User();
user.setId(10005);
boolean userDelete = user.deleteById();
System.out.println(userDelete);
}
@Test
public void insertOrUpdate(){
User user = new User();
user.setName("花儿红");
user.setAge(27);
user.setEmail("877673096@qq.com");
user.setManagerId(1000001);
//如果不设置Id,就是insert,如果设置了Id,就会先查询,存在就update,不存在就insert
boolean insertOrUpdate = user.insertOrUpdate();
System.out.println(insertOrUpdate);
}
}
实现ActionRecord模式
1、实体类必须继承 Model<T> 对象
2、必须是先对象的BaseMapper<T>接口
AR模式:insert update
AR模式:Insert update
AR模式:删除
AR模式:修-改
AR模式:修-改
AR模式:修-
AR模式:修-改
AR模式:查-询2
AR模式:查-询
AR模式:查询
AR模式:保-存-入-库
AR模式:保存入库
AR模式:模型变革
AR模式
AR模式中的这个好像还不错,可以根据有没有Id来决定是更新还是新增
AR模式中的这个好像还不错,可以根据有没有Id来决定是更新还是新增
活动记录,领域模型模式
一个模型类,对应关系型数据库中的一个表
模型类的一个实例,对应表中的一行记录
通过实体类对象,对表进行增删改查操作,方便开发
继承Model<实体类>。加上@EqualsAndHashCode(callSuper=false)
Mapper继承BaseMapper
这里通过让实体类集成Model<实体类>对象,可以开启AR模式,即直接通过对象进行数据库的动态操作。ActiveRecord。这里注意删除逻辑,删除不存在时也算删除成功。
AR模式,即实体操作数据库,前提条件是实体要继续一个对像,如图
insertOrUpdate support
ActiveRecord模式
简介:活动记录,领域模型模式,直接通过实体操作数据库(java的一个实体类对应数据库的一张表,而一个实例对应表中一行记录)
MP中AR模式的实现(前提:mapper接口实现BaseMapper)
insertOrUpdate():如果实体的主键不是null,那么就会先查询,如果有记录就更新,没有就插入。是null直接进行插入。
注意:如下图方法删除不存在的也返回true.
ActiveRecord 模式
insertOrUpdate3 设置的Id不存在
insertOrUpdate2
insertOrUpdate
deleteById
updateById