mybatis-plus自定义sql分页

1.在pom文件中引入mybatis-plus

<dependency>
	<groupId>com.baomidougroupId>
	<artifactId>mybatis-plus-boot-starterartifactId>
	<version>3.2.0version>
dependency>

2.新建一个mybatis-plus配置类

@Configuration
@EnableTransactionManagement
@MapperScan("mapper包")
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor =new PaginationInterceptor();
        return paginationInterceptor;
    }
}

3.在mapper.xml中添加自定义sql


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper所在包">
    <select id="getBlogByType" resultType="返回值类型">
        select *
        from blog
        where type_id =
        (select id from type
        where type_name = #{typeName})
    select>
mapper>

4.在mapper中添加方法

IPage<BlogDto> getBlogByType(IPage<Blog> page ,String typeName);

5.service中增加方法

IPage<BlogDto> getBlogByType(IPage<Blog> page , String typeName);

6.serviceImpl中重写方法

	@Autowired
    BlogMapper blogMapper;
    @Override
    public IPage<BlogDto> getBlogByType(IPage<Blog> page, String typeName) {
        return blogMapper.getBlogByType(page,typeName);
    }

7.在controller中调用方法

   @PostMapping("/blogsByType")
   public Object showBlogsByTpye(@RequestBody BlogsByTypeDto blogsByTypeDto){
       Page page = new Page(blogsByTypeDto.getCurrentPage(),5);
       IPage blogs = blogService.getBlogByType(page,blogsByTypeDto.getTypeName());
       return Result.succ(blogs);
   }

你可能感兴趣的:(mybatis,sql,java)