MyBatis | Configuration File | Use a Data Source Managed by a Java EE Server

When running on a Java EE server, you can register the data source on the server and leave transaction control to the Java EE container.

Source Code

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="test">
    <environment id="test">
      <transactionManager type="MANAGED"/> <!-- Set the type to MANAGED -->

      <dataSource type="JNDI"> <!-- Use JNDI. -->
        <!-- Specify the name that can be looked up from JNDI with data_source. -->
        <property name="data_source" value="java:app/sampleDS"/>
      </dataSource>
    </environment>
  </environments>

  <mappers>
    <mapper resource="sample_mapper.xml"/>
  </mappers>
</configuration>

Explanation

  • Set the type of the <transactionManager> tag to MANAGED.
  • Set the type of the <datasource> tag to JNDI.
  • Configure data_source with the <property> tag.
    • Use the name for looking up JNDI as the value.