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

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

springboot整合TDengine全过程

《springboot整合TDengine全过程》:本文主要介绍springboot整合TDengine全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录环境准备JDBC-JNI方式准备依赖实体类Mapper配置类测试类RESTful方式实体类配置类测试类总结

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加