当前位置: 首页 > 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设计感蓝色商务数据后台网站模板源码

蓝色商务数据后台网站模板是一款适合网站模板下载。提示&#xff1a;本模板调用到谷歌字体库&#xff0c;可能会出现页面打开比较缓慢。 演示下载 qnziyw点cn/wysc/qdmb/20852点html...

二、计算机组成原理与体系结构

&#xff08;一&#xff09;数据的表示 不同进制之间的转换 R 进制转十进制使用按权展开法&#xff0c;其具体操作方式为&#xff1a;将 R 进制数的每一位数值用 Rk 形式表示&#xff0c;即幂的底数是 R &#xff0c;指数为 k &#xff0c;k 与该位和小数点之间的距离有关。当…...

MySQL-sql的优化

表的设计优化索引优化SQL语句优化主从复制、读写分离分库分表 表的设计优化(参考阿里开发手册) 比如设置合适的数值(tinyint int bigint)&#xff0c;要根据实际情况选择 比如设置合适的字符串类型(char和varchar) char定长效率高&#xff0c;varchar可变长度&#xff0c;效…...

一致性哈希在分库分表的应用

文章目录 前言分库分表方法一致性哈希介绍分库分表的应用 总结 前言 大家应该都知道一些哈希算法&#xff0c;比如MD5、SHA-1、SHA-256等&#xff0c;通常被用于唯一标识、安全加密、数据校验等场景。除此之外&#xff0c;还有一种应用是对某个数据进行哈希取模映射到一个有限…...

PostCSS通过px2rem插件和lib-flexible将px单位转换为rem(root em)单位实现大屏适配

目录 文档postcss中使用postcss-plugin-px2rem安装postcss-plugin-px2rem示例默认配置 webpack中使用postcss-plugin-px2rem项目结构安装依赖文件内容 大屏适配参考文章 文档 类似的插件 postcss-plugin-px2rem https://www.npmjs.com/package/postcss-plugin-px2remhttps://g…...

什么是缓冲区溢出?

缓冲区溢出 1. 什么是缓冲区溢出2. 缓冲区溢出攻击的类型3. 攻击者如何利用缓冲区溢出4. 如何防止缓冲区溢出攻击 1. 什么是缓冲区溢出 &#xff08;1&#xff09;缓冲区 缓冲区是一块连续的计算机内存区域&#xff0c;用于在将数据从一个位置移到另一位置时临时存储数据。这…...

论文浅尝 | ChatKBQA:基于微调大语言模型的知识图谱问答框架

第一作者&#xff1a;罗浩然&#xff0c;北京邮电大学博士研究生&#xff0c;研究方向为知识图谱与大语言模型协同推理 OpenKG地址&#xff1a;http://openkg.cn/tool/bupt-chatkbqa GitHub地址&#xff1a;https://github.com/LHRLAB/ChatKBQA 论文链接&#xff1a;https://ar…...

软件测试的目的---防范项目风险

软件测试的目的到底是什么一直是困扰开发人员和测试人员的一个问题, 项目管理人员希望测试能够保证软件项目的成功 开发人员希望希望测试可以让他们理直气壮的说,他们的软件是没有问题的,从而证明他们的工作成果 软件测试经典理论说,测试无法证明软件是没有问题,而只能证明软…...

自己动手写编译器:创建由 C 语言编译而成的语法解析器

在上一章节&#xff0c;我们完成了由 c 语言设计的输入系统&#xff0c;本节我们看看如何在前一节的基础上完成一个由 c 语言设计并编译出来的词法解析器。整个解析器的基本设计思路是&#xff1a; 1&#xff0c;由我们上一节设计的输入系统将字符串从文件中读入。 2&#xff0…...

接口设计-增删改查

关于增删改查的 接口设计&#xff0c;比较简单&#xff0c;有一些固定的做法可以使用。 查询列表 查询列表的接口&#xff0c;带上分页的入参&#xff1a; pageNo&#xff0c;pageSize&#xff0c;非必选&#xff0c;并设置默认值。 入参为 dto&#xff0c;根据 dto 从数据库…...