30
30
import org .springframework .data .jdbc .mapping .model .JdbcMappingContext ;
31
31
import org .springframework .data .jdbc .mapping .model .JdbcPersistentProperty ;
32
32
import org .springframework .data .mapping .PropertyPath ;
33
+ import org .springframework .util .Assert ;
33
34
34
35
/**
35
36
* {@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.
41
43
*
42
44
* @author Jens Schauder
43
45
* @author Kazuki Shimizu
44
46
*/
45
47
public class MyBatisDataAccessStrategy implements DataAccessStrategy {
46
48
47
49
private final SqlSession sqlSession ;
48
- private MyBatisNamingStrategy namingStrategy = new MyBatisNamingStrategy () {} ;
50
+ private NamespaceStrategy namespaceStrategy = NamespaceStrategy . DEFAULT_INSTANCE ;
49
51
50
52
/**
51
53
* 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}
57
55
*/
58
56
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 ) {
59
66
60
67
// the DefaultDataAccessStrategy needs a reference to the returned DataAccessStrategy. This creates a dependency
61
68
// cycle. In order to create it, we need something that allows to defer closing the cycle until all the elements are
62
69
// created. That is the purpose of the DelegatingAccessStrategy.
63
70
DelegatingDataAccessStrategy delegatingDataAccessStrategy = new DelegatingDataAccessStrategy ();
64
71
MyBatisDataAccessStrategy myBatisDataAccessStrategy = new MyBatisDataAccessStrategy (sqlSession );
72
+ myBatisDataAccessStrategy .setNamespaceStrategy (namespaceStrategy );
65
73
66
74
CascadingDataAccessStrategy cascadingDataAccessStrategy = new CascadingDataAccessStrategy (
67
75
asList (myBatisDataAccessStrategy , delegatingDataAccessStrategy ));
@@ -92,11 +100,15 @@ public MyBatisDataAccessStrategy(SqlSession sqlSession) {
92
100
}
93
101
94
102
/**
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}
97
106
*/
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 ;
100
112
}
101
113
102
114
@ Override
@@ -168,7 +180,8 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
168
180
169
181
@ Override
170
182
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 (),
172
185
new MyBatisContext (rootId , null , property .getType (), Collections .emptyMap ()));
173
186
}
174
187
@@ -185,7 +198,7 @@ public long count(Class<?> domainType) {
185
198
}
186
199
187
200
private String namespace (Class <?> domainType ) {
188
- return this .namingStrategy .getNamespace (domainType );
201
+ return this .namespaceStrategy .getNamespace (domainType );
189
202
}
190
203
191
204
private SqlSession sqlSession () {
0 commit comments