MybatisPlus——MybatisPlus封装service

web项目的结构分为三层:DAO层,Service层、Web层
MybatisPlus入门demo(上一篇)使用mybatis封装Dao层,本篇文章使用MybatisPlus封装service

基础理论

MP封装services层:
1、添加Service接口**继承 IService<实体类>
2、添加Service实现类实现接口、继承Servicelmpl<mapper、实体类>

1、创建Service接口

IService 是由MP封装的。

package com.atguigu.auth.service;

import com.atguigu.model.system.SysRole;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface sysRoleService extends IService<SysRole> {
}

2、创建Service实现类

ServiceImpl是由MP封装的
ServiceImpl<SysRoleMapper, SysRole> 是将SysRoleMapper进行自动注入并且设置为baseMapper,baseMapper里面有封装好的CRUD操作

package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysRoleMapper;
import com.atguigu.model.system.SysRole;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.auth.service.sysRoleService;

@Service
public class sysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements sysRoleService {
	//ServiceImpl 源码里实现了自动注入,可是要是不写下面两行手动注入会找不到唯一的bean不知道为什么
    @Autowired
    private SysRoleMapper sysRoleMapper;
}

3、测试

注入service进行测试

package com.atguigu.auth;

import com.atguigu.model.system.SysRole;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.atguigu.auth.service.sysRoleService;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class TestDemo2 {
    @Autowired
    private sysRoleService service;

    @Test
    public void getAll() {
        List<SysRole> list = service.list();
        System.out.println(list);
    }
}

4、结果

在这里插入图片描述

### MyBatisPlus 分页插件使用教程 #### 插件集成与配置 为了使项目能够利用 MyBatisPlus 提供的强大分页能力,需先完成必要的依赖引入以及相应配置。对于采用 Spring Boot 构建的应用程序而言,仅需在 `pom.xml` 或者 `build.gradle` 文件内加入对应的 Maven/Gradle 依赖即可[^1]。 接着,在项目的启动类或是任意一处被 `@Configuration` 注解标记的 Java 类里注册分页拦截器组件: ```java import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; @Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 上述代码展示了如何创建并返回一个新的 `PaginationInterceptor` 实例来激活分页功能[^2]。 #### Mapper接口定义 假设存在一个名为 `UserMapper.java` 的映射接口用于访问数据库表 users,则可以在该接口内部声明如下形式的方法以便于后续发起带有限定条件的数据检索请求: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { /** * 自定义分页查询方法. */ IPage<User> selectUsersByPage(Page<User> page); } ``` 这里值得注意的是参数列表里的第一个位置应当放置代表页面信息的对象实例——即实现了 `IPage<T>` 接口的具体类型对象,比如官方推荐使用的 `Page<T>` 泛型类[^3]。 #### Service层逻辑编写 继续沿用上面提到的例子,现在转向业务服务层实现部分。通常情况下会有一个关联的服务接口和服务实现类组合负责处理来自控制器层面传递过来的操作指令并向其反馈最终结果集。下面给出了一段简单的伪代码用来描述这一过程: ```java @Service public class UserServiceImpl implements IUserService { private final UserMapper userMapper; public UserServiceImpl(UserMapper userMapper) { this.userMapper = userMapper; } @Override public PageResult getUsers(int currentPage, int pageSize) { // 创建分页对象 Page<User> page = new Page<>(currentPage, pageSize); // 执行分页查询 IPage<User> result = userMapper.selectUsersByPage(page); // 将查询到的结果封装成统一响应格式返回给前端 return new PageResult(result.getRecords(), (int)result.getTotal()); } } ``` 此处在获取到了由 DAO 层传回的实际记录集合之后还进行了二次加工转换动作,目的是为了让客户端更容易理解和消费这些结构化后的数据[^4]。 #### 控制器端点设计 最后一步就是暴露 RESTful API 给外部调用了。借助 Spring MVC 提供的支持可以轻松做到这一点: ```java @RestController @RequestMapping("/api/users") public class UserController { private final IUserService userService; public UserController(IUserService userService) { this.userService = userService; } @GetMapping("") public ResponseEntity<PageResult> list(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size){ try{ PageResult response = userService.getUsers(current, size); return ResponseEntity.ok(response); }catch(Exception e){ log.error(e.getMessage(),e); throw new RuntimeException("Failed to fetch paginated data"); } } } ``` 以上便是整个流程的大致轮廓,从最底层的数据源连接直至最高层次的表现层交互都涵盖了进去。当然实际应用场景可能会更加复杂多变,不过只要掌握了这套基本模式就能举一反三地应对各种情况了[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值