1. 引入pom
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
2. yaml配置
spring:
application:
name: demo
datasource:
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
username: XXX
password: XXX
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
3. 配置分页插件
@Configuration
public class MybatisConfiguration {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
4. dto层
一定要加上表名~
@Data
@TableName("tb_production")
public class Production {
private long productionId;
private BigDecimal price;
private Integer amount;
private String productionName;
private Date createTime;
}
5. mapper
记得继承实现 BaseMapper<>,查询一般的数据不用再自己写,自定义的查询可以写在里面
@Mapper
public interface ProductionMapper extends BaseMapper<Production> {
}
6. service
接口:
public interface ProductionService {
Page<Production> getProduction();
}
实现:
@Service
public class ProductionServiceImpl implements ProductionService {
// 引入mapper 最好用resource,@Autowired要报错,尽管不影响使用。(为什么报错?因为mapper是接口,不是一个实现类,idea对@Autowired不支持。
@Resource
private ProductionMapper productionMapper;
@Override
public Page<Production> getProduction() {
// 先生成一个page对象,用来装页的大小和当前页
Page<Production> page = new Page<>();
page.setSize(2);
page.setCurrent(1);
// QueryWrapper 用来增加条件查询
QueryWrapper<Production> queryWrapper = new QueryWrapper<>();
// 查询数量小于20的商品
queryWrapper.lt("amount", 20);
return productionMapper.selectPage(page, queryWrapper);
}
}
7. Controller
@GetMapping("/getProduction")
public Page<Production> getProduction() {
return productionService.getProduction();
}