在写东西的过程中,多表联查和分页功能必不可少。当然,crud也很重要
但是又不想写代码和xml。
通过苦苦的查找。发现MyBatis-Plus一款国产的框架。优化了许多操作
本次主要记录一下,多表联查和分页的使用。
Pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
//Spring boot方式
@EnableTransactionManagement
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
操作完以上步骤(实体 mapper controller 等已建好)
@ResponseBody
@GetMapping("/artList")
public Map<String,Object>articleList(int page,int limit,Article article){
Page<Article> pageArt=new Page<Article>(page,limit);
Page<Article> page1 = articleMapper.findAllAndPage(pageArt); //自定义方法,多表
// QueryWrapper<Article> diseaseQueryWrapperw = new QueryWrapper<Article>(article);
// IPage<Article> page1 = articleService.page(pageArt,diseaseQueryWrapperw); //自带的分页查询。只能单表
// List<Article> list = articleService.list();
int total = (int)page1.getTotal();
return TableMap.ResultJson(0,total,"ok",page1.getRecords()); //layui table 解析返回格式
}
mapper Xml
<select id="findAllAndPage" resultType="com.chaoba.shirodemo1.model.Article">
SELECT a.id,a.title,a.uid,a.isDel,a.createTime,a.type,ae.`name` FROM article a JOIN article_enum ae ON a.type=ae.id
</select>
mapper接口
public interface ArticleMapper extends BaseMapper<Article> {
Page<Article> findAllAndPage( Page<Article> page);
}