配置分页查询拦截器
package com.fox.mp.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PageConfig {
/**
* 3.4.0之前的版本
* @return
*/
/* @Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}*/
/**
* 3.4.0之后版本
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
进行分页查询
分析:其实我们一般不会利用到selectPage的返回值,这时因为其返回值其实还是我们传递page对象本身,因此我们一般不对其返回值进行利用。
最终版本:
@Test
public void testPage(){
IPage page = new Page<>();
//设置每页条数
page.setSize(2);
//设置查询第几页
page.setCurrent(1);
userMapper.selectPage(page, null);
System.out.println(page.getRecords());//获取当前页的数据
System.out.println(page.getTotal());//获取总记录数
System.out.println(page.getCurrent());//当前页码
}
①定义接口,定义方法 方法第一个测试定义成Page类型:
package com.fox.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fox.mp.domain.Orders;
import org.springframework.core.annotation.Order;
public interface OrderMapper extends BaseMapper {
IPage findAllOrders(Page page);
}
在xml文件中无需关注分页具体操作,MP中我们刚刚配置的拦截器会帮我们实现:
方法测试调用如下:
@Autowired
private OrderMapper orderMapper;
@Test
public void testPage02(){
Page page = new Page<>();
//设置每页大小
page.setSize(2);
//设置当前页码
page.setCurrent(2);
orderMapper.findAllOrders(page);
System.out.println(page.getRecords());
System.out.println(page.getTotal());
}
MP也为我们提供了Service层的实现。我们只需要编写一个接口,继承 IService,并创建一个接口实现类继承ServiceImpl,即可使用。
相比于Mapper接口,Service层主要是支持了更多批量操作的方法。
改造前
定义接口
public interface UserService {
List list();
}
定义实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List list() {
return userMapper.selectList(null);
}
}
改造后
接口
public interface UserService extends IService {
}
实现类
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
}
测试:
@Autowired
private UserService userService;
@Test
public void testService() {
List list = userService.list();
System.out.println(list);
}
代码展示:
代码如下:
接口:
package com.fox.mp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fox.mp.domain.User;
public interface UserService extends IService {
User test();
}
自定义实现类:
package com.fox.mp.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fox.mp.domain.Orders;
import com.fox.mp.domain.User;
import com.fox.mp.mapper.OrderMapper;
import com.fox.mp.mapper.UserMapper;
import com.fox.mp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
@Autowired
private OrderMapper orderMapper;
@Override
public User test() {
UserMapper userMapper = getBaseMapper();
List orders = orderMapper.selectList(null);
User user = userMapper.selectById(1);
//查询用户对应的订单
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Orders::getId,3);
List ordersList = orderMapper.selectList(queryWrapper);
return user;
}
}
分析: