Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
Simple example of how to use MyBatis with annotations.

This example uses an embedded H2 database by default. This allows you to check the project out and immediately
run a clean build via Maven:
This example uses PostgreSQL with Testcontainers for automated testing. Tests automatically spin up a PostgreSQL
container, run the tests, and clean up - no manual database setup required!

``mvn clean verify
``
## Quick Start

In addition, a MySQL Workbench file is included, along with a sample MyBatis generator file. You can use these
to explore more complex mappings - for example, to use the generator with an existing schema.
Simply run the tests via Maven. Testcontainers will automatically handle the PostgreSQL setup:

```bash
mvn clean verify
```

**Note:** Docker must be running for tests to work, as Testcontainers manages PostgreSQL containers automatically.

## Manual Local Development (Optional)

If you want to run PostgreSQL manually for development, use the included docker-compose.yml:

```bash
# Start PostgreSQL
docker-compose up -d

# Stop PostgreSQL
docker-compose down

# Stop and remove data
docker-compose down -v
```

The PostgreSQL instance will be available at `localhost:5432` with:
- Database: `testdb`
- Username: `test`
- Password: `test`

## Requirements

- [Maven 3.1.0](http://maven.apache.org/)
- Java 1.7+
- [Maven 3.9.0+](http://maven.apache.org/)
- Java 21+ (LTS)
- Docker (for Testcontainers and docker-compose)

## Dependencies

This project uses the following major dependencies:
- MyBatis 3.5.19
- JUnit 5 (Jupiter) 5.11.4
- Log4j 2.24.0
- PostgreSQL JDBC Driver 42.7.7
- Testcontainers 1.20.1

## Additional Resources

A MySQL Workbench file is included, along with a sample MyBatis generator file. You can use these
to explore more complex mappings - for example, to use the generator with an existing schema.
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.8'

services:
postgres:
image: postgres:16-alpine
container_name: mybatis-postgres
environment:
POSTGRES_DB: testdb
POSTGRES_USER: test
POSTGRES_PASSWORD: test
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U test"]
interval: 10s
timeout: 5s
retries: 5

volumes:
postgres_data:
90 changes: 59 additions & 31 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,59 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<junit.version>5.11.4</junit.version>
<mybatis.version>3.5.19</mybatis.version>
<log4j.version>2.24.0</log4j.version>
<postgresql.version>42.7.7</postgresql.version>
<testcontainers.version>1.20.1</testcontainers.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.189</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -45,62 +75,62 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.4</version>
<version>4.0.0-M16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<version>3.5.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<version>3.4.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.2</version>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<version>3.1.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<version>3.1.3</version>
</plugin>
</plugins>
</pluginManagement>
Expand All @@ -109,11 +139,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<version>3.13.0</version>
<configuration>
<!-- 1.5 or later required for annotations support -->
<source>1.8</source>
<target>1.8</target>
<release>21</release>
</configuration>
</plugin>

Expand All @@ -131,7 +159,7 @@
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-module-markdown</artifactId>
<version>1.6</version>
<version>2.0.0-M12</version>
</dependency>
</dependencies>
</plugin>
Expand All @@ -145,15 +173,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
<version>3.7.0</version>
</plugin>

<!-- Dependency version reporting. Relies on standard version numbering
- you should use standard version numbering too! http://mojo.codehaus.org/versions-maven-plugin/version-rules.html -->
- you should use standard version numbering too! https://www.mojohaus.org/versions/versions-maven-plugin/version-rules.html -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
<version>2.18.0</version>
<reportSets>
<reportSet>
<reports>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface TransactionTokenMapper {
* of functionality to both intialize the database for test purposes as well as validating the bindings. For
* example, a standard schema() method and a validate() method, called as part of the factory setup.
*/
@Update("create table trans_token (id bigint auto_increment, trans_id varchar, token_id varchar)")
@Update("create table trans_token (id bigserial primary key, trans_id varchar, token_id varchar)")
void schema();

@Select({
Expand Down
20 changes: 6 additions & 14 deletions src/main/resources/Configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">

<!-- H2 in-memory mode -->
<property name="driver" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test" />
<property name="username" value="sa" />
<property name="password" value="sa" />

<!-- MySQL Sample Properties -->
<!--<property name="driver" value="com.mysql.jdbc.Driver" />-->
<!--<property name="url" value="jdbc:mysql://localhost/fingw?autoCommit=true" />-->
<!--<property name="username" value="root" />-->
<!--<property name="password" value="" />-->


<!-- PostgreSQL Configuration -->
<!-- These values can be overridden by Testcontainers during tests -->
<property name="driver" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/testdb" />
<property name="username" value="test" />
<property name="password" value="test" />
</dataSource>
</environment>
</environments>
Expand Down
27 changes: 0 additions & 27 deletions src/main/resources/log4j.xml

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<!-- Console output -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p %d{MM-dd HH:mm:ss} %m (%F:%L) %n"/>
</Console>
</Appenders>

<Loggers>
<!-- Set the priority to debug for these two values to see nice debugging output -->
<Logger name="java.sql" level="error"/>
<Logger name="com.ibatis" level="error"/>
<Logger name="org.apache.ibatis" level="error"/>

<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
Loading