前言
MyBatis-Plus (opens new window)
(简称 MP)是一个MyBatis (opens new window)
的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
引入插件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>mybatis-plus-latest-version</version>
</dependency>
配置
在 application.yml 配置文件中添加 数据库的相关配置:
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
mybatis-plus.mapper-locations= classpath*:com/baomidou/**/dal/mapper/*Mapper*.xml
添加扫描
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
使用
定义mapper
@Mapper
public interface MaintainRecordMapper extends BaseMapper<MaintainRecordDO> {
}
添加逻辑实现
@Override
public List<MaintainRecordDO> getInfo(List<Long> OrderLineId) {
LambdaQueryWrapper<MaintainRecordDO> wrapper = Wrappers.lambdaQuery();
wrapper.eq(MaintainRecordDO::getIsDeleted, NumberConstant.NOT_DELETE);
return MaintainRecordMapper.selectList(wrapper);
}
/**
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(entity)
*/
public LambdaQueryWrapper() {
this((T) null);
}
中文案例源码写的很清楚,全中文
如果需要配置多数据源和逆向生成,以及page可以参考文档使用即可
指的说一下就是关于page分页
分页
public IPage<OrderDO> selectAOrderListByPage(OrderQueryListBO OrderQueryListBO) {
IPage<OrderDO> orderDOIPage = new Page<>(orderQueryListBO.getPage().getPageNo(), orderQueryListBO.getPage().getLimit());
QueryWrapper<OrderDO> queryWrapper = getorderDOQueryWrapper(orderQueryListBO);
return orderMapper.selectPage(orderDOIPage, queryWrapper);