Skip to content

Commit 66aa523

Browse files
author
Jarvis
committed
closed #111
1 parent ac5213a commit 66aa523

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public interface MybatisRepository<T, ID extends Serializable>
4242

4343
<S extends T> S update(S entity);
4444

45+
<S extends T> S updateIgnoreNull(S entity);
46+
47+
<S extends T> S saveIgnoreNull(S entity);
48+
4549
@Override
4650
<S extends T> List<S> save(Iterable<S> entities);
4751

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

+38-20
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@
4848
* @author Jarvis Song
4949
*/
5050
public class MybatisSimpleRepositoryMapperGenerator {
51-
private transient static final Logger logger = LoggerFactory.getLogger(MybatisSimpleRepositoryMapperGenerator.class);
52-
private static final String MAPPER_BEGIN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
51+
private transient static final Logger logger = LoggerFactory.getLogger(MybatisSimpleRepositoryMapperGenerator.class);
52+
private static final String MAPPER_BEGIN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
5353
"<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">";
54-
private static final String MAPPER_END = "</mapper>";
54+
private static final String MAPPER_END = "</mapper>";
5555

56-
private final Configuration configuration;
57-
private final Dialect dialect;
58-
private final MybatisMappingContext context;
59-
private final Class<?> domainClass;
56+
private final Configuration configuration;
57+
private final Dialect dialect;
58+
private final MybatisMappingContext context;
59+
private final Class<?> domainClass;
6060
private final MybatisPersistentEntity<?> persistentEntity;
61-
private final MybatisMapperGenerator generator;
61+
private final MybatisMapperGenerator generator;
6262

6363
public MybatisSimpleRepositoryMapperGenerator(Configuration configuration, Dialect dialect, MybatisMappingContext context, Class<?> domainClass) {
6464
this.configuration = configuration;
@@ -132,7 +132,10 @@ private String render() throws IOException {
132132
buildInsertSQL(builder);
133133
}
134134
if (!isStatementExist("_update")) {
135-
buildUpdateSQL(builder);
135+
buildUpdateSQL(builder, "_update", false);
136+
}
137+
if (!isStatementExist("_updateIgnoreNull")) {
138+
buildUpdateSQL(builder, "_updateIgnoreNull", true);
136139
}
137140
if (!isStatementExist("_getById")) {
138141
buildGetById(builder);
@@ -177,11 +180,12 @@ private String render() throws IOException {
177180
return result;
178181
}
179182

180-
private void buildUpdateSQL(final StringBuilder builder) {
183+
184+
private void buildUpdateSQL(final StringBuilder builder, String statementName, final boolean ignoreNull) {
181185
if (!persistentEntity.hasIdProperty()) {
182186
return;
183187
}
184-
builder.append("<update id=\"_update\" parameterType=\"" + domainClass.getName() + "\" lang=\"XML\">");
188+
builder.append("<update id=\"" + statementName + "\" parameterType=\"" + domainClass.getName() + "\" lang=\"XML\">");
185189
builder.append("update ").append(dialect.wrapTableName(persistentEntity.getTableName()));
186190
builder.append("<set>");
187191

@@ -197,12 +201,16 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
197201
builder.append(dialect.wrapColumnName(property.getColumnName())).append("=").append(dialect.wrapColumnName(property.getColumnName())).append("+1,");
198202
return;
199203
}
200-
201-
builder.append("<if test=\"" + property.getName() + " != null\">")
202-
.append(dialect.wrapColumnName(property.getColumnName())).append("=#{").append(property.getName())
204+
if (ignoreNull) {
205+
builder.append("<if test=\"" + property.getName() + " != null\">");
206+
}
207+
builder.append(dialect.wrapColumnName(property.getColumnName())).append("=#{").append(property.getName())
203208
.append(",jdbcType=").append(property.getJdbcType())
204209
.append(null != property.getSpecifiedTypeHandler() ? (",typeHandler=" + property.getSpecifiedTypeHandler().getName()) : "")
205-
.append("}").append(",</if>");
210+
.append("},");
211+
if (ignoreNull) {
212+
builder.append("</if>");
213+
}
206214
}
207215
});
208216

@@ -217,8 +225,13 @@ public void doWithAssociation(Association<? extends PersistentProperty<?>> ass)
217225
@Override
218226
public void doWithPersistentProperty(PersistentProperty<?> pp) {
219227
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
220-
builder.append("<if test=\"" + association.getInverse().getName() + " != null and " + association.getInverse().getName() + "." + property.getName() + " != null\">")
221-
.append(dialect.wrapColumnName(property.getColumnName())).append("=#{").append(association.getInverse().getName()).append(".").append(property.getName()).append("}").append(",</if>");
228+
if (ignoreNull) {
229+
builder.append("<if test=\"" + association.getInverse().getName() + " != null and " + association.getInverse().getName() + "." + property.getName() + " != null\">");
230+
}
231+
builder.append(dialect.wrapColumnName(property.getColumnName())).append("=#{").append(association.getInverse().getName()).append(".").append(property.getName()).append("},");
232+
if (ignoreNull) {
233+
builder.append("</if>");
234+
}
222235
}
223236
});
224237
}
@@ -227,8 +240,13 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
227240

228241
if ((ass instanceof MybatisManyToOneAssociation)) {
229242
MybatisManyToOneAssociation association = (MybatisManyToOneAssociation) ass;
230-
builder.append("<if test=\"" + association.getInverse().getName() + " != null and " + association.getInverse().getName() + "." + association.getObverse().getName() + " != null\">")
231-
.append(dialect.wrapColumnName(association.getJoinColumnName())).append("=#{").append(association.getInverse().getName()).append(".").append(association.getObverse().getName()).append("}").append(",</if>");
243+
if (ignoreNull) {
244+
builder.append("<if test=\"" + association.getInverse().getName() + " != null and " + association.getInverse().getName() + "." + association.getObverse().getName() + " != null\">");
245+
}
246+
builder.append(dialect.wrapColumnName(association.getJoinColumnName())).append("=#{").append(association.getInverse().getName()).append(".").append(association.getObverse().getName()).append("},");
247+
if (ignoreNull) {
248+
builder.append("</if>");
249+
}
232250
return;
233251
}
234252

@@ -835,7 +853,7 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
835853
property.getJdbcType()
836854
));
837855
}
838-
}else{
856+
} else {
839857

840858

841859
}

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

+37-8
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
public class SimpleMybatisRepository<T, ID extends Serializable> extends SqlSessionRepositorySupport
4141
implements MybatisRepository<T, ID> {
4242

43-
private static final String STATEMENT_INSERT = "_insert";
44-
private static final String STATEMENT_UPDATE = "_update";
45-
private static final String STATEMENT_GET_BY_ID = "_getById";
43+
private static final String STATEMENT_INSERT = "_insert";
44+
private static final String STATEMENT_UPDATE = "_update";
45+
private static final String STATEMENT_UPDATE_IGNORE_NULL = "_updateIgnoreNull";
46+
private static final String STATEMENT_GET_BY_ID = "_getById";
4647
private static final String STATEMENT_DELETE_BY_ID = "_deleteById";
4748

4849
private final MybatisEntityInformation<T, ID> entityInformation;
49-
private AuditorAware<Long> auditorAware;
50+
private AuditorAware<Long> auditorAware;
5051

5152
public SimpleMybatisRepository(
5253
MybatisEntityInformation<T, ID> entityInformation,
@@ -91,27 +92,55 @@ public <S extends T> S update(S entity) {
9192
return entity;
9293
}
9394

95+
@Override
96+
@Transactional
97+
public <S extends T> S updateIgnoreNull(S entity) {
98+
entityInformation.setLastModifiedDate(entity);
99+
entityInformation.setLastModifiedBy(entity);
100+
101+
int row = update(STATEMENT_UPDATE_IGNORE_NULL, entity);
102+
if (row == 0) {
103+
throw new MybatisNoHintException("update effect 0 row, maybe version control lock occurred.");
104+
}
105+
if (entityInformation.hasVersion()) {
106+
entityInformation.increaseVersion(entity);
107+
}
108+
return entity;
109+
}
110+
94111
@Override
95112
@Transactional
96113
public <S extends T> S save(S entity) {
97-
Assert.notNull(entity);
114+
Assert.notNull(entity, "entity can not be null");
98115

99116
if (entityInformation.isNew(entity)) {
100117
// insert
101-
102118
insert(entity);
103119
} else {
104120
// update
105-
106121
update(entity);
107122
}
123+
return entity;
124+
}
125+
126+
@Override
127+
@Transactional
128+
public <S extends T> S saveIgnoreNull(S entity) {
129+
Assert.notNull(entity, "entity can not be null");
108130

131+
if (entityInformation.isNew(entity)) {
132+
// insert
133+
insert(entity);
134+
} else {
135+
// update
136+
updateIgnoreNull(entity);
137+
}
109138
return entity;
110139
}
111140

112141
@Override
113142
public T findOne(ID id) {
114-
Assert.notNull(id);
143+
Assert.notNull(id, "id can not be null");
115144
return selectOne(STATEMENT_GET_BY_ID, id);
116145
}
117146

0 commit comments

Comments
 (0)