teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

`Qualifier`を追記

2018/01/27 13:05

投稿

yuji38kwmt
yuji38kwmt

スコア437

title CHANGED
File without changes
body CHANGED
@@ -119,4 +119,15 @@
119
119
  return sqlSession;
120
120
  }
121
121
  }
122
+ ```
123
+
124
+ また、`@Qualifier`でSqlSessionを特定しました。
125
+
126
+ ```java
127
+ public class UserDaoImpl implements UserDao {
128
+ //...
129
+ @Autowired
130
+ @Qualifier("batchSqlSession")
131
+ private SqlSession batchSqlSession;
132
+ }
122
133
  ```

4

insert文に変更

2018/01/27 13:05

投稿

yuji38kwmt
yuji38kwmt

スコア437

title CHANGED
File without changes
body CHANGED
@@ -57,7 +57,7 @@
57
57
 
58
58
  public void insert(List<String> names) {
59
59
  for(String name : names) {
60
-   batchSqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.insertUser", name);
60
+   batchSqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", name);
61
61
  }
62
62
  }
63
63
 

3

不要なコードを削除

2018/01/27 12:16

投稿

yuji38kwmt
yuji38kwmt

スコア437

title CHANGED
File without changes
body CHANGED
@@ -91,9 +91,6 @@
91
91
  ```
92
92
 
93
93
  ```
94
- Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
95
- 2018-01-27 18:59:47.476 ERROR 16344 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
96
-
97
94
  ***************************
98
95
  APPLICATION FAILED TO START
99
96
  ***************************
@@ -114,11 +111,6 @@
114
111
  `@Primary`を付けて、解決しました。
115
112
 
116
113
  ```java
117
- @Configuration
118
- public class ApplicationConfig {
119
- @Autowired
120
- private SqlSessionFactory sqlSessionFactory;
121
-
122
114
  @Bean
123
115
  @Primary
124
116
  public SqlSession sqlSession() {

2

ベストアンサーの手法に追記

2018/01/27 10:20

投稿

yuji38kwmt
yuji38kwmt

スコア437

title CHANGED
File without changes
body CHANGED
@@ -75,4 +75,56 @@
75
75
  </bean>
76
76
  ```
77
77
 
78
- [http://www.mybatis.org/spring/ja/sqlsession.html](http://www.mybatis.org/spring/ja/sqlsession.html)
78
+ [http://www.mybatis.org/spring/ja/sqlsession.html](http://www.mybatis.org/spring/ja/sqlsession.html)
79
+
80
+ ### 【追記】
81
+
82
+ 「ベストアンサー」の方法で問題は解決しました。
83
+ しかし、`@Mapper`クラスが存在しているとエラーが発生しました。
84
+
85
+ ```java
86
+ @Mapper
87
+ public interface CityMapper {
88
+ @Select("select * from city")
89
+ City findCity();
90
+ }
91
+ ```
92
+
93
+ ```
94
+ Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
95
+ 2018-01-27 18:59:47.476 ERROR 16344 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
96
+
97
+ ***************************
98
+ APPLICATION FAILED TO START
99
+ ***************************
100
+
101
+ Description:
102
+
103
+ 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:
104
+ - sqlSession: defined by method 'sqlSession' in class path resource [com/example/demo2/ApplicationConfig.class]
105
+ - sqlSessionTemplate: defined by method 'sqlSessionTemplate' in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
106
+
107
+
108
+ Action:
109
+
110
+ 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
111
+ ```
112
+
113
+ 「1個のbeanを期待してけているけど、2個見つかった」と言っています。
114
+ `@Primary`を付けて、解決しました。
115
+
116
+ ```java
117
+ @Configuration
118
+ public class ApplicationConfig {
119
+ @Autowired
120
+ private SqlSessionFactory sqlSessionFactory;
121
+
122
+ @Bean
123
+ @Primary
124
+ public SqlSession sqlSession() {
125
+ SqlSessionTemplate sqlSession = new SqlSessionTemplate(sqlSessionFactory);
126
+
127
+ return sqlSession;
128
+ }
129
+ }
130
+ ```

1

selectOne⇒insertに変更

2018/01/27 10:19

投稿

yuji38kwmt
yuji38kwmt

スコア437

title CHANGED
File without changes
body CHANGED
@@ -19,7 +19,7 @@
19
19
 
20
20
  public void insert(List<String> names) {
21
21
  for(String name : names) {
22
-   sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.insertUser", name);
22
+   sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", name);
23
23
  }
24
24
  }
25
25