Skip to content

Commit c7958f8

Browse files
committed
DATAJDBC-178 - Polishing.
Renamed MyBatisNamingStrategy into NamespaceStrategy. This avoids the confusion with the existing NamingStrategy. Introduced a default instance. Added a method creating a proper DataAccessStrategy with a NamespaceStrategy. JavaDoc. Original pull request: #44.
1 parent 45a6d34 commit c7958f8

File tree

3 files changed

+66
-48
lines changed

3 files changed

+66
-48
lines changed

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,46 @@
3030
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
3131
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
3232
import org.springframework.data.mapping.PropertyPath;
33+
import org.springframework.util.Assert;
3334

3435
/**
3536
* {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the
36-
* statement gets constructed as follows: By default, the namespace is based on the class of the entity plus the suffix "Mapper".
37-
* This is then followed by the method name separated by a dot. For methods taking a {@link PropertyPath} as argument,
38-
* the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the
39-
* statement name. Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set. For
40-
* methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type.
37+
* statement gets constructed as follows: By default, the namespace is based on the class of the entity plus the suffix
38+
* "Mapper". This is then followed by the method name separated by a dot. For methods taking a {@link PropertyPath} as
39+
* argument, the relevant entity is that of the root of the path, and the path itself gets as dot separated String
40+
* appended to the statement name. Each statement gets an instance of {@link MyBatisContext}, which at least has the
41+
* entityType set. For methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the
42+
* leaf type.
4143
*
4244
* @author Jens Schauder
4345
* @author Kazuki Shimizu
4446
*/
4547
public class MyBatisDataAccessStrategy implements DataAccessStrategy {
4648

4749
private final SqlSession sqlSession;
48-
private MyBatisNamingStrategy namingStrategy = new MyBatisNamingStrategy() {};
50+
private NamespaceStrategy namespaceStrategy = NamespaceStrategy.DEFAULT_INSTANCE;
4951

5052
/**
5153
* Create a {@link DataAccessStrategy} that first checks for queries defined by MyBatis and if it doesn't find one
52-
* used a {@link DefaultDataAccessStrategy}
53-
*
54-
* @param context
55-
* @param sqlSession
56-
* @return
54+
* uses a {@link DefaultDataAccessStrategy}
5755
*/
5856
public static DataAccessStrategy createCombinedAccessStrategy(JdbcMappingContext context, SqlSession sqlSession) {
57+
return createCombinedAccessStrategy(context, sqlSession, NamespaceStrategy.DEFAULT_INSTANCE);
58+
}
59+
60+
/**
61+
* Create a {@link DataAccessStrategy} that first checks for queries defined by MyBatis and if it doesn't find one
62+
* uses a {@link DefaultDataAccessStrategy}
63+
*/
64+
public static DataAccessStrategy createCombinedAccessStrategy(JdbcMappingContext context, SqlSession sqlSession,
65+
NamespaceStrategy namespaceStrategy) {
5966

6067
// the DefaultDataAccessStrategy needs a reference to the returned DataAccessStrategy. This creates a dependency
6168
// cycle. In order to create it, we need something that allows to defer closing the cycle until all the elements are
6269
// created. That is the purpose of the DelegatingAccessStrategy.
6370
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy();
6471
MyBatisDataAccessStrategy myBatisDataAccessStrategy = new MyBatisDataAccessStrategy(sqlSession);
72+
myBatisDataAccessStrategy.setNamespaceStrategy(namespaceStrategy);
6573

6674
CascadingDataAccessStrategy cascadingDataAccessStrategy = new CascadingDataAccessStrategy(
6775
asList(myBatisDataAccessStrategy, delegatingDataAccessStrategy));
@@ -92,11 +100,15 @@ public MyBatisDataAccessStrategy(SqlSession sqlSession) {
92100
}
93101

94102
/**
95-
* Set a naming strategy for MyBatis objects.
96-
* @param namingStrategy Must be non {@literal null}
103+
* Set a NamespaceStrategy to be used.
104+
*
105+
* @param namespaceStrategy Must be non {@literal null}
97106
*/
98-
public void setNamingStrategy(MyBatisNamingStrategy namingStrategy) {
99-
this.namingStrategy = namingStrategy;
107+
public void setNamespaceStrategy(NamespaceStrategy namespaceStrategy) {
108+
109+
Assert.notNull(namespaceStrategy, "The NamespaceStrategy must not be null");
110+
111+
this.namespaceStrategy = namespaceStrategy;
100112
}
101113

102114
@Override
@@ -168,7 +180,8 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
168180

169181
@Override
170182
public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty property) {
171-
return sqlSession().selectList(namespace(property.getOwner().getType()) + ".findAllByProperty-" + property.getName(),
183+
return sqlSession().selectList(
184+
namespace(property.getOwner().getType()) + ".findAllByProperty-" + property.getName(),
172185
new MyBatisContext(rootId, null, property.getType(), Collections.emptyMap()));
173186
}
174187

@@ -185,7 +198,7 @@ public long count(Class<?> domainType) {
185198
}
186199

187200
private String namespace(Class<?> domainType) {
188-
return this.namingStrategy.getNamespace(domainType);
201+
return this.namespaceStrategy.getNamespace(domainType);
189202
}
190203

191204
private SqlSession sqlSession() {

src/main/java/org/springframework/data/jdbc/mybatis/MyBatisNamingStrategy.java renamed to src/main/java/org/springframework/data/jdbc/mybatis/NamespaceStrategy.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
package org.springframework.data.jdbc.mybatis;
1717

1818
/**
19-
* The naming strategy for MyBatis.
19+
* A strategy to derive a MyBatis namespace from a domainType.
2020
*
2121
* @author Kazuki Shimizu
22+
* @author Jens Schauder
2223
*/
23-
public interface MyBatisNamingStrategy {
24+
public interface NamespaceStrategy {
25+
26+
NamespaceStrategy DEFAULT_INSTANCE = new NamespaceStrategy() {};
2427

2528
/**
26-
* Get a namespace that correspond domain type.
29+
* Get a namespace that corresponds to the given domain type.
2730
* <p>
2831
* By default, the namespace is based on the class of the entity plus the suffix "Mapper".
32+
*
2933
* @param domainType Must be non {@literal null}.
3034
* @return a namespace that correspond domain type
3135
*/

src/test/java/org/springframework/data/jdbc/mybatis/MyBatisCustomizingNamespaceHsqlIntegrationTests.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.springframework.data.jdbc.mybatis;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
20-
import java.io.IOException;
18+
import static org.assertj.core.api.Assertions.*;
2119

2220
import junit.framework.AssertionFailedError;
2321

22+
import java.io.IOException;
23+
2424
import org.apache.ibatis.session.Configuration;
2525
import org.apache.ibatis.session.SqlSession;
2626
import org.apache.ibatis.session.SqlSessionFactory;
@@ -38,20 +38,39 @@
3838
import org.springframework.data.repository.CrudRepository;
3939
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
4040
import org.springframework.test.context.ContextConfiguration;
41-
import org.springframework.test.context.jdbc.Sql;
4241
import org.springframework.test.context.junit4.rules.SpringClassRule;
4342
import org.springframework.test.context.junit4.rules.SpringMethodRule;
4443
import org.springframework.transaction.annotation.Transactional;
4544

4645
/**
47-
* Tests the integration for customizing namespace with Mybatis.
46+
* Tests the integration for customizing the namespace with Mybatis.
4847
*
4948
* @author Kazuki Shimizu
49+
* @author Jens Schauder
5050
*/
5151
@ContextConfiguration
5252
@Transactional
5353
public class MyBatisCustomizingNamespaceHsqlIntegrationTests {
5454

55+
@ClassRule public static final SpringClassRule classRule = new SpringClassRule();
56+
@Rule public SpringMethodRule methodRule = new SpringMethodRule();
57+
58+
@Autowired SqlSessionFactory sqlSessionFactory;
59+
@Autowired DummyEntityRepository repository;
60+
61+
@Test // DATAJDBC-178
62+
public void myBatisGetsUsedForInsertAndSelect() {
63+
64+
DummyEntity entity = new DummyEntity(null, "some name");
65+
DummyEntity saved = repository.save(entity);
66+
67+
assertThat(saved.id).isNotNull();
68+
69+
DummyEntity reloaded = repository.findById(saved.id).orElseThrow(AssertionFailedError::new);
70+
71+
assertThat(reloaded.name).isEqualTo("name " + saved.id);
72+
}
73+
5574
@org.springframework.context.annotation.Configuration
5675
@Import(TestConfiguration.class)
5776
@EnableJdbcRepositories(considerNestedRepositories = true)
@@ -85,37 +104,19 @@ SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory factory) {
85104

86105
@Bean
87106
MyBatisDataAccessStrategy dataAccessStrategy(SqlSession sqlSession) {
107+
88108
MyBatisDataAccessStrategy strategy = new MyBatisDataAccessStrategy(sqlSession);
89-
strategy.setNamingStrategy(new MyBatisNamingStrategy() {
109+
110+
strategy.setNamespaceStrategy(new NamespaceStrategy() {
90111
@Override
91112
public String getNamespace(Class<?> domainType) {
92113
return domainType.getPackage().getName() + ".mapper." + domainType.getSimpleName() + "Mapper";
93114
}
94115
});
116+
95117
return strategy;
96118
}
97119
}
98120

99-
@ClassRule public static final SpringClassRule classRule = new SpringClassRule();
100-
@Rule public SpringMethodRule methodRule = new SpringMethodRule();
101-
102-
@Autowired SqlSessionFactory sqlSessionFactory;
103-
@Autowired DummyEntityRepository repository;
104-
105-
@Test // DATAJDBC-178
106-
public void myBatisGetsUsedForInsertAndSelect() {
107-
108-
DummyEntity entity = new DummyEntity(null, "some name");
109-
DummyEntity saved = repository.save(entity);
110-
111-
assertThat(saved.id).isNotNull();
112-
113-
DummyEntity reloaded = repository.findById(saved.id).orElseThrow(AssertionFailedError::new);
114-
115-
assertThat(reloaded.name).isEqualTo("name " + saved.id);
116-
}
117-
118-
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {
119-
}
120-
121+
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> {}
121122
}

0 commit comments

Comments
 (0)