Skip to content

Commit 45d3f67

Browse files
committed
MyBatis quickstart
1 parent 68a577a commit 45d3f67

File tree

9 files changed

+292
-1
lines changed

9 files changed

+292
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This repository contains examples on how to connect to [MariaDB](https://mariadb
1414
- [Spring Boot Data JPA](spring-boot-jpa/): Spring-based programming model for data access on top of JPA.
1515
- [R2DBC ➚](https://github.com/mariadb-developers/reactive-programming-java-examples): Reactive database connectivity.
1616
- [jOOQ](spring-boot-jooq/): Type-safe SQL queries in Java.
17-
- MyBatis (work in progress)
17+
- [MyBatis](spring-boot-mybatis): Map SQL results to Java methods in a simple way.
1818

1919
## Jakarta EE (Java EE)
2020

spring-boot-mybatis/.gitignore

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

spring-boot-mybatis/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# MyBatis quickstart
2+
3+
[MyBatis](https://mybatis.org/mybatis-3) is a persistence framework that maps SQL commands results to Java methods.
4+
5+
This Maven project shows the minimal configuration needed to connect to MariaDB databases using MyBatis in a Spring Boot application.
6+
7+
## LT;DR (kinda)
8+
9+
Add the MariaDB JDBC Driver and MyBatis dependencies:
10+
11+
```xml
12+
<dependency>
13+
<groupId>org.mybatis.spring.boot</groupId>
14+
<artifactId>mybatis-spring-boot-starter</artifactId>
15+
<version>2.2.2</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>org.mariadb.jdbc</groupId>
20+
<artifactId>mariadb-java-client</artifactId>
21+
<scope>runtime</scope>
22+
</dependency>
23+
```
24+
25+
Configure the database connection in the **application.properties** file:
26+
27+
```properties
28+
spring.datasource.url=jdbc:mariadb://localhost:3306/mybatis_demo
29+
spring.datasource.username=user
30+
spring.datasource.password=password
31+
```
32+
33+
Implement a mapper that you can inject in any Spring bean:
34+
35+
```java
36+
@Mapper
37+
public interface ProgrammingLanguageMapper {
38+
39+
@Select("""
40+
SELECT name, rating
41+
FROM programming_language
42+
WHERE rating > 3
43+
""")
44+
List<ProgrammingLanguage> findTopProgrammingLanguages();
45+
46+
}
47+
```
48+
49+
## Requirements
50+
- Java 17 or later. Previous versions should work (update the version in the pom.xml file).
51+
Apache Maven.
52+
- MariaDB server. If you don't want to install anything extra, try creating a free [SkySQL account](https://cloud.mariadb.com).
53+
- An SQL client tool like mariadb, DBeaver, or an SQL integration for your IDE.
54+
55+
## Running the app
56+
57+
Prepare the database:
58+
59+
```sql
60+
CREATE DATABASE mybatis_demo;
61+
CREATE USER 'user'@'%';
62+
GRANT ALL ON mybatis_demo.* TO 'user'@'%' IDENTIFIED BY 'password';
63+
FLUSH PRIVILEGES;
64+
65+
USE mybatis_demo;
66+
CREATE TABLE programming_language(
67+
name VARCHAR(50) NOT NULL UNIQUE,
68+
rating INT
69+
);
70+
```
71+
72+
Run the following in the command line:
73+
74+
```
75+
git clone [email protected]:mariadb-developers/java-quickstart.git
76+
cd java-quickstart/spring-boot-mybatis/
77+
mvn package
78+
java -jar target/spring-boot-mybatis-0.0.1-SNAPSHOT.jar
79+
```

spring-boot-mybatis/pom.xml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.6.6</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>spring-boot-mybatis</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-boot-mybatis</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.mybatis.spring.boot</groupId>
22+
<artifactId>mybatis-spring-boot-starter</artifactId>
23+
<version>2.2.2</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-devtools</artifactId>
29+
<scope>runtime</scope>
30+
<optional>true</optional>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.mariadb.jdbc</groupId>
34+
<artifactId>mariadb-java-client</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.example;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.springframework.boot.ApplicationRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
11+
@SpringBootApplication
12+
public class Application {
13+
14+
private final ProgrammingLanguageMapper programmingLanguageMapper;
15+
16+
public Application(ProgrammingLanguageMapper programmingLanguageMapper) {
17+
this.programmingLanguageMapper = programmingLanguageMapper;
18+
}
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(Application.class, args);
22+
}
23+
24+
@Bean
25+
public ApplicationRunner applicationRunner() {
26+
return args -> {
27+
deleteProgrammingLanguages();
28+
createProgrammingLanguages();
29+
printTopProgrammingLanguages();
30+
};
31+
}
32+
33+
private void deleteProgrammingLanguages() {
34+
System.out.println("Deleting programming languages...");
35+
programmingLanguageMapper.deleteAll();
36+
}
37+
38+
private void createProgrammingLanguages() {
39+
System.out.println("Creating programming languages...");
40+
Arrays.stream("Java,C++,C#,JavaScript,Rust,Go,Python,PHP".split(","))
41+
.forEach(name -> programmingLanguageMapper.insert(name, (int) (Math.random() * 10)));
42+
}
43+
44+
private void printTopProgrammingLanguages() {
45+
System.out.println("Top programming languages:");
46+
47+
List<ProgrammingLanguage> programmingLanguages = programmingLanguageMapper.findTopProgrammingLanguages();
48+
programmingLanguages.stream()
49+
.map(pl -> pl.getName() + ": " + pl.getRating())
50+
.forEach(System.out::println);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example;
2+
3+
public class ProgrammingLanguage {
4+
5+
private String name;
6+
7+
private Integer rating;
8+
9+
public String getName() {
10+
return this.name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public Integer getRating() {
18+
return this.rating;
19+
}
20+
21+
public void setRating(Integer rating) {
22+
this.rating = rating;
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example;
2+
3+
import java.util.List;
4+
5+
import org.apache.ibatis.annotations.Delete;
6+
import org.apache.ibatis.annotations.Insert;
7+
import org.apache.ibatis.annotations.Mapper;
8+
import org.apache.ibatis.annotations.Param;
9+
import org.apache.ibatis.annotations.Select;
10+
11+
@Mapper
12+
public interface ProgrammingLanguageMapper {
13+
14+
@Delete("""
15+
TRUNCATE programming_language
16+
""")
17+
void deleteAll();
18+
19+
@Insert("""
20+
INSERT INTO programming_language(name, rating)
21+
VALUES(#{name}, #{rating})
22+
""")
23+
void insert(@Param("name") String name, @Param("rating") Integer rating);
24+
25+
@Select("""
26+
SELECT name, rating
27+
FROM programming_language
28+
WHERE rating > 3
29+
""")
30+
List<ProgrammingLanguage> findTopProgrammingLanguages();
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.datasource.url=jdbc:mariadb://localhost:3306/mybatis_demo
2+
spring.datasource.username=user
3+
spring.datasource.password=password
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class ApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)