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

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

demo

 1 /**
 2      * 修改用户
 3      * @throws SQLException
 4      */
 5     @Test
 6     public void update() throws SQLException {
 7 
 8         Employee employee = new Employee();
 9 
10         employee.setLastName("Marry");
11         employee.setAge(12);
12         employee.setEmail("marry@163.com");
13         employee.setGender(1);
14 
15         Integer result = employeeMapper.updateById(employee);
16 
17 
18         if (result!=null||result>0) {
19             logger.info("++++++++++++++++修改成功+++++");
20         }
21     }
View Code

1、根据Id修改字段内容

 1  /**
 2      * 修改用户
 3      * @throws SQLException
 4      */
 5     @Test
 6     public void update() throws SQLException {
 7 
 8         Employee employee = new Employee();
 9 
10         employee.setId(1);
11         employee.setLastName("Marry");
12         employee.setAge(55);
13         employee.setEmail("marry@163.com");
14         employee.setGender(0);
15 
16         Integer result = employeeMapper.updateById(employee);
17 
18 
19         if (result!=null||result>0) {
20             logger.info("++++++++++++++++修改成功+++++");
21         }
22     }
View Code

2、全表修改一个字段值

 1  /**
 2      * 修改用户
 3      * @throws SQLException
 4      */
 5     @Test
 6     public void update() throws SQLException {
 7 
 8         Employee employee = new Employee();
 9 
10         employee.setId(1);
11         employee.setLastName("Marry");
12         employee.setAge(55);
13         employee.setGender(0);
14 
15 
16         //全表修改一个字段的值
17         Integer result = employeeMapper.update(employee,new UpdateWrapper().set("email","marry@163.com"));
18 
19       
20 
21        
22 
23 
24         if (result!=null||result>0) {
25             logger.info("++++++++++++++++修改成功+++++");
26         }
27     }
View Code

3、判断重复的就修改

 1  /**
 2      * 修改用户
 3      * @throws SQLException
 4      */
 5     @Test
 6     public void update() throws SQLException {
 7 
 8         Employee employee = new Employee();
 9 
10         employee.setId(1);
11         employee.setLastName("Marry");
12         employee.setAge(55);
13         employee.setEmail("marry@163.com");
14         employee.setGender(0);
15 
16         // 判断重复的就修改
17         // UPDATE tbl_employee SET last_name=?, gender=?, age=? WHERE id=? AND last_name=? AND gender=? AND age=?
18         Integer result = employeeMapper.update(employee,new UpdateWrapper().setEntity(employee));
19 
20       
21 
22         if (result!=null||result>0) {
23             logger.info("++++++++++++++++修改成功+++++");
24         }
25     }
View Code

4、自定义sql语句修改

 1  /**
 2      * 修改用户
 3      * @throws SQLException
 4      */
 5     @Test
 6     public void update() throws SQLException {
 7 
 8         Employee employee = new Employee();
 9 
10         employee.setId(1);
11         employee.setLastName("Marry");
12         employee.setAge(55);
13         employee.setEmail("marry@163.com");
14         employee.setGender(0);
15 
16         //拼接一个sql的值
17         //UPDATE tbl_employee SET last_name=?, gender=?, age=?, email=1 where email=1 and age=12(注意where前的空格))
18         Integer result = employeeMapper.update(employee,new UpdateWrapper().setSql("email="+employee.getId()+" where email=1 and age=12"));
19 
20 
21         if (result!=null||result>0) {
22             logger.info("++++++++++++++++修改成功+++++");
23         }
24     }
View Code

注意:

      2.3版本有

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

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

posted @ 2018-09-30 15:28  北极的大企鹅  阅读(4224)  评论(3编辑  收藏  举报
阅读 - 79万