質問編集履歴
5
`Qualifier`を追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -241,3 +241,25 @@
|
|
241
241
|
}
|
242
242
|
|
243
243
|
```
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
また、`@Qualifier`でSqlSessionを特定しました。
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
```java
|
252
|
+
|
253
|
+
public class UserDaoImpl implements UserDao {
|
254
|
+
|
255
|
+
//...
|
256
|
+
|
257
|
+
@Autowired
|
258
|
+
|
259
|
+
@Qualifier("batchSqlSession")
|
260
|
+
|
261
|
+
private SqlSession batchSqlSession;
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
```
|
4
insert文に変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,7 +116,7 @@
|
|
116
116
|
|
117
117
|
for(String name : names) {
|
118
118
|
|
119
|
-
batchSqlSession.se
|
119
|
+
batchSqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", name);
|
120
120
|
|
121
121
|
}
|
122
122
|
|
3
不要なコードを削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -184,12 +184,6 @@
|
|
184
184
|
|
185
185
|
```
|
186
186
|
|
187
|
-
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
|
188
|
-
|
189
|
-
2018-01-27 18:59:47.476 ERROR 16344 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
|
190
|
-
|
191
|
-
|
192
|
-
|
193
187
|
***************************
|
194
188
|
|
195
189
|
APPLICATION FAILED TO START
|
@@ -230,16 +224,6 @@
|
|
230
224
|
|
231
225
|
```java
|
232
226
|
|
233
|
-
@Configuration
|
234
|
-
|
235
|
-
public class ApplicationConfig {
|
236
|
-
|
237
|
-
@Autowired
|
238
|
-
|
239
|
-
private SqlSessionFactory sqlSessionFactory;
|
240
|
-
|
241
|
-
|
242
|
-
|
243
227
|
@Bean
|
244
228
|
|
245
229
|
@Primary
|
2
ベストアンサーの手法に追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -153,3 +153,107 @@
|
|
153
153
|
|
154
154
|
|
155
155
|
[http://www.mybatis.org/spring/ja/sqlsession.html](http://www.mybatis.org/spring/ja/sqlsession.html)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
### 【追記】
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
「ベストアンサー」の方法で問題は解決しました。
|
164
|
+
|
165
|
+
しかし、`@Mapper`クラスが存在しているとエラーが発生しました。
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
```java
|
170
|
+
|
171
|
+
@Mapper
|
172
|
+
|
173
|
+
public interface CityMapper {
|
174
|
+
|
175
|
+
@Select("select * from city")
|
176
|
+
|
177
|
+
City findCity();
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
|
188
|
+
|
189
|
+
2018-01-27 18:59:47.476 ERROR 16344 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
***************************
|
194
|
+
|
195
|
+
APPLICATION FAILED TO START
|
196
|
+
|
197
|
+
***************************
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
Description:
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
file [C:\MyProgram\pleiades_4.7\workspace\demo2\target\classes\com\example\demo2\mybatis\mapper\CityMapper.class] required a single bean, but 2 were found:
|
206
|
+
|
207
|
+
- sqlSession: defined by method 'sqlSession' in class path resource [com/example/demo2/ApplicationConfig.class]
|
208
|
+
|
209
|
+
- sqlSessionTemplate: defined by method 'sqlSessionTemplate' in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
Action:
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
「1個のbeanを期待してけているけど、2個見つかった」と言っています。
|
226
|
+
|
227
|
+
`@Primary`を付けて、解決しました。
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
```java
|
232
|
+
|
233
|
+
@Configuration
|
234
|
+
|
235
|
+
public class ApplicationConfig {
|
236
|
+
|
237
|
+
@Autowired
|
238
|
+
|
239
|
+
private SqlSessionFactory sqlSessionFactory;
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
@Bean
|
244
|
+
|
245
|
+
@Primary
|
246
|
+
|
247
|
+
public SqlSession sqlSession() {
|
248
|
+
|
249
|
+
SqlSessionTemplate sqlSession = new SqlSessionTemplate(sqlSessionFactory);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
return sqlSession;
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
```
|
1
selectOne⇒insertに変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,7 +40,7 @@
|
|
40
40
|
|
41
41
|
for(String name : names) {
|
42
42
|
|
43
|
-
sqlSession.se
|
43
|
+
sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", name);
|
44
44
|
|
45
45
|
}
|
46
46
|
|