Spring Boot MySQL Integration Tutorial with MyBatis: Project Setup and Code Example

This beginner-friendly tutorial walks through creating a Spring Boot project, adding MySQL and MyBatis dependencies, configuring the application.yml file, and implementing UserMapper, User, and UserController classes to demonstrate a complete MySQL query demo.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Spring Boot MySQL Integration Tutorial with MyBatis: Project Setup and Code Example

This article is suitable for beginners.

Creating a new project and importing dependencies

After the project is created, check the pom.xml file.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
    <scope>runtime</scope>
</dependency>

Create local database tables.

Project directory structure in IDEA:

Configuration file: application.yml (note that key‑value pairs must contain spaces).

Next, the code:

UserMapper.java (interface)

package com.example.runlife.mapper;

import com.example.runlife.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from tt_user_user")
    List<User> findAll();
}

User.java (entity class)

package com.example.runlife.entity;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String uname;
    private String upwd;
}

UserController.java (REST controller)

package com.example.runlife.controller;

import com.example.runlife.entity.User;
import com.example.runlife.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/")
    public List<User> index() {
        List<User> all = userMapper.findAll();
        return all;
    }
}

Right‑click to run RunlifeApplication:

Open a browser and visit the root URL:

At this point, the Spring Boot MySQL query demo is complete; you can try it yourself.

Remember to follow the public account.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendjavaSpring BootmysqlMyBatisTutorial
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.