Skip to content

Commit 3e13d54

Browse files
author
Jarvis
committed
closed #103: NamedQuery support assign operation
1 parent 2a82b82 commit 3e13d54

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/main/java/org/springframework/data/mybatis/repository/annotation/Query.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.lang.annotation.*;
2424

25+
import static org.springframework.data.mybatis.repository.annotation.Query.Operation.unknown;
26+
2527
/**
2628
* Annotated to named query.
2729
*
@@ -50,9 +52,19 @@
5052

5153
boolean basic() default true;
5254

55+
Operation operation() default unknown;
56+
5357
class Unspecified {
5458
}
5559

60+
enum Operation {
61+
insert,
62+
update,
63+
select,
64+
delete,
65+
unknown
66+
}
67+
5668
}
5769

5870

src/main/java/org/springframework/data/mybatis/repository/query/AbstractMybatisQuery.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.core.convert.converter.Converter;
2323
import org.springframework.data.mybatis.repository.annotation.Basic;
2424
import org.springframework.data.mybatis.repository.annotation.Query;
25+
import org.springframework.data.mybatis.repository.annotation.Query.Operation;
2526
import org.springframework.data.mybatis.repository.query.MybatisQueryExecution.*;
2627
import org.springframework.data.repository.query.ParametersParameterAccessor;
2728
import org.springframework.data.repository.query.RepositoryQuery;
@@ -30,6 +31,8 @@
3031
import org.springframework.util.Assert;
3132
import org.springframework.util.StringUtils;
3233

34+
import static org.springframework.data.mybatis.repository.annotation.Query.Operation.unknown;
35+
3336
/**
3437
* abstract mybatis query.
3538
*
@@ -81,6 +84,14 @@ protected Class<?> getStatementReturnType() {
8184
return annotation.returnType();
8285
}
8386

87+
protected Operation getOperation() {
88+
Query annotation = method.getQueryAnnotation();
89+
if (null == annotation) {
90+
return null;
91+
}
92+
return annotation.operation();
93+
}
94+
8495
protected String getStatementId() {
8596
return getNamespace() + "." + getStatementName();
8697
}
@@ -125,6 +136,22 @@ public SqlSessionTemplate getSqlSessionTemplate() {
125136

126137

127138
protected MybatisQueryExecution getExecution() {
139+
Operation operation = getOperation();
140+
if (null != operation && operation != unknown) {
141+
switch (operation) {
142+
case insert:
143+
return new InsertExecution();
144+
case update:
145+
return new UpdateExecution();
146+
case select:
147+
break;
148+
case delete:
149+
return new DeleteExecution();
150+
case unknown:
151+
break;
152+
}
153+
}
154+
128155

129156
if (method.isStreamQuery()) {
130157
return new StreamExecution();

src/main/java/org/springframework/data/mybatis/repository/query/MybatisQueryExecution.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.springframework.util.CollectionUtils;
3131
import org.springframework.util.StringUtils;
3232

33-
import java.util.Collection;
33+
import java.util.Collection;
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
@@ -201,6 +201,50 @@ protected <X> long calculateTotal(Pageable pager, List<X> result) {
201201
return -1;
202202
}
203203
}
204+
static class InsertExecution extends MybatisQueryExecution {
205+
@Override
206+
protected Object doExecute(AbstractMybatisQuery query, Object[] values) {
207+
if (null == values || values.length == 0) {
208+
return query.getSqlSessionTemplate().insert(query.getStatementId());
209+
}
210+
MybatisParameters parameters = query.getQueryMethod().getParameters();
211+
Map<String, Object> parameter = new HashMap<String, Object>();
212+
213+
int c = 0;
214+
for (MybatisParameter param : parameters.getBindableParameters()) {
215+
String name = param.getName();
216+
if (StringUtils.isEmpty(name)) {
217+
name = "p" + c;
218+
}
219+
parameter.put(name, values[param.getIndex()]);
220+
c++;
221+
}
222+
223+
return query.getSqlSessionTemplate().insert(query.getStatementId(), parameter);
224+
}
225+
}
226+
static class UpdateExecution extends MybatisQueryExecution {
227+
@Override
228+
protected Object doExecute(AbstractMybatisQuery query, Object[] values) {
229+
if (null == values || values.length == 0) {
230+
return query.getSqlSessionTemplate().update(query.getStatementId());
231+
}
232+
MybatisParameters parameters = query.getQueryMethod().getParameters();
233+
Map<String, Object> parameter = new HashMap<String, Object>();
234+
235+
int c = 0;
236+
for (MybatisParameter param : parameters.getBindableParameters()) {
237+
String name = param.getName();
238+
if (StringUtils.isEmpty(name)) {
239+
name = "p" + c;
240+
}
241+
parameter.put(name, values[param.getIndex()]);
242+
c++;
243+
}
244+
245+
return query.getSqlSessionTemplate().update(query.getStatementId(), parameter);
246+
}
247+
}
204248

205249
static class SingleEntityExecution extends MybatisQueryExecution {
206250

0 commit comments

Comments
 (0)