Spring Boot集成MyBatis-Plus

在這裏插入圖片描述
封面圖:未名湖,北京,2012年4月。
MyBatis-Plus,官網https://mp.baomidou.com/,爲簡化開發而生。
本文將基於spring-boot-demo項目進行集成示例,源代碼:https://github.com/wu-boy/spring-boot-demo.git,mybatis模塊中的mybatis-plus模塊。

新建mybatis-plus模塊

pom.xml中依賴如下:

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
    </dependencies>

編寫源碼

1、創建Spring Boot啓動類,並添加@MapperScan註解。

@SpringBootApplication
@MapperScan("com.wu.springboot.demo.mybatis.plus")
public class MyBatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyBatisPlusApplication.class, args);
    }
}

2、創建pojo,添加@TableName註解,對應數據庫中的表。

@TableName("user_test")
public class User {

    private Long id;

    private String username;

    private Date createTime;

    private Date updateTime;
    // 請自行補充get/set
}

3、創建mapper接口,注意繼承BaseMapper。

public interface UserMapper extends BaseMapper<User> {
}

4、增加配置文件application.yml

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://127.0.0.1:5432/test
    username: postgres
    password: postgres

5、數據庫建表

drop table if exists user_test;
create table user_test(
  id bigint primary key,
  username varchar(32) unique not null,
  create_time timestamp default localtimestamp(0),
  update_time timestamp
);

6、創建測試類

@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void contextLoads() {
    }

    @Test
    public void insertTest() throws Exception {
        User user = new User();
        user.setUsername("admin");
        System.out.println(userMapper.insert(user));
    }

}

運行測試類,觀察數據庫,即可看到插入一條數據。

使用原生MyBatis

從上面的例子來看,只需要繼承BaseMapper,不用自己寫SQL語句,但是如果想用自己的Mapper接口怎麼辦呢?很簡單,只需要增加簡單配置即可。
1、修改application.yml,增加如下配置:

mybatis-plus:
  type-aliases-package: com.wu.springboot.demo.mybatis.plus
  mapper-locations: classpath:mybatis/*.xml
  configuration:
    cache-enabled: true
    auto-mapping-behavior: FULL
    default-executor-type: REUSE
    map-underscore-to-camel-case: true
    lazy-loading-enabled: true
    aggressive-lazy-loading: false

2、UserMapper中新增getByUsername接口

public interface UserMapper extends BaseMapper<User> {
    User getByUsername(String username);
}

3、新建UserMapper.xml文件,位於resources/mybatis目錄下

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wu.springboot.demo.mybatis.plus.dao.UserMapper">
    <select id="getByUsername" parameterType="string" resultType="user">
        select * from user_test where username=#{username}
    </select>
</mapper>

4、測試類中新增如下代碼進行測試

@Test
    public void selectTest() throws Exception {
        User user = userMapper.getByUsername("admin");
        System.out.println(user.getId());
    }

總結

相對於使用原生MyBatis,只需在Mapper接口中繼承BaseMapper即可實現功能增強,類似於JPA的使用。想用BaseMapper自帶的接口可以,想自己寫接口和SQL語句也可以。更多更強大的功能,請參考MyBatis-Plus官網。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章