Spring Boot、Camel、Mybatis如何整合

How to integrate Spring Boot, Camel and Mybatis

需要使用 SpringBoot 将 Camel 和 MyBatis 与应用程序集成。 SpringBoot 为 Camel 和 MyBatis 提供开箱即用的支持。还提供 Camel 和 MyBatis Spring启动器。

但是,当我尝试将 Spring 引导应用程序与 Camel 和 MyBatis 集成时,它失败了。

我正在使用 Java 基于 DSL 的 Camel Route。还使用 Mybatis Spring 引导依赖项。注释映射器,在 application.properties 文件中添加数据库属性。我期待发生的事情: 1) Spring启动时启动设置数据源和映射器、sqlsessionfactory。 2) 接下来调用 Camel-MyBatis 消费者,在 (1) 中完成的设置将允许 Camel 使用 mybatis 成功地进行数据库调用。

我创建了 spring 带注释的配置 class 并将其用于 create/get DataSource bean。

如何让 Camel 使用这个数据源 bean? 如何告诉 Camel 使用新构建的 SQL 会话工厂,而不是尝试从配置文件构建?

在 github 中创建了示例应用程序,它使用内存数据库 (h2)
sample-app

获得 NPE 消费者 [mybatis://getClaimInfo?statementType=SelectOne] 轮询端点失败:端点 [mybatis://getClaimInfo?statementType=SelectOne]。将在下次投票时重试。原因:[org.apache.ibatis.exceptions.PersistenceException -

打开会话时出错。原因:java.lang.NullPointerException

原因: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] 在 org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47) ~[mybatis-3.4.0.jar:3.4.0]

我已经能够使用 Spring Boot 1.3.6, Apache Camel 2.17.2 with Mybatis-Spring-Boot-Starter 1.1.1 成功:

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>

要声明的关键 bean

@Bean(name="mybatis")
public MyBatisComponent myBatisComponent( SqlSessionFactory sqlSessionFactory )
{
    MyBatisComponent result = new MyBatisComponent();
    result.setSqlSessionFactory( sqlSessionFactory );
    return result;
}