当前位置: 首页 > 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;所以…...

UE5 学习系列(二)用户操作界面及介绍

这篇博客是 UE5 学习系列博客的第二篇&#xff0c;在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下&#xff1a; 【Note】&#xff1a;如果你已经完成安装等操作&#xff0c;可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作&#xff0c;重…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

高频面试之3Zookeeper

高频面试之3Zookeeper 文章目录 高频面试之3Zookeeper3.1 常用命令3.2 选举机制3.3 Zookeeper符合法则中哪两个&#xff1f;3.4 Zookeeper脑裂3.5 Zookeeper用来干嘛了 3.1 常用命令 ls、get、create、delete、deleteall3.2 选举机制 半数机制&#xff08;过半机制&#xff0…...

【python异步多线程】异步多线程爬虫代码示例

claude生成的python多线程、异步代码示例&#xff0c;模拟20个网页的爬取&#xff0c;每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程&#xff1a;允许程序同时执行多个任务&#xff0c;提高IO密集型任务&#xff08;如网络请求&#xff09;的效率…...

【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具

第2章 虚拟机性能监控&#xff0c;故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令&#xff1a;jps [options] [hostid] 功能&#xff1a;本地虚拟机进程显示进程ID&#xff08;与ps相同&#xff09;&#xff0c;可同时显示主类&#x…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)

上一章用到了V2 的概念&#xff0c;其实 Fiori当中还有 V4&#xff0c;咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务)&#xff0c;代理中间件&#xff08;ui5-middleware-simpleproxy&#xff09;-CSDN博客…...

保姆级教程:在无网络无显卡的Windows电脑的vscode本地部署deepseek

文章目录 1 前言2 部署流程2.1 准备工作2.2 Ollama2.2.1 使用有网络的电脑下载Ollama2.2.2 安装Ollama&#xff08;有网络的电脑&#xff09;2.2.3 安装Ollama&#xff08;无网络的电脑&#xff09;2.2.4 安装验证2.2.5 修改大模型安装位置2.2.6 下载Deepseek模型 2.3 将deepse…...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...