MyBatis-Plus3分页注解⽅式MyBatis-Plus3 分页注解⽅式
官⽹例⼦
selectPage :
/**
* <p>
* 根据 entity 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
* @return 实体分页对象
*/
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
第⼀种⽅法,根据官⽅给的例⼦配置:
1.先给MybatisPlusConfig添加
package com.duboo.fig;
import sion.plugins.PaginationInterceptor;
batis.spring.annotation.MapperScan;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
@MapperScan("com.duboo.provider.dubboprovider.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
2.service层
selectPage是继承了mapper,直接调⽤
pageNo是设置第⼏页
pageSize是每页显⽰
package com.duboo.provider.vie;
import com.fig.annotation.Service;
import ditions.query.QueryWrapper;
import adata.IPage;
import sion.plugins.pagination.Page;
import com.dubbo.iy.movie.Movie;
import com.dubbo.api.vie.MovieService;
import com.duboo.provider.vie.MovieMapper;
import javax.annotation.Resource;
import java.util.List;
/**
* 描述:
* Date: 2019/7/11
*/
@Service
public class MovieServiceImpl implements MovieService {
@Resource
private MovieMapper movieMapper;
@Override
public IPage<Movie>searchMovie(String name,int pageNo,int pageSize){
Page<Movie> page =new Page<>(pageNo,pageSize);
return movieMapper.selectPage(page,new QueryWrapper<Movie>().eq("name", name)); }
}
@test
@Test
public void test()
{
IPage page= movieService.searchMovie("我不是药神",1,5);
Gson gson =new Gson();
System.out.Json(page));
}
第⼆种⽅法,⾃⼰注释写SQL
1.先给MybatisPlusConfig添加
package com.duboo.fig;
import sion.plugins.PaginationInterceptor; batis.spring.annotation.MapperScan;
import t.annotation.Bean;
import t.annotation.Configuration;
@Configuration
@MapperScan("com.duboo.provider.dubboprovider.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
2.service层
selectPage是继承了mapper,直接调⽤
pageNo是设置第⼏页
pageSize是每页显⽰
package com.duboo.provider.vie;
import com.fig.annotation.Service;
import ditions.query.QueryWrapper; import adata.IPage;
import sion.plugins.pagination.Page; import com.dubbo.iy.movie.Movie;
import com.dubbo.api.vie.MovieService;
import com.duboo.provider.vie.MovieMapper;
import javax.annotation.Resource;
import java.util.List;
/**
* 描述:
* Date: 2019/7/11
*/
@Service
public class MovieServiceImpl implements MovieService {
@Resource
private MovieMapper movieMapper;
@Override
public IPage<Movie>searchMovie(String name,int pageNo,int pageSize){ Page<Movie> page =new Page<>(pageNo,pageSize);
return movieMapper.selectMovieList(page,name);
}
}
3.MovieMapper
package com.duboo.provider.vie;
import apper.BaseMapper;
import adata.IPage;
import sion.plugins.pagination.Page;
import com.dubbo.iy.movie.Movie;
import org.apache.ibatis.annotations.Select;
/**
* 描述:
* Author: fuweirong 【wr.fu@vdyoo】
* Date: 2019/7/11
*/
public interface MovieMapper extends BaseMapper<Movie>{
/**
* 分页查询条件是:like name,ordername
* @param page
* @param name
* @return
*/
@Select("select * from movie where CONCAT(`name`,`ordername`) LIKE CONCAT ('%', #{name},'%')") IPage<Movie>selectMovieList(Page<Movie> page, String name);
}
@test分页查询插件
@Test
public void test()
{
IPage page= movieService.searchMovie("我不是药神",1,5);
Gson gson =new Gson();
System.out.Json(page));
}
查询结果
{
"records":[
{
"id":1,
"name":"我不是药神",
"code":"",
"year":2018
}
],
"total":1,
"size":5,
"current":1,
"orders":[
],
"optimizeCountSql":true,
"isSearchCount":true
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论