Programming & Development / April 11, 2025

Getting Started with MyBatis in Spring Boot: A Simple Guide

MyBatis Spring Boot Java MyBatis Spring Boot integration database access MySQL CRUD operations Java persistence

Content:

If you're developing a Spring Boot application and want fine-grained control over your SQL while avoiding the boilerplate of JDBC, MyBatis is a great choice. It offers a flexible and lightweight framework for working with databases. In this guide, we'll walk through a simple example of integrating MyBatis into a Spring Boot application.

Why Use MyBatis?

Unlike JPA/Hibernate, MyBatis lets you write your own SQL queries, giving you precise control over the database layer. It maps SQL results directly to Java objects and vice versa with minimal configuration.

Step 1: Add Dependencies

In your pom.xml, add the following dependencies:

xml

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

Step 2: Configure Application Properties

properties

spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=yourpassword
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.type-aliases-package=com.example.demo.model

Step 3: Create the Model

java

public class User {
    private int id;
    private String name;
    private int age;

    // Getters and Setters
}

Step 4: Create the Mapper Interface

java

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users WHERE id = #{id}")
    User findById(int id);

    @Insert("INSERT INTO users(name, age) VALUES(#{name}, #{age})")
    void insert(User user);
}

Step 5: Create a Service

java

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User getUser(int id) {
        return userMapper.findById(id);
    }

    public void addUser(User user) {
        userMapper.insert(user);
    }
}

Step 6: Use It in a Controller

java

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User getUser(@PathVariable int id) {
        return userService.getUser(id);
    }

    @PostMapping
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }
}

Conclusion

MyBatis with Spring Boot makes it easy to integrate SQL into your Java application without the complexity of full ORM frameworks. It's especially powerful when you need performance and full SQL control.

Whether you're building microservices or full-stack apps, MyBatis is a solid choice for data persistence.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex