如何集成 Spring Boot、Camel 和 Mybatis

rak*_*k22 1 java spring apache-camel mybatis spring-mybatis

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

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

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

我创建了 spring 带注释的配置类并用它来创建/获取 DataSource bean。

我怎样才能让Camel使用这个dataSource bean?如何告诉 Camel 使用新构建的 SQL 会话工厂,而不是尝试从配置文件构建?

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

获取 NPE Con​​sumer[mybatis://getClaimInfo?statementType=SelectOne] 失败的轮询端点:Endpoint[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]
Run Code Online (Sandbox Code Playgroud)

在 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]

Ian*_*Lim 5

我已经能够成功使用 Spring Boot 1.3.6、Apache Camel 2.17.2 和 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>
Run Code Online (Sandbox Code Playgroud)

要声明的关键 bean

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