33
33
34
34
/**
35
35
* {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the
36
- * statement gets constructed as follows: The namespace is based on the class of the entity plus the suffix "Mapper".
36
+ * statement gets constructed as follows: By default, the namespace is based on the class of the entity plus the suffix "Mapper".
37
37
* This is then followed by the method name separated by a dot. For methods taking a {@link PropertyPath} as argument,
38
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
39
* statement name. Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set. For
44
44
*/
45
45
public class MyBatisDataAccessStrategy implements DataAccessStrategy {
46
46
47
- private static final String MAPPER_SUFFIX = "Mapper" ;
48
-
49
47
private final SqlSession sqlSession ;
48
+ private MyBatisNamingStrategy namingStrategy = new MyBatisNamingStrategy () {};
50
49
51
50
/**
52
51
* Create a {@link DataAccessStrategy} that first checks for queries defined by MyBatis and if it doesn't find one
@@ -92,30 +91,38 @@ public MyBatisDataAccessStrategy(SqlSession sqlSession) {
92
91
this .sqlSession = sqlSession ;
93
92
}
94
93
94
+ /**
95
+ * Set a naming strategy for MyBatis objects.
96
+ * @param namingStrategy Must be non {@literal null}
97
+ */
98
+ public void setNamingStrategy (MyBatisNamingStrategy namingStrategy ) {
99
+ this .namingStrategy = namingStrategy ;
100
+ }
101
+
95
102
@ Override
96
103
public <T > void insert (T instance , Class <T > domainType , Map <String , Object > additionalParameters ) {
97
- sqlSession ().insert (mapper (domainType ) + ".insert" ,
104
+ sqlSession ().insert (namespace (domainType ) + ".insert" ,
98
105
new MyBatisContext (null , instance , domainType , additionalParameters ));
99
106
}
100
107
101
108
@ Override
102
109
public <S > void update (S instance , Class <S > domainType ) {
103
110
104
- sqlSession ().update (mapper (domainType ) + ".update" ,
111
+ sqlSession ().update (namespace (domainType ) + ".update" ,
105
112
new MyBatisContext (null , instance , domainType , Collections .emptyMap ()));
106
113
}
107
114
108
115
@ Override
109
116
public void delete (Object id , Class <?> domainType ) {
110
117
111
- sqlSession ().delete (mapper (domainType ) + ".delete" ,
118
+ sqlSession ().delete (namespace (domainType ) + ".delete" ,
112
119
new MyBatisContext (id , null , domainType , Collections .emptyMap ()));
113
120
}
114
121
115
122
@ Override
116
123
public void delete (Object rootId , PropertyPath propertyPath ) {
117
124
118
- sqlSession ().delete (mapper (propertyPath .getOwningType ().getType ()) + ".delete-" + toDashPath (propertyPath ),
125
+ sqlSession ().delete (namespace (propertyPath .getOwningType ().getType ()) + ".delete-" + toDashPath (propertyPath ),
119
126
new MyBatisContext (rootId , null , propertyPath .getLeafProperty ().getTypeInformation ().getType (),
120
127
Collections .emptyMap ()));
121
128
}
@@ -124,7 +131,7 @@ public void delete(Object rootId, PropertyPath propertyPath) {
124
131
public <T > void deleteAll (Class <T > domainType ) {
125
132
126
133
sqlSession ().delete ( //
127
- mapper (domainType ) + ".deleteAll" , //
134
+ namespace (domainType ) + ".deleteAll" , //
128
135
new MyBatisContext (null , null , domainType , Collections .emptyMap ()) //
129
136
);
130
137
}
@@ -136,49 +143,49 @@ public <T> void deleteAll(PropertyPath propertyPath) {
136
143
Class leaveType = propertyPath .getLeafProperty ().getTypeInformation ().getType ();
137
144
138
145
sqlSession ().delete ( //
139
- mapper (baseType ) + ".deleteAll-" + toDashPath (propertyPath ), //
146
+ namespace (baseType ) + ".deleteAll-" + toDashPath (propertyPath ), //
140
147
new MyBatisContext (null , null , leaveType , Collections .emptyMap ()) //
141
148
);
142
149
}
143
150
144
151
@ Override
145
152
public <T > T findById (Object id , Class <T > domainType ) {
146
- return sqlSession ().selectOne (mapper (domainType ) + ".findById" ,
153
+ return sqlSession ().selectOne (namespace (domainType ) + ".findById" ,
147
154
new MyBatisContext (id , null , domainType , Collections .emptyMap ()));
148
155
}
149
156
150
157
@ Override
151
158
public <T > Iterable <T > findAll (Class <T > domainType ) {
152
- return sqlSession ().selectList (mapper (domainType ) + ".findAll" ,
159
+ return sqlSession ().selectList (namespace (domainType ) + ".findAll" ,
153
160
new MyBatisContext (null , null , domainType , Collections .emptyMap ()));
154
161
}
155
162
156
163
@ Override
157
164
public <T > Iterable <T > findAllById (Iterable <?> ids , Class <T > domainType ) {
158
- return sqlSession ().selectList (mapper (domainType ) + ".findAllById" ,
165
+ return sqlSession ().selectList (namespace (domainType ) + ".findAllById" ,
159
166
new MyBatisContext (ids , null , domainType , Collections .emptyMap ()));
160
167
}
161
168
162
169
@ Override
163
170
public <T > Iterable <T > findAllByProperty (Object rootId , JdbcPersistentProperty property ) {
164
- return sqlSession ().selectList (mapper (property .getOwner ().getType ()) + ".findAllByProperty-" + property .getName (),
171
+ return sqlSession ().selectList (namespace (property .getOwner ().getType ()) + ".findAllByProperty-" + property .getName (),
165
172
new MyBatisContext (rootId , null , property .getType (), Collections .emptyMap ()));
166
173
}
167
174
168
175
@ Override
169
176
public <T > boolean existsById (Object id , Class <T > domainType ) {
170
- return sqlSession ().selectOne (mapper (domainType ) + ".existsById" ,
177
+ return sqlSession ().selectOne (namespace (domainType ) + ".existsById" ,
171
178
new MyBatisContext (id , null , domainType , Collections .emptyMap ()));
172
179
}
173
180
174
181
@ Override
175
182
public long count (Class <?> domainType ) {
176
- return sqlSession ().selectOne (mapper (domainType ) + ".count" ,
183
+ return sqlSession ().selectOne (namespace (domainType ) + ".count" ,
177
184
new MyBatisContext (null , null , domainType , Collections .emptyMap ()));
178
185
}
179
186
180
- private String mapper (Class <?> domainType ) {
181
- return domainType . getName () + MAPPER_SUFFIX ;
187
+ private String namespace (Class <?> domainType ) {
188
+ return this . namingStrategy . getNamespace ( domainType ) ;
182
189
}
183
190
184
191
private SqlSession sqlSession () {
0 commit comments