MyBatis Tutorial
Introduction to MyBatis
MyBatis is a popular Java persistence framework that simplifies database interactions by mapping SQL statements to Java methods. Unlike JPA/Hibernate, MyBatis focuses on SQL, allowing developers to write optimized queries while still benefiting from object mapping.
Setting Up MyBatis
1. Add Dependencies
To use MyBatis in a Maven project, add the following dependencies to pom.xml
:
1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
2. Configure MyBatis
Create a mybatis-config.xml
file to define settings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
</configuration>
Creating the Data Model
Define a simple Java class to represent a User
entity:
1
2
3
4
5
6
7
public class User {
private int id;
private String name;
private String email;
// Getters and Setters
}
Creating a Mapper
Define a mapper XML file UserMapper.xml
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" resultType="User">
SELECT * FROM users WHERE id = #{id};
</select>
<insert id="insertUser">
INSERT INTO users (name, email) VALUES (#{name}, #{email});
</insert>
<update id="updateUser">
UPDATE users SET name=#{name}, email=#{email} WHERE id=#{id};
</update>
<delete id="deleteUser">
DELETE FROM users WHERE id=#{id};
</delete>
</mapper>
Creating the Mapper Interface
Define an interface to match the mapper:
1
2
3
4
5
6
public interface UserMapper {
User getUserById(int id);
void insertUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
Using MyBatis in Java Code
1. Load MyBatis Configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
2. Perform Database Operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Main {
public static void main(String[] args) {
try (SqlSession session = MyBatisUtil.getSession()) {
UserMapper userMapper = session.getMapper(UserMapper.class);
// Insert a new user
User newUser = new User();
newUser.setName("Alice");
newUser.setEmail("alice@example.com");
userMapper.insertUser(newUser);
session.commit();
// Retrieve user
User user = userMapper.getUserById(1);
System.out.println("User: " + user.getName() + ", " + user.getEmail());
}
}
}
Advanced Features
1. Dynamic SQL
MyBatis supports dynamic SQL using <if>
, <choose>
, and <foreach>
:
1
2
3
4
5
6
7
8
9
<select id="searchUsers" resultType="User">
SELECT * FROM users WHERE 1=1
<if test="name != null">
AND name = #{name}
</if>
<if test="email != null">
AND email = #{email}
</if>
</select>
2. One-to-Many Mapping
Define a relationship where a user has multiple orders:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<resultMap id="UserResultMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<collection property="orders" ofType="Order">
<id property="id" column="order_id"/>
<result property="amount" column="amount"/>
</collection>
</resultMap>
<select id="getUserWithOrders" resultMap="UserResultMap">
SELECT u.*, o.id as order_id, o.amount FROM users u
LEFT JOIN orders o ON u.id = o.user_id WHERE u.id = #{id};
</select>
Conclusion
MyBatis provides a flexible and powerful way to interact with databases using SQL while still benefiting from object mapping. It is an excellent choice for projects requiring fine-tuned query optimization while maintaining clean, maintainable code.