问题
Need to Integrate Camel and MyBatis with application using SpringBoot. SpringBoot provides out-of-box support for Camel and MyBatis. Also provides Camel and MyBatis SpringBoot starters.
However, when i try to integrate Spring Boot application with Camel and MyBatis, it fails.
I am using Java DSL based Camel Route. Also using Mybatis Spring boot dependencies. Annotate mappers, added database properties in the application.properties file. What I was expecting to happen: 1) SpringBoot setup datasource and mappers, sqlsessionfactory on start-up. 2) Next the Camel-MyBatis consumer is called, the setup done in (1) would allow, Camel to successfully make database calls using mybatis.
I created spring annotated configuration class and used it to create/get DataSource bean.
How can i get Camel to use this dataSource bean? How to tell Camel to use newly build SQL session factory, instead of it trying to build from configuration file?
Created sample appl in github, its using in-memory db (h2)
sample-app
Getting NPE Consumer[mybatis://getClaimInfo?statementType=SelectOne] failed polling endpoint: Endpoint[mybatis://getClaimInfo?statementType=SelectOne]. Will try again at next poll. Caused by: [org.apache.ibatis.exceptions.PersistenceException -
Error opening session. Cause: java.lang.NullPointerException
Cause: java.lang.NullPointerException]
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100) ~[mybatis-3.4.0.jar:3.4.0] at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47) ~[mybatis-3.4.0.jar:3.4.0]
回答1:
I have been able to use Spring Boot 1.3.6, Apache Camel 2.17.2 with Mybatis-Spring-Boot-Starter 1.1.1 successfully:
Key dependencies in maven:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mybatis</artifactId>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
</dependency>
Key bean to declare
@Bean(name="mybatis")
public MyBatisComponent myBatisComponent( SqlSessionFactory sqlSessionFactory )
{
MyBatisComponent result = new MyBatisComponent();
result.setSqlSessionFactory( sqlSessionFactory );
return result;
}
来源:https://stackoverflow.com/questions/38292022/how-to-integrate-spring-boot-camel-and-mybatis