環境
- mybatis-spring-boot-starter: 1.3.1
- mybatis-spring-boot-starter-test: 1.3.1
- postgresql jdbc : 42.1.4
- Spring Boot 1.5.9
- Java8
- PostgreSQL 9.6
プログラム
Spring Boot用のMyBatisを使っています。
通常時はSIMPLE sqlSession, INSERT時はBATCHのsqlSessiionを使うために、以下のようなプログラムを作成しました。
java
1 2@RequiredArgsConstructor 3@Configuration 4public class ApplicationConfig { 5 private final SqlSessionFactory sqlSessionFactory; 6 7 @Bean 8 @Primary //@Mapperを使ってもエラーが出ないように指定 9 public SqlSession sqlSession() { 10 SqlSessionTemplate sqlSession = new SqlSessionTemplate(sqlSessionFactory); 11 12 return sqlSession; 13 } 14 15 @Bean 16 public SqlSession batchSqlSession() { 17 SqlSessionTemplate sqlSession = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH); 18 19 return sqlSession; 20 } 21}
java
1@Component 2public class HotelDao { 3 4 @Autowired 5 public SqlSession sqlSession; 6 7 @Autowired 8 @Qualifier("batchSqlSession") 9 public SqlSession batchSqlSession; 10 11 public Hotel findById(long id) { 12 return this.sqlSession.selectOne("findHotelById", id); 13 } 14 15 public void insert(int cityId) { 16 HashMap<String,Object> sqlMap = new HashMap<>(); 17 sqlMap.put("cityId", cityId); 18 sqlMap.put("name", cityId + "_name"); 19 batchSqlSession.insert("insert", sqlMap); 20 } 21 22}
java
1@RunWith(SpringRunner.class) 2@MybatisTest 3@AutoConfigureTestDatabase(replace = Replace.NONE) 4@Import({HotelDao.class}) 5public class HotelDaoTest { 6 7 @Autowired 8 private HotelDao hotelDao; 9 10 @Commit 11 @Test 12 public void test() { 13 //試しにINSERT 14 hotelDao.insert(10); 15 } 16 17}
問題
JUnitでHotelDaoTest
のtest
メソッドを実行した際、以下のエラーが発生しました。
txt
1java.lang.IllegalStateException: Failed to load ApplicationContext 2 ... 3Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo2.mybatis.dao.HotelDao': Unsatisfied dependency expressed through field 'batchSqlSession'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=batchSqlSession)} 4 ... 5Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=batchSqlSession)} 6 ...
質問
このエラーを解決するには、テストコードをどのように修正すればよろしいでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/06 13:36