首页/新闻资讯/正文详情

springboot创建web项目

发布时间:2026/9/24 14:00:02 来源:云帆数科 栏目:资讯中心
springboot创建web项目
一、创建项目二、导入依赖pom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.2.12.RELEASE/version /parent groupIdcom.cy/groupId artifactIdSpringboot-day01-re/artifactId version1.0-SNAPSHOT/version properties maven.compiler.source8/maven.compiler.source maven.compiler.target8/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties dependencies !--②、引入依赖(起步依赖)-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId /dependency !--mybatis提供依赖-- dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version2.2.2/version /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.1.49/version /dependency !--连接池-- dependency groupIdcom.alibaba/groupId artifactIddruid-spring-boot-starter/artifactId version1.2.16/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId /dependency /dependencies /project三、配置文件application.yml文件server: port: 8080 servlet: context-path: /suv #Spring配置 spring: #资源配置 datasource: #数据源类型 type: com.alibaba.druid.pool.DruidDataSource #地址 url: jdbc:mysql://localhost:3306/rbac #用户名 username: root #密码 password: chenying #驱动名称 driver-class-name: com.mysql.jdbc.Driver #持久层框架配置 mybatis: #别名配置 type-aliases-package: com.cy.pojo.entity #映射文件 mapper-locations: classpath*:mappers/**/*mapper.xml # 配置日志 logging: level: root: info com.cy: debug四、XxxApplication内容SpringBootApplication public class XxxApplication { public static void main(String[] args) { SpringApplication.run(XxxApplication.class,args); } }五、使用注解或config配置文件解决注解问题package com.cy.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; Configuration public class CorsConfig { Bean public CorsFilter corsFilter() { CorsConfiguration config new CorsConfiguration(); config.addAllowedOrigin(*); config.addAllowedMethod(*); config.addAllowedHeader(*); UrlBasedCorsConfigurationSource source new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration(/**, config); return new CorsFilter(source); } }六、完成后端的增删查改操作controller(控制器层)package com.cy.controller; import com.cy.pojo.dto.EmployeeDto; import com.cy.pojo.entity.Employee; import com.cy.pojo.vo.Result; import com.cy.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; RestController RequestMapping(list) //解决跨域问题 public class EmployeeController { Autowired private EmployeeService employeeService; GetMapping(/{pageNo}/{pageSize}) public Result queryPage(PathVariable(pageNo) Integer pageNo, PathVariable(pageSize) Integer pagesize, RequestParam(required false) String username ){ EmployeeDto employees employeeService.PageByData(pageNo,pagesize,username); Result result Result.builder() .code(20000) .message(查询成功) .data(employees) .build(); return result; } PostMapping(/save) public Result save(RequestBody Employee employee){ Integer add employeeService.addByData(employee); if (addnull){ return Result.builder().code(50000).message(新增失败).build(); } return Result.builder().code(20000).message(新增成功).build(); } RequestMapping(/listById) public Result getById(RequestParam(id) Integer id){ Employee employeeemployeeService.selectById(id); if (employeenull){ return Result.builder().code(50000).message(查询失败).build(); } return Result.builder() .code(20000) .message(查询成功) .data(employee) .build(); } PutMapping(/update) public Result update(RequestBody Employee employee){ Integer updateemployeeService.updateByData(employee); if (updatenull){ return Result.builder().code(50000).message(修改失败).build(); } return Result.builder().code(20000).message(修改成功).build(); } DeleteMapping(/delete) public Result del(RequestParam Integer id){ Integer delemployeeService.delById(id); if (delnull){ return Result.builder().code(50000).message(删除失败).build(); } return Result.builder().code(20000).message(删除成功).build(); } RequestMapping(/batchDel) public Result batchDel(RequestBody ListInteger ids){ Integer dels employeeService.batchDel(ids); if (delsnull){ return Result.builder().code(50000).message(批量删除失败).build(); } return Result.builder().code(20000).message(批量删除成功).build(); } }service业务逻辑层package com.cy.service; import com.cy.pojo.dto.EmployeeDto; import com.cy.pojo.entity.Employee; import java.util.List; public interface EmployeeService { EmployeeDto PageByData(Integer pageNo, Integer pagesize, String username); Integer addByData(Employee employee); Integer updateByData(Employee employee); Employee selectById(Integer id); Integer delById(Integer id); Integer batchDel(ListInteger ids); }package com.cy.service.impl; import com.cy.mapper.EmployeeMapper; import com.cy.pojo.dto.EmployeeDto; import com.cy.pojo.entity.Employee; import com.cy.service.EmployeeService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; Service public class EmployeeServiceImpl implements EmployeeService { Resource private EmployeeMapper employeeMapper; Override public EmployeeDto PageByData(Integer pageNo, Integer pagesize,String username) { Integer totalemployeeMapper.sum(); int ceil (int) Math.ceil((total / (double) pagesize)); Integer cceil; Integer start; if (pageNoc){ start(pageNo-1)*pagesize; }else { start(ceil-1)*pagesize; } ListEmployee employeeListemployeeMapper.getPage(start,pagesize,username); EmployeeDto dto EmployeeDto.builder() .pageNo(pageNo) .pageSize(pagesize) .total(ceil) .dataList(employeeList) .build(); return dto; } Override public Integer addByData(Employee employee) { Integer addemployeeMapper.add(employee); return add; } Override public Integer updateByData(Employee employee) { Integer updateemployeeMapper.updateByData(employee); return update; } Override public Employee selectById(Integer id) { Employee employeeemployeeMapper.getById(id); return employee; } Override public Integer delById(Integer id) { Integer delemployeeMapper.del(id); return del; } Override public Integer batchDel(ListInteger ids) { return employeeMapper.batchDel(ids); } }mapper数据访问层package com.cy.mapper; import com.cy.pojo.entity.Employee; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; Mapper public interface EmployeeMapper { ListEmployee getPage(Param(start) Integer start, Param(pageSize) Integer pagesize, Param(username) String username); Integer sum(); Integer add(Employee employee); Integer updateByData(Employee employee); Employee getById(Integer id); Integer del(Integer id); Integer batchDel(ListInteger ids); }mappers?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.cy.mapper.EmployeeMapper insert idadd insert into employee(username,name,password,email,age,admin,dept_id) values(#{username},#{name},#{password},#{email},#{age},#{admin},#{deptId}) /insert update idupdateByData update employee set if testusername!null username#{username}, /if if testname!null name#{name}, /if if testpassword!null password#{password}, /if if testemail!null email#{email}, /if if testage!null age#{age}, /if if testadmin!null admin#{admin}, /if if testdeptId!null dept_id#{deptId}, /if /set where id#{id} /update delete iddel delete from employee where id#{id} /delete delete idbatchDel delete from employee where id trim prefixin ( suffix) suffixOverrides, foreach collectionids itemitem separator, #{item} /foreach /trim /delete !--mapper namespace-- select idgetPage resultTypecom.cy.pojo.entity.Employee select * from employee where if testusername!null and username like concat(%,#{username},%) /if /where if teststart!null and pageSize!null limit #{start},#{pageSize} /if /select select idsum resultTypejava.lang.Integer select count(*) from employee /select select idgetById resultTypecom.cy.pojo.entity.Employee select * from employee where id#{id} /select /mapper还有一些简单的工具类EmployeeDtopackage com.cy.pojo.dto; import com.cy.pojo.entity.Employee; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.List; /** * Author chen * Version 1.0 * Date 2024/12/24 * Description 返回前端的数据 */ Data Builder AllArgsConstructor NoArgsConstructor public class EmployeeDto { /** *数据总数 */ private Integer total; /** *页面数 */ private Integer pageNo; /** *每页数据大小 */ private Integer pageSize; /** *数据集合 */ private ListEmployee dataListnew ArrayList(); }实体类package com.cy.pojo.entity; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.List; Data Builder NoArgsConstructor AllArgsConstructor public class Employee { /** *主键 */ private Integer id; /** *用户名 */ private String username; /** *名称 */ private String name; /** *密码 */ private String password; /** *邮箱 */ private String email; /** *年龄 */ private Integer age; /** *管理员 */ private Integer admin; /** *所属部门 */ private Integer deptId; //角色id集 //private ListInteger roleIds new ArrayList(); }统一后端返回前端的格式package com.cy.pojo.vo; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** * Author chen * Version 1.0 * Date 2024/12/24 * Description 统一返回类型 */ Data Builder NoArgsConstructor AllArgsConstructor public class Result { /** *状态码 */ private Integer code; /** *提示信息 */ private String message; /** *返回数据 */ private Object data; }前端HTML页面效果显示!DOCTYPE html html langen head meta charsetUTF-8 titleTitle/title !-- Bootstrap -- link relstylesheet href./css/bootstrap.css script src./js/vue.js/script script src./js/axios.min.js/script /head body div idapp table classtable form classform-inline label 产品名/概述/label input typetext v-modelusername placeholder请输入产品名/概述 button typesubmit clicksearch搜索/button button classfat-btn typebutton stylebackground-color: #269abc clickresBtn重置 /button /form div button classfat-btn typebutton stylebackground-color: #269abc clickasveBtn 新增 /button button classfat-btn typebutton stylebackground-color: tomato clickbatchDel 批量删除 /button /div thead tr th选择/th th用户名/th th用户/th th密码/th th邮件/th th年龄/th th管理员/th th操作/th /tr /thead tbody tr v-for(value,key) in dataList :keykey td input typecheckbox :valuevalue.id v-modelids /td td{{value.username}}/td td{{value.name}}/td td{{value.password}}/td td{{value.email}}/td td{{value.age}}/td td{{value.admin}}/td td button typebutton stylebackground-color: tomato clickdelData(value.id) 删除 /button button classfat-btn typebutton stylebackground-color: darkcyan clickfindData(value.id) 编辑 /button /td /tr /tbody /table !--分页-- div stylepadding-right: 20px ul classpagination lia href#laquo;/a/li li v-forpage in totalPage :keypage a href# :class{active:pagepageNo} clickchange(page){{page}}/a/li lia href#raquo;/a/li /ul /div !--新增窗口-- div stylewidth: 50%;align-content: center v-ifnewBtn form div classform-group label 用户名/label input typetext classform-control v-modeladdem.username placeholder用户名 /div div classform-group label 用户/label input typetext classform-control v-modeladdem.name placeholder用户 /div div classform-group label 密码/label input typetext classform-control v-modeladdem.password placeholder密码 /div div classform-group label 邮件/label input typetext classform-control placeholder邮件 v-modeladdem.email /div div classform-group label 年龄/label input typetext classform-control placeholder年龄 v-modeladdem.age /div div classform-group label 管理员/label input typetext classform-control placeholder管理员 v-modeladdem.admin /div div classform-group label 所属部门/label input typetext classform-control placeholder所属部门 v-modeladdem.deptId /div button typesubmit classbtn btn-default clicksaveData确定/button button typesubmit classbtn btn-default clickcloseBtn取消/button /form /div !--编辑窗口-- div stylewidth: 50%;align-content: center v-ifupdateBtn form div classform-group label 用户名/label input typetext classform-control v-modelempol.username placeholder用户名 /div div classform-group label 用户/label input typetext classform-control v-modelempol.name placeholder用户 /div div classform-group label 密码/label input typetext classform-control v-modelempol.password placeholder密码 /div div classform-group label 邮件/label input typetext classform-control placeholder邮件 v-modelempol.email /div div classform-group label 年龄/label input typetext classform-control placeholder年龄 v-modelempol.age /div div classform-group label 管理员/label input typetext classform-control placeholder管理员 v-modelempol.admin /div div classform-group label 所属部门/label input typetext classform-control placeholder所属部门 v-modelempol.deptId /div button typesubmit classbtn btn-default clickupdateData确定/button button typesubmit classbtn btn-default clickcloseBtn取消/button /form /div /div script src./js/bootstrap.js/script script new Vue({ el:#app, data:{ pageNo:1, pageSize:3, totalPage:1, dataList:[], ids:[], username:null, form:{ username:null, name:null }, addem:{ }, empol:{}, newBtn:false, updateBtn:false }, methods:{ getAll(){ console.log(username,this.username) axios({ method:get, url:http://localhost:8080/suv/list/${this.pageNo}/${this.pageSize}, params:{ username:this.username } }).then(resp{ console.log(分页查询的数据,resp.data) if (resp.data.code20000){ this.dataListresp.data.data.dataList this.totalPageresp.data.data.total } }) }, change(page){ this.pageNopage this.getAll() } , /*新增*/ saveData(){ axios({ method: post, url: http://localhost:8080/suv/list/save, data: this.addem }).then(resp{ if (resp.data.code20000){ alert(resp.data.message) }else { alert(resp.data.message) } }) }, findData(id){ this.updateBtn true axios({ method:get, url:http://localhost:8080/suv/list/listById, params: { id:id } }).then(resp{ console.log(resp.data) if (resp.data.code20000){ this.empolresp.data.data } }) } , updateData() { axios({ method:put, url:http://localhost:8080/suv/list/update, data:this.empol }).then(resp{ if (resp.data.code20000){ alert(resp.data.message) this.updateBtnfalse this.getAll() } }) } , asveBtn(){ this.newBtntrue }, search(){ this.pageNo 1, this.getAll() } , resBtn(){ this.closeBtn() this.usernamenull this.getAll() }, closeBtn(){ this.newBtn false, this.updateBtn false }, /*删除*/ delData(id){ if (confirm(确定删除吗)){ axios({ method:delete, url:http://localhost:8080/suv/list/delete, params:{ id:id } }).then(resp{ if (resp.data.code20000){ alert(resp.data.message) }else { alert(resp.data.message) } this.pageNo1 this.getAll() }) } }, /*批量删除*/ batchDel(){ axios({ method:post, url:http://localhost:8080/suv/list/batchDel, data:this.ids }).then(resp{ alert(resp.data.message) this.pageNo1 this.getAll() }) } }, created(){ this.getAll() } }) /script /body /html数据库数据设置效果展示

相关推荐

node-restify 实战:用 TODO 示例应用掌握 restify 服务端、客户端与测试的完整工程结构
node-restify 实战:用 TODO 示例应用掌握 restify 服务端、客户端与测试的完整工程结构

后端 【免费下载链接】node-restify The future of Node.js REST development 项目地址: https://gitcode.com/gh_mirrors/no/node-restify 点击查看 免费下载 导读 本文以 examples/todoapp 示例应用为主体,系统讲解如何使用 restify 搭建一个结构清晰… · 2026/9/24 14:00:02

Windows 上 3 步装好安卓:WSABuilds 带 Play 商店和 Root 的完整安装指南
Windows 上 3 步装好安卓:WSABuilds 带 Play 商店和 Root 的完整安装指南

Windows 上 3 步装好安卓:WSABuilds 带 Play 商店和 Root 的完整安装指南 【免费下载链接】WSABuilds Run Windows Subsystem For Android on your Windows 10 and Windows 11 PC using prebuilt binaries with Google Play Store (MindTheGapps) and/or Magisk or … · 2026/9/24 14:00:02

【Dv2Admin】基于腾讯云Cos文件、图片上传
【Dv2Admin】基于腾讯云Cos文件、图片上传

本篇文章记录如何在基于 Dv2Admin 框架 的项目中接入腾讯云 COS 文件上传功能。该功能基于 dvadmin_cloud_storage 插件开发,整体操作与 Dv3Admin 一致,但在 Dv2 中需要做一些调整和兼容性修改 文章目录 后端插件安装 初始化数据兼容修改 腾讯云 COS 配置 代码修改说明 总结 … · 2026/9/24 14:00:02

Skia SkSL 与 Runtime Effects 完整实战指南:从着色语言语法到色彩管理、预乘 Alpha 与代码最小化
Skia SkSL 与 Runtime Effects 完整实战指南:从着色语言语法到色彩管理、预乘 Alpha 与代码最小化

图形学 【免费下载链接】skia Skia is a complete 2D graphic library for drawing Text, Geometries, and Images. See documentation for contribution instructions. 项目地址: https://gitcode.com/gh_mirrors/ski/skia 点击查看 免费下载 SkSL(Ski… · 2026/9/24 14:28:51

WinUtil:装软件做优化修系统,一个窗口搞定
WinUtil:装软件做优化修系统,一个窗口搞定

WinUtil:装软件做优化修系统,一个窗口搞定 【免费下载链接】winutil Chris Titus Techs Windows Utility - Install Programs, Tweaks, Fixes, and Updates 项目地址: https://gitcode.com/GitHub_Trending/wi/winutil 新机装系统那天&#xff0c… · 2026/9/24 14:28:51

Vercel AI SDK 5 技术解析与 VoltAgent 深度集成指南:从 LLM 调用到可观测智能体编排
Vercel AI SDK 5 技术解析与 VoltAgent 深度集成指南:从 LLM 调用到可观测智能体编排

Vercel AI SDK 5 技术解析与 VoltAgent 深度集成指南:从 LLM 调用到可观测智能体编排 【免费下载链接】voltagent AI Agent Engineering Platform built on an Open Source TypeScript AI Agent Framework 项目地址: https://gitcode.com/gh_mirrors/vo/voltagent… · 2026/9/24 14:28:50

Altium Designer、Cadence、PADS三大PCB设计软件选型对比与实战指南
Altium Designer、Cadence、PADS三大PCB设计软件选型对比与实战指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/9/24 14:28:44

亚马逊工具飞鱼数据参谋折扣码是什么?飞鱼数据参谋是什么?
亚马逊工具飞鱼数据参谋折扣码是什么?飞鱼数据参谋是什么?

亚马逊工具飞鱼数据参谋折扣码是什么?飞鱼数据参谋是什么?在亚马逊百万级卖家的激烈竞争中,选品如同在迷雾中探路 —— 既需要精准捕捉市场需求,又要避开红海陷阱。传统选品工具往往依赖估算数据或滞后信息,而飞鱼数据… · 2026/9/24 14:28:44

C型、LC型、CLC型电源滤波电路对比与12V/1A纹波抑制设计实例
C型、LC型、CLC型电源滤波电路对比与12V/1A纹波抑制设计实例

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/9/24 14:28:37

基于YOLOv8的渔船作业监控系统:从环境搭建到边缘部署全流程
基于YOLOv8的渔船作业监控系统:从环境搭建到边缘部署全流程

简介:这是一套面向计算机、人工智能、自动化等专业学生与教师的毕业设计级项目资源,围绕YOLOv8实现渔船作业监控系统,可用于毕设、课程设计、大作业或项目立项演示。压缩包共97个文件,约24.21MB,以70个Python源码文件为… · 2026/9/24 0:00:13

1D-CNN时间序列建模实战:从Conv1d原理到工业落地
1D-CNN时间序列建模实战:从Conv1d原理到工业落地

简介:面向时间序列数据建模的一维卷积神经网络完整实现,适合深度学习入门者及需要快速验证时序模型的研究者,能够从音频、文本、传感器或股价等序列中挖掘局部特征与时间依赖。压缩包体积很小,只有3KB,内含3个Python脚… · 2026/9/24 0:00:26

柔软的L:汉语语流中被忽视的舌肌张力控制
柔软的L:汉语语流中被忽视的舌肌张力控制

1. 这个“L”不是字母表里的L,而是舌尖上的L最近在几个方言群和语音教学社群里,反复看到有人发一句:“也说字母L:柔软的长舌”。初看以为是英语发音课笔记,点开才发现全是方言爱好者、播音系学生、语言康复师甚至戏曲演… · 2026/9/24 0:00:44

了解更多?预约专属演示

我们的顾问将为您一对一讲解产品与方案

企业微信二维码