本文介绍了如何整合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.

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

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

我如何让骆驼使用此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)
sample-app

获得NPE 使用者[mybatis://getClaimInfo?statementType = SelectOne]轮询端点失败:端点[mybatis://getClaimInfo?statementType = SelectOne].将在下一次民意调查中重试.由以下原因引起:[org.apache.ibatis.exceptions.PersistenceException-

Getting NPEConsumer[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(name="mybatis")
public MyBatisComponent myBatisComponent( SqlSessionFactory sqlSessionFactory )
{
    MyBatisComponent result = new MyBatisComponent();
    result.setSqlSessionFactory( sqlSessionFactory );
    return result;
}

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

07-22 09:26