Spring Boot 集成 MyBatis(注解方式)操作数据库

注解方式相较于 XML 方式而言,SQL 语句查找方便,直接放在接口方法的注解上,可以很容易找到,但 SQL 语句排版效果不是很好,如果是复杂的 SQL 语句很难看明白它的逻辑,并且对动态 SQL 语句的支持很差,需要单独提供生成 SQL 语句的方法。

下面,我们来看看基于注解方式的 MyBatis 如何配置。

添加 MyBatis 依赖

1
<dependency>
2
    <groupId>org.mybatis.spring.boot</groupId>
3
    <artifactId>mybatis-spring-boot-starter</artifactId>
4
    <version>1.3.1</version>
5
</dependency>

配置数据源信息

  • application.properties
1
spring.datasource.url=jdbc:mysql://localhost:3306/game?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
2
spring.datasource.username=root
3
spring.datasource.password=root
4
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

创建 Mapper 类

1
@Mapper // 此处可以不用添加注解,可以统一在启动类上添加 @MapperScan
2
public interface UserMapper {
3
4
    @Select("SELECT * FROM user WHERE username = #{username}")
5
    User findByName(@Param("username") String name);
6
7
    @Insert("INSERT INTO user(username, password) VALUES(#{username}, #{password})")
8
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
9
    int save(@Param("name") String name, @Param("password") String password);
10
11
    @UpdateProvider(type = UserDAOProvider.class, method = "updateByPrimaryKey")
12
    int updateById(@Param("user") User user);
13
14
    @Delete("delete from user where id = #{id}")
15
    int deleteById(@Param("id") Integer id);
16
}

其中,Update 方法使用的是一个 type 和一个 method 属性来指定,它们指定了 Provider 类中的一个方法。因为在进行数据更新时需要判断字段是否为空来决定是否更新这个字段,而在 XML 配置中可以使用 <if> 标签实现。如果使用注解的方式,就只能通过提供一个 Provider 类来动态生成 SQL 语句。

  • UserDAOProvider.class
1
public class UserDAOProvider {
2
3
    public String updateByPrimaryKey(Map<String, Object> map) {
4
        User user = (User) map.get("user");
5
        if (user == null || user.getId() == null) {
6
            throw new RuntimeException("The primaryKey can not be null.");
7
        }
8
        // 拼接 sql 语句
9
        StringBuilder updateStrSb = new StringBuilder("UPDATE user SET ");
10
        StringBuilder setStrSb = new StringBuilder();
11
        if (user.getUsername() != null) {
12
            setStrSb.append("username = #{user.username},");
13
        }
14
        if (user.getPassword() != null) {
15
            setStrSb.append("password = #{user.password}");
16
        }
17
18
        if (setStrSb.length() <= 0) {
19
            throw new RuntimeException("None element to update.");
20
        }
21
        updateStrSb.append(setStrSb).append(" WHERE id = #{user.id}");
22
23
        return updateStrSb.toString();
24
    }
25
}

在启动类上添加 @MapperScan 注解

MyBatis 启动时可以不在 mapper 层加上 @Mapper 注解,但是一定要在启动类上加上 @MapperScan 注解,并指定扫包范围。如果在 mapper 接口类上加上了 @Mapper 注解,就不需要在启动类上加上 @MapperScan 注解了。

1
@SpringBootApplication
2
@MapperScan(basePackages = "com.example.mybatis.mapper")
3
public class MybatisApplication {
4
5
    public static void main(String[] args) {
6
        SpringApplication.run(MybatisApplication.class, args);
7
    }
8
9
}

这样,MyBatis 注解方式的使用配置就完成了,接下来就可以操作数据库啦!

最后,详细代码可以查看本示例的 Demo。

源码地址

springboot-mybatis-annotation