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

相关文章

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二: