Skip to content

Spring Boot integration with MyBatis-Plus

Overview

In web application, data storage is a crucial component. On the persistence layer, we commonly use Mybatis or Jpa. Mybatis is one of the most commonly used open-source frameworks for implementing SQL databases access in Java application. In this tutorial, I will show how to integrate Mybatis with Spring Boot.

Maven Dependencies

xml
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>

create table

sql
create table t_user
(
    id         bigint                  not null comment 'ID'
        primary key,
    t_username varchar(64)  default '' not null comment 'username',
    t_password varchar(64)  default '' not null comment 'password',
    t_phone    varchar(64)  default '' not null comment 'phone',
    t_address  varchar(255) default '' not null comment 'address'
);

Define Model

java
@TableName(value = "t_user")
@Data
public class TUser {

    private Long id;

    @TableField(value = "t_username")
    private String username;

    @TableField(value = "t_password")
    private String password;

    @TableField(value = "t_phone")
    private String phone;

    @TableField(value = "t_address")
    private String address;

}
``

## Define Mapper Interface
```java
public interface TUserMapper extends BaseMapper<TUser> {

}

Write mapper xml

xml
<?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="org.example.mapper.TUserMapper">

    <resultMap id="BaseResultMap" type="org.example.domain.TUser">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="tUsername" column="t_username" jdbcType="VARCHAR"/>
            <result property="tPassword" column="t_password" jdbcType="VARCHAR"/>
            <result property="tPhone" column="t_phone" jdbcType="VARCHAR"/>
            <result property="tAddress" column="t_address" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,t_username,t_password,
        t_phone,t_address
    </sql>
</mapper>

Define UserService Interface

java
public interface TUserService extends IService<TUser> {

}

Implements UserService

java
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
    implements TUserService{
}

UserController

java
@RestController
@Slf4j
@RequestMapping("user")
public class TestController {

    @Value("${env.name}")
    private String env;
    private final TUserService userService;

    public TestController(TUserService userService) {
        this.userService = userService;
    }

    @GetMapping("{id}")
    private TUser getById(@PathVariable String id) {
        log.info("current env {}",env);
        return userService.getById(id);
    }
}

Main

java
@SpringBootApplication
@MapperScan("org.example.mapper")
public class SpringTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringTestApplication.class,args);
    }
}

configuration application.yml file

yml
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  profiles:
    active: dev

Conclusion

In this article, we explored multiple ways of configuring MyBatis-plus with Spring boot. also you can find more use demo from https://baomidou.com/

Released under the MIT License.