Skip to content
Permalink
master
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

bootique-mybatis

Provides MyBatis integration with Bootique.

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

Setup

Add bootique-mybatis to your build

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.bootique.bom</groupId>
            <artifactId>bootique-bom</artifactId>
            <version>X.X</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
...
<dependency>
    <groupId>io.bootique.mybatis</groupId>
    <artifactId>bootique-mybatis</artifactId>
</dependency>

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 ...
		MybatisModule.extend(binder)
			// ... a whole package of Mappers
			.addMapperPackage(MyMapper1.class.getPackage())
			// ... a single mapper
			.addMapper(MyMapper2.class)
			// ... a whole package of TypeHandlers
			.addTypeHandlerPackage(MyTH1.class.getPackage())
			// ... a single mapper
			.addTypeHandler(MyTH.class))
    }
}

Configure DataSource in Bootique:

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

# Explicitly reference a named DataSource
mybatis:
  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:

mybatis:
  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 "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <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://127.0.0.1/mydb"/>
                <property name="username" value="root"/>
                <property name="password" value="secret"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/foo/MyMapper1.xml"/>
        <mapper resource="com/foo/MyMapper2.xml"/>
    </mappers>
</configuration>

Use MyBatis

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

public class MyClass {

	@Inject
	private SqlSessionManager sessionManager;

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