3030import org .springframework .data .jdbc .mapping .model .JdbcMappingContext ;
3131import org .springframework .data .jdbc .mapping .model .JdbcPersistentProperty ;
3232import 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 */
4547public 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 () {
0 commit comments