Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time

build test deploy Maven Central


Provides MyBatis integration with Bootique.

For additional help/questions about this example send a message to Bootique forum.


Add bootique-mybatis to your build

Add the dependency on bootique-mybatis to your build. Here is a Maven example:


After that you can configure a number of things (mappers, type handlers) in the code and use Bootique-provided DataSource, and/or use MyBatis XML configuration. Below are a the examples of both.

Configure Extensions in the Code / DataSource Provided by Bootique

Configure MyBatis mappers and type handlers in the code:

public class MyModule implements Module {

	public void configure(Binder binder) {

		// add annotated Mappers ...
			// ... a whole package of Mappers
			// ... a single mapper
			// ... a whole package of TypeHandlers
			// ... a single mapper

Configure DataSource in Bootique:

# Implicit single DataSource. MyBatis will find and use it automatically.
    jdbcUrl: "jdbc:mysql://"
    username: root
    password: secret
    jdbcUrl: "jdbc:mysql://"
    username: root
    password: secret

# Explicitly reference a named DataSource
  datasource: myds

Configure Anything in MyBatis XML

If you'd rather prefer to use MyBatis "canonical" approach with an XML config file, you can still do that (optionally combining it with Bootique-configured DataSource).

First, configure a reference to MyBatis XML:

  environmentId: qa
  config: classpath:mybatis-config.xml

Second create MyBatis config XML as you normally would. In this example it contains the <environment>..</environment> section with DB connection info. If you omit the "environment" config, make sure you configure a Bootique DataSource in YAML as described above.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-// Config 3.0//EN"
    <environments default="default">

        <!-- If "environment" is not provided, Bootique will look for DataSource configuration in YAML -->
        <environment id="qa">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://"/>
                <property name="username" value="root"/>
                <property name="password" value="secret"/>
        <mapper resource="com/foo/MyMapper1.xml"/>
        <mapper resource="com/foo/MyMapper2.xml"/>

Use MyBatis

Regardless of how MyBatis was configured, you can use it in the same way, by injecting SqlSessionManager:

public class MyClass {

	private SqlSessionManager sessionManager;

	public void doSomething() {
		try (SqlSession session = sessionManager.openSession()) {
			MyMapper2 mapper = session.getMapper(MyMapper2.class);
			Optional<O1> o1 = mapper.find(1);