-
Notifications
You must be signed in to change notification settings - Fork 4
/
SpringSuportCrudUserMapperUpdate.java
169 lines (142 loc) · 4.85 KB
/
SpringSuportCrudUserMapperUpdate.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.javaoffers.base.modelhelper.sample.spring;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.javaoffers.base.modelhelper.sample.spring.constant.Sex;
import com.javaoffers.base.modelhelper.sample.spring.constant.Work;
import com.javaoffers.base.modelhelper.sample.spring.mapper.BriefUserMapper;
import com.javaoffers.base.modelhelper.sample.spring.model.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@SpringBootApplication
@RequestMapping
@MapperScan("com.javaoffers.base.modelhelper.sample.spring.mapper")
public class SpringSuportCrudUserMapperUpdate implements InitializingBean {
ObjectMapper objectMapper = new ObjectMapper();
public static boolean status = true;
@Resource
BriefUserMapper crudUserMapper;
public static void main(String[] args) {
SpringApplication.run(SpringSuportCrudUserMapperUpdate.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
testNpdateNull();
testUpdate();
testBatchUpdate();
if(status){
System.exit(0);
}
}
public void testNpdateNull(){
try {
crudUserMapper.update()
.npdateNull()
.col(User::getName, null)
.where()
.eq(User::getId, 1)
.ex();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
public void testBatchUpdate(){
List<User> users = crudUserMapper.queryAll();
for(User user : users){
user.setSex(Sex.Boy);
}
crudUserMapper.update().npdateNull()
.col(User::getWork, Work.JAVA)
.where()
.eq(User::getId, users.get(0).getId())
.addBatch()
.col(User::getWork, Work.PYTHON)
.where()
.eq(User::getId, users.get(1).getId())
.ex();
}
public void testUpdate() throws JsonProcessingException {
Long id = 109L;
List<User> exs = crudUserMapper.select()
.colAll()
.where()
.isNotNull(User::getId)
.exs();
id = exs.get(0).getId();
crudUserMapper.update()
.npdateNull()
.col(User::getName,"ling")
.where()
.eq(User::getId,id)
.ex();
crudUserMapper.update().npdateNull()
.col(User::getBirthday, new Date())
//name not will update . because its null
.col(User::getName,null)
.where()
.eq(User::getId, id)
.ex();
User user = crudUserMapper.queryUserById(id);
print(user);
crudUserMapper.update()
.updateNull()
.col(User::getBirthday, new Date())
.col(User::getId, id)
.where()
.eq(User::getId,id)
.ex();
user = crudUserMapper.select()
.colAll()
.where()
.eq(User::getId, id)
.ex();
crudUserMapper.update()
.npdateNull()
.colAll(user)
.where()
.eq(User::getId,id)
.ex();
try {
crudUserMapper.update()
.npdateNull()
.colAll(new User())
.where()
.eq(User::getId,id)
.ex();
}catch (Exception e){
print(e.getMessage());
}
crudUserMapper.update()
.npdateNull()
.col(User::getName,"zhou")
.where()
.eq(User::getId,1)
.addBatch()
.colAll(user)
.where()
.eq(User::getId,2)
.ex();
print(user);
}
public void testUpdateUser(){
User user = User.builder().name("Jom").birthday(null).build();
crudUserMapper.update().npdateNull()
.colAll(user)
.where()
.eq(User::getId,1)
.ex();
crudUserMapper.update().updateNull()
.colAll(user)
.where()
.eq(User::getId, 1)
.ex();
}
public void print(Object user) throws JsonProcessingException {
System.out.println(objectMapper.writeValueAsString(user));
}
}