Mybatis-plus使用

Posted by KANG's BLOG on Tuesday, May 18, 2021

1. 介绍

​ 封装mybatis,无需编写mapper,直接通过jpa方式定义接口即可操作数据库。

2. pom

<!-- spring boot环境 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<!-- MP核心jar包 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.1.tmp</version>
</dependency>
<!-- 数据库相关 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.9</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.18</version>
</dependency>

3. mapper

public interface TestTableMapper extends BaseMapper<TestTable> {

}
  • 与BaseMapper接口中相同的方法将直接重写
  • 复杂方法可使用mybatis的方式定义

4. 实体类定义

@Data
@TableName("t_test_table")
public class TestTable {
    private Long id;
    private String name;
}

5. 使用

@Autowired
private UserMapper userMapper;

@Test
public void testSelect() {
    List<TestTable> userList = testTableMapper.selectList(null);
    // TODO ...
}