MyBatis-Plus入门3

update 更新

1
2
3
4
5
6
7
8
9
10
11
12
// 参考:https://mp.baomidou.com/guide/crud-interface.html#update

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereEntity 条件,更新记录
boolean update(T entity, Wrapper<T> updateWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.mp;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.mp.dao.UserMapper;
import com.mp.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;

/**
* @Description
* @auther mohuani
* @create 2019-12-24 23:01
*/

@RunWith(SpringRunner.class)
@SpringBootTest
public class UpdateTest {

@Autowired
private UserMapper userMapper;

@Test
public void updateById() {
User user = new User();
user.setId(1088248166370832385L);
user.setAge(26);
user.setEmail("[email protected]");
int rows = userMapper.updateById(user);
System.out.println("影响记录条数:" + rows);
}

/**
* 通过Wrapper形式
*/
@Test
public void updateByWrapper() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "李艺伟").eq("age", 28);
User user = new User();
user.setEmail("[email protected]");
user.setAge(29);
int rows = userMapper.update(user, updateWrapper);
System.out.println("影响记录条数:" + rows);
}

/**
* 构造where条件的时候直接设置update的内容
*/
@Test
public void updateByWrapper3() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "李艺伟").eq("age", 29).set("age", 30);
User user = new User();
user.setEmail("[email protected]");
user.setAge(29);
int rows = userMapper.update(user, updateWrapper);
System.out.println("影响记录条数:" + rows);
}

/**
* 通过Lambda形式
*/
@Test
public void updateByWrapper4() {
LambdaUpdateWrapper<User> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
lambdaUpdateWrapper.eq(User::getName, "李艺伟").eq(User::getAge, 30).set(User::getAge, 31);

int rows = userMapper.update(null, lambdaUpdateWrapper);
System.out.println("影响记录条数:" + rows);
}

/**
* 通过Lambda形式
*/
@Test
public void updateByWrapper5() {
boolean update = new LambdaUpdateChainWrapper<>(userMapper)
.eq(User::getName, "李艺伟").eq(User::getAge, 31).set(User::getAge, 32).update();
System.out.println(update);
}
}

删除 delete

1
2
3
4
5
6
7
8
9
10
// 参考:https://mp.baomidou.com/guide/crud-interface.html#delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.mp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.mp.dao.UserMapper;
import com.mp.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* @Description
* @auther mohuani
* @create 2019-12-24 23:01
*/

@RunWith(SpringRunner.class)
@SpringBootTest
public class DeleteTest {

@Autowired
private UserMapper userMapper;

@Test
public void deleteById() {
int rows = userMapper.deleteById(1088248166370832385L);
System.out.println("删除记录条数:" + rows);
}

/**
* 构造where条件删除
*/
@Test
public void deleteByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("name", "刘明强5");
columnMap.put("age", 31);

int rows = userMapper.deleteByMap(columnMap);
System.out.println("删除记录条数:" + rows);
}

/**
* 批量删除
*/
@Test
public void deleteBatchIds() {
int rows = userMapper.deleteBatchIds(Arrays.asList(1209754139000852481L, 1209838254358319105L, 1209838329864204289L));
System.out.println("删除记录条数:" + rows);
}


/**
* 通过Lambda形式
*/
@Test
public void updateByWrapper4() {
LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
lambdaQuery.gt(User::getAge, 35).lt(User::getAge, 40);
int rows = userMapper.delete(lambdaQuery);
System.out.println("影响记录条数:" + rows);
}

}


MyBatis-Plus入门3
http://yoursite.com/2016/06/17/Java/MyBatis/MyBatis-Plus入门3/
作者
mohuani
发布于
2016年6月17日
许可协议