Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bootique/bootique-mybatis
Provides LinkMove integration with Bootique
https://github.com/bootique/bootique-mybatis
Last synced: about 2 months ago
JSON representation
Provides LinkMove integration with Bootique
- Host: GitHub
- URL: https://github.com/bootique/bootique-mybatis
- Owner: bootique
- License: apache-2.0
- Created: 2020-01-22T13:33:41.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-22T08:26:01.000Z (8 months ago)
- Last Synced: 2024-04-22T18:21:11.676Z (8 months ago)
- Language: Java
- Homepage: https://bootique.io
- Size: 104 KB
- Stars: 3
- Watchers: 10
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![build test deploy](https://github.com/bootique/bootique-mybatis/actions/workflows/maven.yml/badge.svg)](https://github.com/bootique/bootique-mybatis/actions/workflows/maven.yml)
[![Maven Central](https://img.shields.io/maven-central/v/io.bootique.mybatis/bootique-mybatis.svg?colorB=brightgreen)](https://search.maven.org/artifact/io.bootique.mybatis/bootique-mybatis/)# bootique-mybatis
Provides [MyBatis](https://mybatis.org/mybatis-3/) integration with [Bootique](https://bootique.io).
*For additional help/questions about this example send a message to
[Bootique forum](https://groups.google.com/forum/#!forum/bootique-user).*# Setup
## Add `bootique-mybatis` to your build
Add the dependency on `bootique-mybatis` to your build. Here is a Maven example:
```xml
io.bootique.bom
bootique-bom
X.X
pom
import
...
io.bootique.mybatis
bootique-mybatis```
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:
```java
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:
```yaml
# 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
``````yaml
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:
```yaml
mybatis:
environmentId: qa
config: classpath:mybatis-config.xml
```
Second create MyBatis config XML as you normally would. In this example it contains the `..`
section with DB connection info. If you omit the "environment" config, make sure you configure a Bootique
DataSource in YAML as described above.```xml
```
## Use MyBatis
Regardless of how MyBatis was configured, you can use it in the same way, by injecting `SqlSessionManager`:
```java
public class MyClass {@Inject
private SqlSessionManager sessionManager;public void doSomething() {
try (SqlSession session = sessionManager.openSession()) {
MyMapper2 mapper = session.getMapper(MyMapper2.class);
Optional o1 = mapper.find(1);
}
}
}
```