MyBatis Plus Mapper CRUD接口测试
插入
插入一条记录
int insert(T entity);
删除
根据主键ID删除,主键ID支持String、Number类型
int deleteById(Serializable id);
根据主键ID批量删除
int deleteBatchIds(Collection<? extends Serializable> idList);
根据条件进行删除。原理为SQL语句拼接,因此Map中需要传输数据库字段列
实质为 delete from 表 where 【key1 = value1】 AND 【key2 = value2】
【key1-value1】【key2-value2】为Map的键值对
int deleteByMap(Map<String, Object> columnMap);
更新
根据ID更新。null值忽略。
Employee employee = new Employee();
employee.setId(7);
employee.setLastName("a");
employee.setAge(23);
employee.setEmail(null);
employee.setGender(null);
mapper.updateById(employee);
UPDATE tb_employee SET last_name='a', age=23
WHERE id=7;
根据条件更新,null值忽略。
Employee employee = new Employee();
employee.setId(7L);
employee.setLastName("c");
//这里设置的null,就是说不操作email字段
employee.setEmail(null);
employee.setGender(null);
employee.setAge(23);
mapper.update(employee, new UpdateWrapper<Employee>().lambda()
.eq(Employee::getId, 7)
);
UPDATE tb_employee SET last_name='c', age=23
WHERE (id = 7);
定制置空。
Employee employee = new Employee();
employee.setId(7L);
employee.setLastName("c");
//这里设置的null,就是说不操作email字段
employee.setEmail(null);
employee.setGender(null);
employee.setAge(23);
mapper.update(employee, new UpdateWrapper<Employee>().lambda()
.eq(Employee::getId, 7)
//这里可以定制置空SQL
.set(Employee::getEmail,null)
.set(Employee::getGender,"")
);
UPDATE tb_employee SET last_name='c', age=23, email=null,gender=''
WHERE (id = 7);