SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展

本文主要是介绍SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

业务层开发

对于业务层的制作有个误区

Service层接口定义与数据层接口定义具有较大差别 不要混用

业务层接口关注的是业务名称

数据层接口关注的是数据层名称

操作是不难

但是有些东西还是要掌握的

业务层接口如果是业务方法 就按照业务名称来代替

如果是数据操作 直接用操作名称来代替

写接口

package com.bigdata1421.ssmp.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bigdata1421.ssmp.domain.User;import java.util.List;public interface UserService {Boolean save(User user);Boolean update(User user);Boolean delete(Integer id);User getById(Integer id);List<User> geTAll();IPage<User> getPage(int currentPage , int pageSize);}

接口写完了我们去写实现类

实现方法

@service 注解 定义成数据层对于的bean

@Autowired 注入

package com.bigdata1421.ssmp.service.impl;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata1421.ssmp.dao.UserDao;
import com.bigdata1421.ssmp.domain.User;
import com.bigdata1421.ssmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;// 定义成业务层对应的bean
@Service
public class UserServiceImpl implements UserService {//注入@Autowiredprivate UserDao userDao;@Overridepublic Boolean save(User user) {return userDao.insert(user)>0;}@Overridepublic Boolean update(User user) {return userDao.updateById(user)>0;}@Overridepublic Boolean delete(Integer id) {return userDao.deleteById(id)>0;}@Overridepublic User getById(Integer id) {return userDao.selectById(id);}@Overridepublic List<User> geTAll() {return userDao.selectList(null);}@Overridepublic IPage<User> getPage(int currentPage, int pageSize) {IPage page = new Page(currentPage,pageSize);userDao.selectPage(page,null);return page;}
}

测试

业务层的逻辑必须书写测试方法

package com.bigdata1421.ssmp.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata1421.ssmp.dao.UserDao;
import com.bigdata1421.ssmp.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.xml.ws.soap.Addressing;@SpringBootTest
public class UserserviceTestCase {@Autowiredprivate UserService userService;@Testvoid testGetById(){System.out.println(userService.getById(1));}@Testvoid testSave(){User user=new User();user.setId(11);user.setName("王连扬");user.setAge(21);user.setGender(1);userService.save(user);}@Testvoid testUpdate(){User user=new User();user.setId(11);user.setName("王连扬");user.setAge(21);user.setGender(1);userService.update(user);}@Testvoid testDelete(){userService.delete(11);}@Testvoid testGetAll(){userService.geTAll();}@Testvoid testGetPage(){IPage<User>page=userService.getPage(2,5);System.out.println(page.getCurrent());System.out.println(page.getSize());System.out.println(page.getTotal());System.out.println(page.getPages());System.out.println(page.getRecords());}}

测试已经通过

小结

定义方法

实现类

测试类

业务层快速开发

业务层的开发快死死了

我们一个一个的写 

其实不用

我们用Mybatis提供的业务层提供的公共接口即可实现功能的拓展

重写业务层接口

package com.bigdata1421.ssmp.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.bigdata1421.ssmp.domain.User;public interface IUserService extends IService<User> {
}

直接写实现类

在通用类的基础上做功能重载和功能追加

impl 实现接口

package com.bigdata1421.ssmp.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata1421.ssmp.dao.UserDao;
import com.bigdata1421.ssmp.service.IUserService;
import com.bigdata1421.ssmp.domain.User;
import org.springframework.stereotype.Service;@Service//定义成业务层的bean
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements IUserService {
}

开发测试

package com.bigdata1421.ssmp.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata1421.ssmp.dao.UserDao;
import com.bigdata1421.ssmp.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.xml.ws.soap.Addressing;@SpringBootTest
public class UserServiceTest {@Autowiredprivate IUserService iUserService ;@Testvoid testGetById(){System.out.println(iUserService.getById(1));}@Testvoid testSave(){User user=new User();user.setId(11);user.setName("王连扬");user.setAge(21);user.setGender(1);iUserService.save(user);}@Testvoid testUpdate(){User user=new User();user.setId(11);user.setName("王连扬");user.setAge(21);user.setGender(1);iUserService.updateById(user);}@Testvoid testDelete(){iUserService.removeById(1);}@Testvoid testGetAll(){iUserService.list();}@Testvoid testGetPage(){IPage<User>page=new Page<User>(2,5);iUserService.page(page);System.out.println(page.getCurrent());System.out.println(page.getSize());System.out.println(page.getTotal());System.out.println(page.getPages());System.out.println(page.getRecords());}}

测试通过

对于我们现在业务层接口和实现类

我们都是用提供的统用功能来实现的

我们有时候不一定要这样

我们需要在接口里手工编辑

在接口的实现类中实现方法

各种各样

所以我们在以后开发中是混合着用

小结

这篇关于SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999180

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分