Skip to content

Commit 2163a26

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-157 - Replace dots in MyBatis statement ids with dashes.
1 parent ab6da6e commit 2163a26

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@
2525
import org.springframework.data.mapping.PropertyPath;
2626

2727
/**
28-
* {@link DataAccessStrategy} implementation based on MyBatis.
29-
*
30-
* Each method gets mapped to a statement. The name of the statement gets constructed as follows:
31-
*
32-
* The namespace is based on the class of the entity plus the suffix "Mapper". This is then followed by the method name separated by a dot.
33-
*
34-
* For methods taking a {@link PropertyPath} as argument, the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the statement name.
35-
*
36-
* Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set.
37-
*
38-
* For methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type.
28+
* {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the
29+
* statement gets constructed as follows: The namespace is based on the class of the entity plus the suffix "Mapper".
30+
* This is then followed by the method name separated by a dot. For methods taking a {@link PropertyPath} as argument,
31+
* the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the
32+
* statement name. Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set. For
33+
* methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type.
3934
*
4035
* @author Jens Schauder
4136
*/
@@ -73,7 +68,7 @@ public void delete(Object id, Class<?> domainType) {
7368
@Override
7469
public void delete(Object rootId, PropertyPath propertyPath) {
7570

76-
sqlSession().delete(mapper(propertyPath.getOwningType().getType()) + ".delete." + propertyPath.toDotPath(),
71+
sqlSession().delete(mapper(propertyPath.getOwningType().getType()) + ".delete-" + toDashPath(propertyPath),
7772
new MyBatisContext(rootId, null, propertyPath.getLeafProperty().getTypeInformation().getType(),
7873
Collections.emptyMap()));
7974
}
@@ -94,7 +89,7 @@ public <T> void deleteAll(PropertyPath propertyPath) {
9489
Class leaveType = propertyPath.getLeafProperty().getTypeInformation().getType();
9590

9691
sqlSession().delete( //
97-
mapper(baseType) + ".deleteAll." + propertyPath.toDotPath(), //
92+
mapper(baseType) + ".deleteAll-" + toDashPath(propertyPath), //
9893
new MyBatisContext(null, null, leaveType, Collections.emptyMap()) //
9994
);
10095
}
@@ -119,7 +114,7 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
119114

120115
@Override
121116
public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty property) {
122-
return sqlSession().selectList(mapper(property.getOwner().getType()) + ".findAllByProperty." + property.getName(),
117+
return sqlSession().selectList(mapper(property.getOwner().getType()) + ".findAllByProperty-" + property.getName(),
123118
new MyBatisContext(rootId, null, property.getType(), Collections.emptyMap()));
124119
}
125120

@@ -131,7 +126,8 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
131126

132127
@Override
133128
public long count(Class<?> domainType) {
134-
return sqlSession().selectOne(mapper(domainType) + ".count");
129+
return sqlSession().selectOne(mapper(domainType) + ".count",
130+
new MyBatisContext(null, null, domainType, Collections.emptyMap()));
135131
}
136132

137133
private String mapper(Class<?> domainType) {
@@ -141,4 +137,8 @@ private String mapper(Class<?> domainType) {
141137
private SqlSession sqlSession() {
142138
return sqlSessionFactory.openSession();
143139
}
140+
141+
private String toDashPath(PropertyPath propertyPath) {
142+
return propertyPath.toDotPath().replaceAll("\\.", "-");
143+
}
144144
}

src/test/java/org/springframework/data/jdbc/core/MyBatisDataAccessStrategyUnitTests.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void deleteAllByPath() {
123123

124124
accessStrategy.deleteAll(PropertyPath.from("class.name.bytes", String.class));
125125

126-
verify(session).delete(eq("java.lang.StringMapper.deleteAll.class.name.bytes"), captor.capture());
126+
verify(session).delete(eq("java.lang.StringMapper.deleteAll-class-name-bytes"), captor.capture());
127127

128128
assertThat(captor.getValue()) //
129129
.isNotNull() //
@@ -167,7 +167,7 @@ public void deleteByPath() {
167167

168168
accessStrategy.delete("rootid", PropertyPath.from("class.name.bytes", String.class));
169169

170-
verify(session).delete(eq("java.lang.StringMapper.delete.class.name.bytes"), captor.capture());
170+
verify(session).delete(eq("java.lang.StringMapper.delete-class-name-bytes"), captor.capture());
171171

172172
assertThat(captor.getValue()) //
173173
.isNotNull() //
@@ -248,6 +248,7 @@ public void findAllById() {
248248
);
249249
}
250250

251+
@SuppressWarnings("unchecked")
251252
@Test // DATAJDBC-123
252253
public void findAllByProperty() {
253254

@@ -259,7 +260,7 @@ public void findAllByProperty() {
259260

260261
accessStrategy.findAllByProperty("id", property);
261262

262-
verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty.propertyName"), captor.capture());
263+
verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty-propertyName"), captor.capture());
263264

264265
assertThat(captor.getValue()) //
265266
.isNotNull() //
@@ -298,4 +299,29 @@ public void existsById() {
298299
);
299300
}
300301

302+
@Test // DATAJDBC-157
303+
public void count() {
304+
305+
doReturn(0L).when(session).selectOne(anyString(), any());
306+
307+
accessStrategy.count(String.class);
308+
309+
310+
verify(session).selectOne(eq("java.lang.StringMapper.count"), captor.capture());
311+
312+
assertThat(captor.getValue()) //
313+
.isNotNull() //
314+
.extracting( //
315+
MyBatisContext::getInstance, //
316+
MyBatisContext::getId, //
317+
MyBatisContext::getDomainType, //
318+
c -> c.get("key") //
319+
).containsExactly( //
320+
null, //
321+
null, //
322+
String.class, //
323+
null //
324+
);
325+
}
326+
301327
}

0 commit comments

Comments
 (0)