MyBatisPlus之分页查询及Service接口运用
一、分页查询
1.1 基本分页查询
配置分页查询拦截器
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<User> 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());//当前页码
}
1.2 多表分页查询
①定义接口,定义方法 方法第一个测试定义成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<Order> {
IPage<Orders> findAllOrders(Page<Orders> page);
}
在xml文件中无需关注分页具体操作,MP中我们刚刚配置的拦截器会帮我们实现:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.fox.mp.mapper.OrderMapper">
<select id="findAllOrders" resultType="com.fox.mp.domain.Orders">
SELECT
o.*,u.`user_name`
FROM
TB_USER u,orders o
WHERE
o.`user_id` = u.`id`
</select>
</mapper>
方法测试调用如下:
@Autowired
private OrderMapper orderMapper;
@Test
public void testPage02(){
Page<Orders> page = new Page<>();
//设置每页大小
page.setSize(2);
//设置当前页码
page.setCurrent(2);
orderMapper.findAllOrders(page);
System.out.println(page.getRecords());
System.out.println(page.getTotal());
}
二、Service 层接口
MP也为我们提供了Service层的实现。我们只需要编写一个接口,继承 IService,并创建一个接口实现类继承ServiceImpl,即可使用。
相比于Mapper接口,Service层主要是支持了更多批量操作的方法。
2.1 基本使用
改造前
定义接口
public interface UserService {
List<User> list();
}
定义实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> list() {
return userMapper.selectList(null);
}
}
改造后
接口
public interface UserService extends IService<User> {
}
实现类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}
测试:
@Autowired
private UserService userService;
@Test
public void testService() {
List<User> list = userService.list();
System.out.println(list);
}
代码展示:
2.2 自定义方法
代码如下:
接口:
package com.fox.mp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fox.mp.domain.User;
public interface UserService extends IService<User> {
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<UserMapper,User> implements UserService {
@Autowired
private OrderMapper orderMapper;
@Override
public User test() {
UserMapper userMapper = getBaseMapper();
List<Orders> orders = orderMapper.selectList(null);
User user = userMapper.selectById(1);
//查询用户对应的订单
LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Orders::getId,3);
List<Orders> ordersList = orderMapper.selectList(queryWrapper);
return user;
}
}
分析: