48
48
* @author Jarvis Song
49
49
*/
50
50
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 " +
53
53
"<!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>" ;
55
55
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 ;
60
60
private final MybatisPersistentEntity <?> persistentEntity ;
61
- private final MybatisMapperGenerator generator ;
61
+ private final MybatisMapperGenerator generator ;
62
62
63
63
public MybatisSimpleRepositoryMapperGenerator (Configuration configuration , Dialect dialect , MybatisMappingContext context , Class <?> domainClass ) {
64
64
this .configuration = configuration ;
@@ -132,7 +132,10 @@ private String render() throws IOException {
132
132
buildInsertSQL (builder );
133
133
}
134
134
if (!isStatementExist ("_update" )) {
135
- buildUpdateSQL (builder );
135
+ buildUpdateSQL (builder , "_update" , false );
136
+ }
137
+ if (!isStatementExist ("_updateIgnoreNull" )) {
138
+ buildUpdateSQL (builder , "_updateIgnoreNull" , true );
136
139
}
137
140
if (!isStatementExist ("_getById" )) {
138
141
buildGetById (builder );
@@ -177,11 +180,12 @@ private String render() throws IOException {
177
180
return result ;
178
181
}
179
182
180
- private void buildUpdateSQL (final StringBuilder builder ) {
183
+
184
+ private void buildUpdateSQL (final StringBuilder builder , String statementName , final boolean ignoreNull ) {
181
185
if (!persistentEntity .hasIdProperty ()) {
182
186
return ;
183
187
}
184
- builder .append ("<update id=\" _update \" parameterType=\" " + domainClass .getName () + "\" lang=\" XML\" >" );
188
+ builder .append ("<update id=\" " + statementName + " \" parameterType=\" " + domainClass .getName () + "\" lang=\" XML\" >" );
185
189
builder .append ("update " ).append (dialect .wrapTableName (persistentEntity .getTableName ()));
186
190
builder .append ("<set>" );
187
191
@@ -197,12 +201,16 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
197
201
builder .append (dialect .wrapColumnName (property .getColumnName ())).append ("=" ).append (dialect .wrapColumnName (property .getColumnName ())).append ("+1," );
198
202
return ;
199
203
}
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 ())
203
208
.append (",jdbcType=" ).append (property .getJdbcType ())
204
209
.append (null != property .getSpecifiedTypeHandler () ? (",typeHandler=" + property .getSpecifiedTypeHandler ().getName ()) : "" )
205
- .append ("}" ).append (",</if>" );
210
+ .append ("}," );
211
+ if (ignoreNull ) {
212
+ builder .append ("</if>" );
213
+ }
206
214
}
207
215
});
208
216
@@ -217,8 +225,13 @@ public void doWithAssociation(Association<? extends PersistentProperty<?>> ass)
217
225
@ Override
218
226
public void doWithPersistentProperty (PersistentProperty <?> pp ) {
219
227
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
+ }
222
235
}
223
236
});
224
237
}
@@ -227,8 +240,13 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
227
240
228
241
if ((ass instanceof MybatisManyToOneAssociation )) {
229
242
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
+ }
232
250
return ;
233
251
}
234
252
@@ -835,7 +853,7 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
835
853
property .getJdbcType ()
836
854
));
837
855
}
838
- }else {
856
+ } else {
839
857
840
858
841
859
}
0 commit comments