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

SpringBoot案例(数据层、业务层、表现层)

1.创建项目

2.选择坐标

3.添加坐标

说明:为了便于开发,引入了lombak坐标。

        <!--添加mybatis-plus坐标--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3</version></dependency>
<!--        添加Druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency><!--      lombok  --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>

4.Lombok

说明:Lombok是java类库,提供了一组注解,简化POJO实体列开发 ;它有SpringBoot提供,无需指定版本。为当前实体类在编译期设置对应的get/set方法,toString方法,hashCode方法,equals方法等。

5.实体类

package com.forever.domain;import lombok.Data;@Data    //Data中封装了getter方法,setter方法,toString封装;没有构造方法public class User {private  Integer id;private  String username;private  String  password;private  String gender;private  String addrCity;}

6.数据层开发

说明:技术实现方案

MyBatisPlus;数据源用Druid;bookDao和bookMapper是同一个意思。

6.1dao层

package com.forever.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.forever.domain.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserDao extends BaseMapper<User> {}

6.2测试类

package com.forever.dao;import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class daoTest {@Autowiredprivate UserDao userDao;@Testvoid testGetByid() {System.out.println(userDao.selectById(1));}@Testvoid testInsert(){User user=new User();user.setGender("男");user.setUsername("天王");user.setPassword("123456");user.setAddrCity("成都市");userDao.insert(user);}}

6.3注意

问题:

解决:将id-type的值设置为auto

#配置数据库
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCusername: rootpassword: 123456
server:port: 80
mybatis-plus:global-config:db-config:table-prefix: tb_id-type: auto  #auto是自增策略 assign_id是雪花算法自增id

6.5成功 

 

7.mp日志

说明:mybatis-plus开启日志,  配置configuration下的log-impl的值就行了。

#配置数据库
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCusername: rootpassword: 123456
server:port: 80
mybatis-plus:global-config:db-config:table-prefix: tb_id-type: auto  #auto是自增策略 assign_id是雪花算法自增idconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  #标准输出日志

8.分页功能实现

说明:需要加入拦截器类

8.1拦截器类

package com.forever.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MPConfig {@Bean//创建了mybatis拦截器的壳public MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//拦截分页功能interceptor.addInnerInterceptor(new PaginationInnerInterceptor());return  interceptor;}}

8.2测试类

 说明:加入测试方法。

    @Testvoid testPage(){IPage page=new Page(1,5);userDao.selectPage(page,null);}

8.3成功 

 8.4知识点

说明:分页操作是在MyBatisPlus的常规操作基础上增强得到,内部是动态的拼写SQL语句,因此需要增强对应的功能,使用MyBatisPlus:拦截器实现。

9.按条件进行查询

9.1QueryWrapper

说明:QueryWrapper手工录入属性名;LambdaQueryWrapper不用手工录入属性名。

    @Testvoid testGetBy(){//按条件查询QueryWrapper qw = new QueryWrapper<>();qw.like("username","李");userDao.selectList(qw);}

9.2LambadQueryWrapper

    @Testvoid testGetBy2() {String name=null;//按条件查询LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();lqw.like(name!=null, User::getUsername, name);userDao.selectList(lqw);}

10.业务层

说明:创建了业务层接口,和实现类。

10.1业务接口

package com.forever.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forever.domain.User;import java.awt.print.Book;
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();//分页实现用int类型IPage<User> getPage(int currentPage,int pageSize);}

10.2业务实现类

package com.forever.service.impl;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forever.dao.UserDao;
import com.forever.domain.User;
import com.forever.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.awt.print.Book;
import java.util.List;@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;}
}

 10.3测试类

package com.forever.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class UserServiceTestCase {@Autowiredprivate  UserService userService;@Testvoid testGetById(){//关注的是数据能否正常的显示System.out.println( userService.getByID(1));;}@Testvoid  testGetAll(){User user=new User();user.setGender("男");user.setUsername("李四");user.setPassword("123456");user.setAddrCity("成都市");userService.save(user);}@Testvoid testGetPage(){IPage page=userService.getPage(1,5);System.out.println(page.getTotal());}}

 10.4简化业务层

说明:业务接口

package com.forever.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.forever.domain.User;//业务层接口public interface IUserService extends IService<User> {}

业务接口实现类

package com.forever.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forever.dao.UserDao;
import com.forever.domain.User;
import com.forever.service.IUserService;
import org.springframework.stereotype.Service;//泛型第一个是实现类,第二个模型类
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {
}

 测试类

package com.forever.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forever.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class UserServiceTestCase {@Autowiredprivate  IUserService userService;@Testvoid testGetById(){//关注的是数据能否正常的显示System.out.println( userService.getById(1));;}@Testvoid  testGetAll(){User user=new User();user.setGender("男");user.setUsername("李四");user.setPassword("123456");user.setAddrCity("成都市");userService.save(user);}@Testvoid testGetPage(){IPage<User> page=new Page<User>(1,5);userService.page(page);System.out.println(page.getTotal());}}

11.表现层

说明:基于Restful进行表现层接口开发;使用Postman测试表现层接口功能。

package com.forever.controller;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.forever.domain.User;
import com.forever.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate IUserService userService;@GetMappingpublic List<User> getAll(){return userService.list();}@PostMappingpublic  Boolean save(@RequestBody User user){return  userService.save(user);}@GetMapping("{id}")public User getByid(@PathVariable Integer id){return  userService.getById(id);}//占位符@GetMapping("{currentPage}/{pageSize}")public IPage<User> getPage(@PathVariable int currentPage,@PathVariable int pageSize){return userService.getPage(currentPage,pageSize);}}

 

相关文章:

SpringBoot案例(数据层、业务层、表现层)

1.创建项目 2.选择坐标 3.添加坐标 说明&#xff1a;为了便于开发&#xff0c;引入了lombak坐标。 <!--添加mybatis-plus坐标--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><ver…...

交叉编译程序:以 freetype 为例

1 程序运行的一些基础知识 1.1 编译程序时去哪找头文件&#xff1f; 系统目录&#xff1a;就是交叉编译工具链里的某个 include 目录&#xff1b;也可以自己指定&#xff1a;编译时用 “ -I dir ” 选项指定。 1.2 链接时去哪找库文件&#xff1f; 系统目录&#…...

spring-cloud-starter-dubbo不设置心跳间隔导致生产者重启no Provider问题记录

版本 spring-cloud-starter-dubbo-2.2.4.RELEASE 问题描述 生产者重启后&#xff0c;正常注册到注册中心&#xff0c;但是消费者调用接口是no provider&#xff0c;偶现&#xff0c;频繁出现 解决办法 先说原因和解决办法&#xff0c;有兴趣可以看下问题的排查过程。 原因…...

【数据结构】败者树的建树与比较过程

文章目录 前置知识归并段 建树过程比较过程疑问为什么比较次数减少了&#xff1f;如果某个归并段的元素一直获胜&#xff0c;没有元素了怎么办&#xff1f;处理方法 1处理方法 2 前置知识 归并段 外部排序算法通常用于处理大规模数据&#xff0c;其中数据量远超过计算机内存的…...

GlobalMapper---dem生成均匀分布的网格,或者均匀分布的点高程点

1打开DEM数据。点击工具栏上的Open Data File(s)按钮&#xff0c;打开DEM数据 2点击【Create Grid】按钮 3生成点 4导出格式xyz 5南方cass展点 6过滤抽稀...

k8s系列文章一:安装指南

前言 k8s是docker的升级版&#xff0c;可用于docker集群配置管理微服务 一、更新ubuntu系统版本 sudo apt update sudo apt upgrade二、添加GPG密钥(阿里源) 尽管我不知道gpg是个什么东西&#xff0c;反正跟着做就完了 curl https://mirrors.aliyun.com/kubernetes/apt/do…...

Pod 进阶

目录 1、资源限制 1.1 官网示例 1.2 CPU 资源单位 1.3 内存 资源单位 2、健康检查&#xff1a;又称为探针&#xff08;Probe&#xff09; 2.1 探针的三种规则 2.2 Probe支持三种检查方法 2.3 官网示例 3、扩展 pod的状态 3.1 Container生命周期 1、资源限制 当定义…...

Proteus仿真--12864LCD显示计算器键盘按键实验(仿真文件+程序)

本文主要介绍基于51单片机的12864LCD液晶显示电话拨号键盘按键实验&#xff08;完整仿真源文件及代码见文末链接&#xff09; 仿真图如下 本设计主要介绍计算器键盘仿真&#xff0c;按键按下后在12864液晶上显示对应按键键值 仿真运行视频 Proteus仿真--12864LCD显示计算器…...

pam_radius库的使用

一. 前言 我们知道&#xff0c;linux pam库是一系列的库&#xff0c;用于处理一些应用程序的认证工作&#xff0c;比如login程序。但是默认的pam库只是用于本地认证&#xff0c;也就是认证的用户名和密码存储在本机上。如果需要远程认证&#xff0c;比如向radius服务器认证&…...

qt6:无法使用setFontColor

问题描述 跟着C开发指南视频学习&#xff0c;但是发现无论是直接使用ui设计&#xff0c;还是纯代码都无法实现变更字体颜色的功能。图中显示&#xff0c;点击颜色控件后&#xff0c;文本框的文字加粗、下划线、斜体等才能设置&#xff0c;但是无法变更颜色。 此文提醒qt sty…...

竞赛 深度学习疫情社交安全距离检测算法 - python opencv cnn

文章目录 0 前言1 课题背景2 实现效果3 相关技术3.1 YOLOV43.2 基于 DeepSort 算法的行人跟踪 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; **基于深度学习疫情社交安全距离检测算法 ** 该项目较为新颖&#xff0c;适合作为竞赛…...

无声的世界,精神科用药并结合临床的一些分析及笔记(十)

目录 回 “ 家 ” 克服恐惧 奥沙西泮 除夕 酒与药 警告 离别 回 “ 家 ” 她的锥切手术进行的很顺利&#xff0c;按计划继续返回安定医院调节心理状态&#xff0c;病友们都盼着我们回“家”。当我俩跨入病区&#xff0c;大家都涌过来帮我们大包小包的拎着行李&#xff0…...

构建强大的Web应用之Django详解

引言&#xff1a; Django是一个功能强大且灵活的Python Web框架&#xff0c;它提供了一套完整的工具和功能&#xff0c;帮助开发者快速构建高效的Web应用。本篇文章将带您逐步了解Django的基本概念和使用方法&#xff0c;并通过实际的代码案例&#xff0c;帮助您从零开始构建自…...

Linux 之搭建 arm 的 qemu 模拟器

目录 1. Linux 之搭建 arm 的 qemu 模拟器 1. Linux 之搭建 arm 的 qemu 模拟器 OS: kali 1. 安装交叉编译工具、GDB 和 QEMU # sudo apt-get install qemu debootstrap qemu-user-static # sudo apt-get install qemu-system-arm # sudo apt-get install gdb-multiarch //支持…...

uinapp微信小程序隐私政策授权

&#x1f680; 隐私弹窗效果图&#xff1a; 1、启用隐私相关功能在manifest.json文件中配置 usePrivacyCheck: true "mp-weixin" : {"__usePrivacyCheck__" : true, },2、创建组件 <template><view><!-- 隐私政策弹窗 --><uni-popu…...

使用Java工作流简单介绍

本人详解 作者:王文峰,参加过 CSDN 2020年度博客之星,《Java王大师王天师》 公众号:JAVA开发王大师,专注于天道酬勤的 Java 开发问题中国国学、传统文化和代码爱好者的程序人生,期待你的关注和支持!本人外号:神秘小峯 山峯 转载说明:务必注明来源(注明:作者:王文峰…...

数字媒体技术基础之:ICC 配置文件

ICC 配置文件&#xff08;也称为 ICC 色彩配置文件或 ICC 色彩描述文件&#xff09;是由国际色彩联盟&#xff08;International Color Consortium, ICC&#xff09;制定的一种标准文件格式&#xff0c;用于在不同的设备和软件之间保持颜色的一致性。 ICC 配置文件包含有关设备…...

解析SD-WAN组网方式及应用场景,全面了解典型案例

随着企业业务高速发展&#xff0c;跨区域开展业务首要解决的难题是构建各站点能互联互通的网络&#xff0c;然而目前大多数企业在广域网优化的问题上依旧碰壁&#xff0c;主要原因是企业广域网面临的挑战并不能马上得到解决。 传统网络互联方案无论是IPsec还是专线&#xff0c…...

中小学智慧校园电子班牌管理系统源码

智慧校园云平台电子班牌系统&#xff0c;利用先进的云计算技术&#xff0c;将教育信息化资源和教学管理系统进行有效整合&#xff0c;实现基础数据共享、应用统一管理。借助全新的智能交互识别终端和移动化教育管理系统&#xff0c;以考勤、课表、通知、家校互通等功能为切入点…...

日常踩坑-[sass]Error: Expected newline

在学习sass的时候&#xff0c;运行时发现报错 经过网上冲浪知道&#xff0c;原来在声明语言的时候 lang 不能声明为 sass &#xff0c;而是 scss ,这就有点坑了 原因&#xff1a; scss是sass3引入进来的&#xff0c;scss语法有"{}“,”;"而sass没有&#xff0c;所以…...

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…...

CTF show Web 红包题第六弹

提示 1.不是SQL注入 2.需要找关键源码 思路 进入页面发现是一个登录框&#xff0c;很难让人不联想到SQL注入&#xff0c;但提示都说了不是SQL注入&#xff0c;所以就不往这方面想了 ​ 先查看一下网页源码&#xff0c;发现一段JavaScript代码&#xff0c;有一个关键类ctfs…...

前端导出带有合并单元格的列表

// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...

转转集团旗下首家二手多品类循环仓店“超级转转”开业

6月9日&#xff0c;国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解&#xff0c;“超级…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

代码随想录刷题day30

1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币&#xff0c;另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额&#xff0c;返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

R语言速释制剂QBD解决方案之三

本文是《Quality by Design for ANDAs: An Example for Immediate-Release Dosage Forms》第一个处方的R语言解决方案。 第一个处方研究评估原料药粒径分布、MCC/Lactose比例、崩解剂用量对制剂CQAs的影响。 第二处方研究用于理解颗粒外加硬脂酸镁和滑石粉对片剂质量和可生产…...

【LeetCode】3309. 连接二进制表示可形成的最大数值(递归|回溯|位运算)

LeetCode 3309. 连接二进制表示可形成的最大数值&#xff08;中等&#xff09; 题目描述解题思路Java代码 题目描述 题目链接&#xff1a;LeetCode 3309. 连接二进制表示可形成的最大数值&#xff08;中等&#xff09; 给你一个长度为 3 的整数数组 nums。 现以某种顺序 连接…...

脑机新手指南(七):OpenBCI_GUI:从环境搭建到数据可视化(上)

一、OpenBCI_GUI 项目概述 &#xff08;一&#xff09;项目背景与目标 OpenBCI 是一个开源的脑电信号采集硬件平台&#xff0c;其配套的 OpenBCI_GUI 则是专为该硬件设计的图形化界面工具。对于研究人员、开发者和学生而言&#xff0c;首次接触 OpenBCI 设备时&#xff0c;往…...

nnUNet V2修改网络——暴力替换网络为UNet++

更换前,要用nnUNet V2跑通所用数据集,证明nnUNet V2、数据集、运行环境等没有问题 阅读nnU-Net V2 的 U-Net结构,初步了解要修改的网络,知己知彼,修改起来才能游刃有余。 U-Net存在两个局限,一是网络的最佳深度因应用场景而异,这取决于任务的难度和可用于训练的标注数…...