当前位置: 首页 > news >正文

springboot业务层service开发全过程(以mybatis-plus为例)

在配置完数据层Dao/Mapper层的基础上,接下来我们要开始实现业务层的开发。

数据层和业务层的区别:

简单来说业务层是数据层的一个升级,从名字上也可以看出,数据层要想查询一个ID,都是需要定义SelectById这样的名称,但是在业务层直接就是GetById这种,只要结果,就跟主管一样,只要结果,干活的事情交给数据层去做了。

第一部分:在service包下新建实体类和接口

(1)FuelService接口

package com.example.service;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Fuel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class ServiceTests {@Autowiredprivate FuelService fuelService;@Testvoid contextLoads1() {//测试添加Fuel fuel=new Fuel(100L,"周煤",1.1,1.1,1.1,1.1,1.1,1.1);fuelService.save(fuel);}@Testvoid contextLoads2() {//测试更新Long id=100L;Fuel fuel=fuelService.getById(id);fuel.setFossilEnergyType("周1煤");fuelService.update(fuel);}@Testvoid contextLoads3() {//测试删除Long id=100L;fuelService.delete(id);}@Testvoid contextLoads4() {//测试根据id查询Long id=100L;fuelService.getById(id);}@Testvoid contextLoads5() {//测试查询全部fuelService.getAll();}@Testvoid contextLoads6() {//分页查询//分页IPage page=fuelService.getPage(2,5);//查询第二页,每页展示条数据page.getCurrent();//第二页//获取一页展示多少个数据page.getSize();//查询总共有多少条数据page.getTotal();//查询一共能够有多少页page.getPages();//列表形式,也就是查询到的数据page.getRecords();//输出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());}@Testvoid contextLoads7() {//条件查询-我们查询fossilEnergyType属性类别数据中含有“煤”的数据条String SearchName="煤";LambdaQueryWrapper<Fuel> wrapper=new LambdaQueryWrapper<>();if(SearchName!=null) {//避免查询的字段为null名字的字段wrapper.like(Fuel::getFossilEnergyType, SearchName);//第一个是属性名字,第二个是我们输入要like的内容}fuelService.getLikeAll(wrapper);}}

(2)FuelServiceImpl实现类

package com.example.service;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Fuel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class ServiceTests {@Autowiredprivate FuelService fuelService;@Testvoid contextLoads1() {//测试添加Fuel fuel=new Fuel(100L,"周煤",1.1,1.1,1.1,1.1,1.1,1.1);fuelService.save(fuel);}@Testvoid contextLoads2() {//测试更新Long id=100L;Fuel fuel=fuelService.getById(id);fuel.setFossilEnergyType("周1煤");fuelService.update(fuel);}@Testvoid contextLoads3() {//测试删除Long id=100L;fuelService.delete(id);}@Testvoid contextLoads4() {//测试根据id查询Long id=100L;fuelService.getById(id);}@Testvoid contextLoads5() {//测试查询全部fuelService.getAll();}@Testvoid contextLoads6() {//分页查询//分页IPage page=fuelService.getPage(2,5);//查询第二页,每页展示条数据page.getCurrent();//第二页//获取一页展示多少个数据page.getSize();//查询总共有多少条数据page.getTotal();//查询一共能够有多少页page.getPages();//列表形式,也就是查询到的数据page.getRecords();//输出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());}@Testvoid contextLoads7() {//条件查询-我们查询fossilEnergyType属性类别数据中含有“煤”的数据条String SearchName="煤";LambdaQueryWrapper<Fuel> wrapper=new LambdaQueryWrapper<>();if(SearchName!=null) {//避免查询的字段为null名字的字段wrapper.like(Fuel::getFossilEnergyType, SearchName);//第一个是属性名字,第二个是我们输入要like的内容}fuelService.getLikeAll(wrapper);}}

整体结构为:

(3)测试部分代码

package com.example.service;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Fuel;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class ServiceTests {@Autowiredprivate FuelService fuelService;@Testvoid contextLoads1() {//测试添加Fuel fuel=new Fuel(100L,"周煤",1.1,1.1,1.1,1.1,1.1,1.1);fuelService.save(fuel);}@Testvoid contextLoads2() {//测试更新Long id=100L;Fuel fuel=fuelService.getById(id);fuel.setFossilEnergyType("周1煤");fuelService.update(fuel);}@Testvoid contextLoads3() {//测试删除Long id=100L;fuelService.delete(id);}@Testvoid contextLoads4() {//测试根据id查询Long id=100L;fuelService.getById(id);}@Testvoid contextLoads5() {//测试查询全部fuelService.getAll();}@Testvoid contextLoads6() {//分页查询//分页IPage page=fuelService.getPage(2,5);//查询第二页,每页展示条数据page.getCurrent();//第二页//获取一页展示多少个数据page.getSize();//查询总共有多少条数据page.getTotal();//查询一共能够有多少页page.getPages();//列表形式,也就是查询到的数据page.getRecords();//输出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());}@Testvoid contextLoads7() {//条件查询-我们查询fossilEnergyType属性类别数据中含有“煤”的数据条String SearchName="煤";LambdaQueryWrapper<Fuel> wrapper=new LambdaQueryWrapper<>();if(SearchName!=null) {//避免查询的字段为null名字的字段wrapper.like(Fuel::getFossilEnergyType, SearchName);//第一个是属性名字,第二个是我们输入要like的内容}fuelService.getLikeAll(wrapper);}}

(1)测试新建

测试代码:

@Testvoid contextLoads1() {//测试添加Fuel fuel=new Fuel(100L,"周煤",1.1,1.1,1.1,1.1,1.1,1.1);fuelService.save(fuel);}

测试结果:

(2)测试修改

测试代码:

@Testvoid contextLoads2() {//测试更新Long id=100L;Fuel fuel=fuelService.getById(id);fuel.setFossilEnergyType("周1煤");fuelService.update(fuel);}

测试结果:

(3)测试删除

测试代码:

  @Testvoid contextLoads3() {//测试删除Long id=100L;fuelService.delete(id);}

测试结果:

(4)测试按照id查询

测试代码:

@Testvoid contextLoads4() {//测试根据id查询Long id=100L;fuelService.getById(id);}

测试结果:

(5)测试查询全部

测试代码:

 @Testvoid contextLoads5() {//测试查询全部fuelService.getAll();}

测试结果:

(6)测试分页查询

测试代码:

 @Testvoid contextLoads6() {//分页查询//分页IPage page=fuelService.getPage(2,5);//查询第二页,每页展示条数据page.getCurrent();//第二页//获取一页展示多少个数据page.getSize();//查询总共有多少条数据page.getTotal();//查询一共能够有多少页page.getPages();//列表形式,也就是查询到的数据page.getRecords();//输出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());}

测试结果:

(7)测试条件查询

测试代码:

 @Testvoid contextLoads7() {//条件查询-我们查询fossilEnergyType属性类别数据中含有“煤”的数据条String SearchName="煤";LambdaQueryWrapper<Fuel> wrapper=new LambdaQueryWrapper<>();if(SearchName!=null) {//避免查询的字段为null名字的字段wrapper.like(Fuel::getFossilEnergyType, SearchName);//第一个是属性名字,第二个是我们输入要like的内容}fuelService.getLikeAll(wrapper);}

测试结果:

文件获取:

①数据库

链接:https://pan.baidu.com/s/1edTUrhnzfp1gkDuq0wptJw?pwd=uxbh 
提取码:uxbh 
--来自百度网盘超级会员V5的分享

②源文件

链接:https://pan.baidu.com/s/1ozBWoj86Z0QDiimpcs6UUw?pwd=2umt 
提取码:2umt 
--来自百度网盘超级会员V5的分享

运行我打包的项目,为了能够正常运行(需要兼容maven以及java版本),具体的调整方法看我博客:http://t.csdnimg.cn/Uovig

好啦,希望能够帮助到大家!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • EF访问PostgreSql,如何判断jsonb类型的数组是否包含某个数值
  • k8s学习--k8s集群部署kubesphere的详细过程
  • 2024.8.1(前端服务器的配置以及tomcat环境的配置)
  • 对象转化成base64-再转回对象
  • 人数管控系统助力图书馆实现精准客流统计分析
  • uniapp微信小程序按钮分享定制动态传参
  • git回退未commit、回退已commit、回退已push、合并某一次commit到另一个分支
  • 下载安装docker并解决拉去镜像的connect:connection refused问题(2024.7.31亲测有效)
  • 【Linux】文件描述符 fd
  • uniapp手写滚动选择器
  • 开机WiFi没了只能宽带,连声音都有问题,服务里系统还原等一堆错误无法调试!——DHCP服务器常见的故障影响这么大?
  • Go 语言中如何使用指针
  • ABAP+json格式数据转换时参数为空没传值
  • Python 3.12新功能(1)
  • 【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第四篇 嵌入式Linux系统移植篇-第七十三章内核添加网卡驱动
  • 2017年终总结、随想
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • 3.7、@ResponseBody 和 @RestController
  • Asm.js的简单介绍
  • bearychat的java client
  • co模块的前端实现
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • java B2B2C 源码多租户电子商城系统-Kafka基本使用介绍
  • ng6--错误信息小结(持续更新)
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Spring声明式事务管理之一:五大属性分析
  • Vue--数据传输
  • vue总结
  • Yii源码解读-服务定位器(Service Locator)
  • 代理模式
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 动态魔术使用DBMS_SQL
  • 后端_ThinkPHP5
  • 简单实现一个textarea自适应高度
  • 前端存储 - localStorage
  • 前端性能优化--懒加载和预加载
  • 数据可视化之 Sankey 桑基图的实现
  • 王永庆:技术创新改变教育未来
  • 我有几个粽子,和一个故事
  • 用mpvue开发微信小程序
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ‌前端列表展示1000条大量数据时,后端通常需要进行一定的处理。‌
  • ###C语言程序设计-----C语言学习(3)#
  • #ubuntu# #git# repository git config --global --add safe.directory
  • $ git push -u origin master 推送到远程库出错
  • (Windows环境)FFMPEG编译,包含编译x264以及x265
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (四)Linux Shell编程——输入输出重定向
  • (一) springboot详细介绍
  • (一)Docker基本介绍
  • (一)UDP基本编程步骤
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。