Mybatis-Plus 实战完整学习笔记(五)------insert测试

时间:2024-08-22 19:35:44

一、更新操作探究(3.0.3版本)

demo

 /**
* 修改用户
* @throws SQLException
*/
@Test
public void update() throws SQLException { Employee employee = new Employee(); employee.setLastName("Marry");
employee.setAge(12);
employee.setEmail("marry@163.com");
employee.setGender(1); Integer result = employeeMapper.updateById(employee); if (result!=null||result>0) {
logger.info("++++++++++++++++修改成功+++++");
}
}

1、根据Id修改字段内容

  /**
* 修改用户
* @throws SQLException
*/
@Test
public void update() throws SQLException { Employee employee = new Employee(); employee.setId(1);
employee.setLastName("Marry");
employee.setAge(55);
employee.setEmail("marry@163.com");
employee.setGender(0); Integer result = employeeMapper.updateById(employee); if (result!=null||result>0) {
logger.info("++++++++++++++++修改成功+++++");
}
}

2、全表修改一个字段值

  /**
* 修改用户
* @throws SQLException
*/
@Test
public void update() throws SQLException { Employee employee = new Employee(); employee.setId(1);
employee.setLastName("Marry");
employee.setAge(55);
employee.setGender(0); //全表修改一个字段的值
Integer result = employeeMapper.update(employee,new UpdateWrapper().set("email","marry@163.com")); if (result!=null||result>0) {
logger.info("++++++++++++++++修改成功+++++");
}
}

3、判断重复的就修改

  /**
* 修改用户
* @throws SQLException
*/
@Test
public void update() throws SQLException { Employee employee = new Employee(); employee.setId(1);
employee.setLastName("Marry");
employee.setAge(55);
employee.setEmail("marry@163.com");
employee.setGender(0); // 判断重复的就修改
// UPDATE tbl_employee SET last_name=?, gender=?, age=? WHERE id=? AND last_name=? AND gender=? AND age=?
Integer result = employeeMapper.update(employee,new UpdateWrapper().setEntity(employee)); if (result!=null||result>0) {
logger.info("++++++++++++++++修改成功+++++");
}
}

4、自定义sql语句修改

  /**
* 修改用户
* @throws SQLException
*/
@Test
public void update() throws SQLException { Employee employee = new Employee(); employee.setId(1);
employee.setLastName("Marry");
employee.setAge(55);
employee.setEmail("marry@163.com");
employee.setGender(0); //拼接一个sql的值
//UPDATE tbl_employee SET last_name=?, gender=?, age=?, email=1 where email=1 and age=12(注意where前的空格))
Integer result = employeeMapper.update(employee,new UpdateWrapper().setSql("email="+employee.getId()+" where email=1 and age=12")); if (result!=null||result>0) {
logger.info("++++++++++++++++修改成功+++++");
}
}

注意:

2.3版本有

  1. 1)  Integer updateById(@Param("et") T entity); 根据ID修改

  2. 2)  Integer updateAllColumnById(@Param("et") T entity)  不管是否为空,都会出现在slq语句中。默认未设置的字段设置为空