Search code examples
parameterscxfapache-camelmybatisibatis

How do You pass parameter to myBatis component in Camel?


How can I provide myBatis component with desired query parameters? I want to perfom a simple Select statement with selection by id. The best solution would be a possibility of having automatic parameter extraction directly from Cxf payload.

I'm not using any domain classes for myBatis mapping. Route works fine when using getAll method.

So far, my route looks like this:

@Override
public void configure() throws Exception {
    from(cxfEndpoint).to("mybatis:getById?statementType=SelectList")
    });

and my mapper configuration:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="User">

  <select id="getAll" resultType="map">
      SELECT * FROM USERS
  </select>

  <select id="getById" parameterType="int" resultType="map">
      SELECT * FROM USERS WHERE ID = #{id}
  </select>

</mapper>

Solution

  • You should just populate your body with the Id itself. It can be just an object of type Integer:

    from(cxfEndpoint).setBody(Integer.valueOf(123)).to("mybatis:getById?statementType=SelectList")
    

    where 123 is your Ide.