如何集成 Spring Boot、Camel 和 Mybatis [英] How to integrate Spring Boot, Camel and Mybatis

查看:100
本文介绍了如何集成 Spring Boot、Camel 和 Mybatis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

然而,当我尝试将 Spring Boot 应用程序与 Camel 和 MyBatis 集成时,它失败了.

However, when i try to integrate Spring Boot application with Camel and MyBatis, it fails.

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

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.

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

I created spring annotated configuration class and used it to create/get DataSource bean.

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

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?

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

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

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 -

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.4.0.jar:3.4.0]

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

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]

推荐答案

我已经能够成功使用 Spring Boot 1.3.6、Apache Camel 2.17.2 和 Mybatis-Spring-Boot-Starter 1.1.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:

maven 中的关键依赖:

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>

要声明的关键bean

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

这篇关于如何集成 Spring Boot、Camel 和 Mybatis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆