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

基于大模型的 UI 自动化系统

基于大模型的 UI 自动化系统 下面是一个完整的 Python 系统,利用大模型实现智能 UI 自动化,结合计算机视觉和自然语言处理技术,实现"看屏操作"的能力。 系统架构设计 #mermaid-svg-2gn2GRvh5WCP2ktF {font-family:"trebuchet ms",verdana,arial,sans-…...

Linux 文件类型,目录与路径,文件与目录管理

文件类型 后面的字符表示文件类型标志 普通文件&#xff1a;-&#xff08;纯文本文件&#xff0c;二进制文件&#xff0c;数据格式文件&#xff09; 如文本文件、图片、程序文件等。 目录文件&#xff1a;d&#xff08;directory&#xff09; 用来存放其他文件或子目录。 设备…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

EtherNet/IP转DeviceNet协议网关详解

一&#xff0c;设备主要功能 疆鸿智能JH-DVN-EIP本产品是自主研发的一款EtherNet/IP从站功能的通讯网关。该产品主要功能是连接DeviceNet总线和EtherNet/IP网络&#xff0c;本网关连接到EtherNet/IP总线中做为从站使用&#xff0c;连接到DeviceNet总线中做为从站使用。 在自动…...

html-<abbr> 缩写或首字母缩略词

定义与作用 <abbr> 标签用于表示缩写或首字母缩略词&#xff0c;它可以帮助用户更好地理解缩写的含义&#xff0c;尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时&#xff0c;会显示一个提示框。 示例&#x…...

LabVIEW双光子成像系统技术

双光子成像技术的核心特性 双光子成像通过双低能量光子协同激发机制&#xff0c;展现出显著的技术优势&#xff1a; 深层组织穿透能力&#xff1a;适用于活体组织深度成像 高分辨率观测性能&#xff1a;满足微观结构的精细研究需求 低光毒性特点&#xff1a;减少对样本的损伤…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...

第八部分:阶段项目 6:构建 React 前端应用

现在&#xff0c;是时候将你学到的 React 基础知识付诸实践&#xff0c;构建一个简单的前端应用来模拟与后端 API 的交互了。在这个阶段&#xff0c;你可以先使用模拟数据&#xff0c;或者如果你的后端 API&#xff08;阶段项目 5&#xff09;已经搭建好&#xff0c;可以直接连…...

2025年- H71-Lc179--39.组合总和(回溯,组合)--Java版

1.题目描述 2.思路 当前的元素可以重复使用。 &#xff08;1&#xff09;确定回溯算法函数的参数和返回值&#xff08;一般是void类型&#xff09; &#xff08;2&#xff09;因为是用递归实现的&#xff0c;所以我们要确定终止条件 &#xff08;3&#xff09;单层搜索逻辑 二…...

数据库正常,但后端收不到数据原因及解决

从代码和日志来看&#xff0c;后端SQL查询确实返回了数据&#xff0c;但最终user对象却为null。这表明查询结果没有正确映射到User对象上。 在前后端分离&#xff0c;并且ai辅助开发的时候&#xff0c;很容易出现前后端变量名不一致情况&#xff0c;还不报错&#xff0c;只是单…...