Mybatis-Puls (下文简称MP)如何在SpringBoot中使用
1、首先,引入相关的依赖2、在application.yml文件配置MP的相关属性com.baomidou mybatis-plus-boot-starter 3.5.1
mybatis-plus:
configuration:
# 在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
# 举两个栗子 表名:address_book -->实体类名: AddressBook 字段名:user_name --> 属性名:userName
map-underscore-to-camel-case: true
# 开启控制台SQL日志打印
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
# 指定主键生成策略 assign_id 采用雪花算法
id-type: assign_id
3、如何使用MP的CRUD
mapper层
-
继承MP提供的 BaseMapper
类,并指定泛型 @Mapper public interface EmployeeMapper extends BaseMapper
{ }
service接口层
-
继承MP提供的 IService
接口,并指定泛型 public interface EmployeeService extends IService
{ }
service实现类层
-
继承MP提供的 ServiceImpl
,指定泛型,实现 service 接口 @Service public class EmployeeServiceImpl extends ServiceImpl
implements EmployeeService { } 需要指定两个泛型,第一个是Mapper,第二个是实体类
- 先搞一个配置类
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
end…



