添加依赖


Spring Boot2

Maven

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.17</version>
</dependency>

配置


在Spring Boot启动类添加@MapperScan注解扫描Mapper包:

@SpringBootApplication 
@MapperScan("com.example.mapper") 
public class ExampleApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(ExampleApplication.class, args);  
    }  
  
}

或者在每个Mapper接口上使用@Mapper

使用


首先编写一个Mapper接口,并继承BaseMapper<T>接口,指定Mapper接口对应的实体类:

public interface UserMapper extends BaseMapper<User> {
 
}

然后在其他类中注入UserMapper并使用对应方法。