Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

MyBatis is configured using a SqlMapConfig.xml file located in the root of the classpath, eg in src/main/resources.
This configuration files setup MyBatis as well a pooled data source

Code Block
xml
xml
titleMyBatis SqlMapConfig.xmlxml
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

  <settings>
    <setting name="useGeneratedKeys" value="false"/>
  </settings>

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAliases>
    <typeAlias alias="Order" type="org.apache.camel.example.mybatis.Order"/>
  </typeAliases>

  <!-- setup environment with JDBC data source -->
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
        <property name="url" value="jdbc:derby:memory:mybatis;create=true"/>
      </dataSource>
    </environment>
  </environments>

  <!-- mapping files -->
  <mappers>
    <mapper resource="org/apache/camel/example/mybatis/Order.xml"/>
  </mappers>

</configuration>

...

We have a plain POJO org.apache.camel.example.mybatis.Order which just has getter/setters as shown below:

Code Block
java
java
titleOrder POJOjava
public class Order {

    private int id;
    private String item;
    private int amount;
    private String description;
    private boolean processed;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isProcessed() {
        return processed;
    }

    public void setProcessed(boolean processed) {
        this.processed = processed;
    }
}

And the MyBatis mapping file Order.xml is located in src/main/resources/org/apache/camel/example/mybatis where we map from SQL to this Order POJO, as shown below:

Code Block
xml
xml
titleMyBatis mapping file for Orderxml
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Order">

  <!-- Result maps describe the mapping between the columns returned
 from a query, and the class properties.  A result map isn't
 necessary if the columns (or aliases) match to the properties
 exactly. -->
  <resultMap id="OrderResult" type="Order">
    <result property="id" column="ORD_ID"/>
    <result property="item" column="ITEM"/>
    <result property="amount" column="ITEM_COUNT"/>
    <result property="description" column="ITEM_DESC"/>
    <result property="processed" column="ORD_DELETED"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Order class. -->
  <select id="selectOrders" resultMap="OrderResult">
    select * from ORDERS where ORD_DELETED = false order by ORD_ID
  </select>

  <!-- Insert example, using the Order parameter class -->
  <insert id="insertOrder" parameterType="Order">
    insert into ORDERS (
    ORD_ID,
    ITEM,
    ITEM_COUNT,
    ITEM_DESC,
    ORD_DELETED
    )
    values (
    #{id}, #{item}, #{amount}, #{description}, false
    )
  </insert>

  <update id="consumeOrder" parameterType="Order">
    update ORDERS set ORD_DELETED = true where ORD_ID = #{id}
  </update>

</mapper>

...