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

相关文章

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

mybatis-plus如何根据任意字段saveOrUpdateBatch

《mybatis-plus如何根据任意字段saveOrUpdateBatch》MyBatisPlussaveOrUpdateBatch默认按主键判断操作类型,若需按其他唯一字段(如agentId、pe... 目录使用场景方法源码方法改造首先在service层定义接口service层接口实现总结使用场景my

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模