Skip to content

Commit 178a58e

Browse files
author
Jarvis
committed
closed #83
1 parent 5b1256b commit 178a58e

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

src/main/java/org/springframework/data/mybatis/repository/support/MybatisRepository.java

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
public interface MybatisRepository<T, ID extends Serializable>
3939
extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
4040

41+
<S extends T> S insert(S entity);
42+
43+
<S extends T> S update(S entity);
44+
4145
@Override
4246
<S extends T> List<S> save(Iterable<S> entities);
4347

src/main/java/org/springframework/data/mybatis/repository/support/SimpleMybatisRepository.java

+30-19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ protected String getNamespace() {
6060
return entityInformation.getJavaType().getName();
6161
}
6262

63+
@Override
64+
public <S extends T> S insert(S entity) {
65+
entityInformation.setCreatedDate(entity);
66+
entityInformation.setCreatedBy(entity);
67+
68+
if (entityInformation.hasVersion()) {
69+
entityInformation.setVersion(entity, 0);
70+
}
71+
72+
73+
insert(STATEMENT_INSERT, entity);
74+
return entity;
75+
}
76+
77+
@Override
78+
public <S extends T> S update(S entity) {
79+
entityInformation.setLastModifiedDate(entity);
80+
entityInformation.setLastModifiedBy(entity);
81+
82+
int row = update(STATEMENT_UPDATE, entity);
83+
if (row == 0) {
84+
throw new MybatisNoHintException("update effect 0 row, maybe version control lock occurred.");
85+
}
86+
if (entityInformation.hasVersion()) {
87+
entityInformation.increaseVersion(entity);
88+
}
89+
return entity;
90+
}
6391

6492
@Override
6593
@Transactional
@@ -69,28 +97,11 @@ public <S extends T> S save(S entity) {
6997
if (entityInformation.isNew(entity)) {
7098
// insert
7199

72-
entityInformation.setCreatedDate(entity);
73-
entityInformation.setCreatedBy(entity);
74-
75-
if (entityInformation.hasVersion()) {
76-
entityInformation.setVersion(entity, 0);
77-
}
78-
79-
80-
insert(STATEMENT_INSERT, entity);
100+
insert(entity);
81101
} else {
82102
// update
83103

84-
entityInformation.setLastModifiedDate(entity);
85-
entityInformation.setLastModifiedBy(entity);
86-
87-
int row = update(STATEMENT_UPDATE, entity);
88-
if (row == 0) {
89-
throw new MybatisNoHintException("update effect 0 row, maybe version control lock occurred.");
90-
}
91-
if (entityInformation.hasVersion()) {
92-
entityInformation.increaseVersion(entity);
93-
}
104+
update(entity);
94105
}
95106

96107
return entity;

template.mf

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Import-Template:
1313
org.slf4j.*;version="${slf4j:[=.=.=,+1.0.0)}",
1414
org.springframework.*;version="${spring:[=.=.=.=,+1.0.0)}",
1515
org.springframework.data.*;version="${springdata.commons.version:[=.=.=.=,+1.0.0)}",
16-
org.apache.ibatis.*;version="3.4.1",
17-
org.mybatis.spring.*;version="1.3.0",
16+
org.apache.ibatis.*;version="3.4.2",
17+
org.mybatis.spring.*;version="1.3.1",
1818
org.joda.time.*;version="1.3.0"

0 commit comments

Comments
 (0)