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
好啦,希望能够帮助到大家!