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

09前后端分离+SSM整合的小案例

  • 前端的Node = 后端的Tomcat,是前端程序的容器。
  • 前端的npm = 后端的maven

1. 导入前端项目

node版本:16.16.0

  1. 配置阿里镜像

    npm config set registry https://registry.npmjs.org/

  2. 更新npm版本

    npm install -g npm@9.6.6

  3. 用vscode打开解压后的项目 , 右上角toggle panel打开命令行

  4. npm依赖下载命令

    npm install
    即可下载所有需要的依赖

  5. npm run dev //运行测试.

2. 后端项目

  1. 数据库脚本:
CREATE TABLE schedule (id INT NOT NULL AUTO_INCREMENT,title VARCHAR(255) NOT NULL,completed BOOLEAN NOT NULL,PRIMARY KEY (id)
);INSERT INTO schedule (title, completed)
VALUES('学习java', true),('学习Python', false),('学习C++', true),('学习JavaScript', false),('学习HTML5', true),('学习CSS3', false),('学习Vue.js', true),('学习React', false),('学习Angular', true),('学习Node.js', false),('学习Express', true),('学习Koa', false),('学习MongoDB', true),('学习MySQL', false),('学习Redis', true),('学习Git', false),('学习Docker', true),('学习Kubernetes', false),('学习AWS', true),('学习Azure', false);
  1. 新建一个module,转web项目. 先写配置类
    因为涉及了数据库, 还要写连接池的配置类 , 但是将数据库连接池和mapper的配置写到一起 . 总计还是4个配置类 , Controller放到Web容器 , Service/mapper+连接池/数据源 放到root容器.
    此外,还要一个初始化IoC容器的初始化类
    把上节的四个复制粘贴即可

controller

package com.sunsplanter.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;/*** projectName: com.atguigu.config** 1.实现Springmvc组件声明标准化接口WebMvcConfigurer 提供了各种组件对应的方法* 2.添加配置类注解@Configuration* 3.添加mvc复合功能开关@EnableWebMvc* 4.添加controller层扫描注解* 5.开启默认处理器,支持静态资源处理*/
@Configuration
@EnableWebMvc
@ComponentScan("com.sunsplanter.controller")
public class WebMvcJavaConfig implements WebMvcConfigurer {//开启静态资源@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}//jsp视图解析器前后缀@Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {registry.jsp("WEB-INF/views","jsp");}//拦截器,指明包含的路径排除的路径@Overridepublic void addInterceptors(InterceptorRegistry registry) {//registry.addInterceptor((new 拦截器的类).addPathPatterns().excludePathPatterns)}
}

service

    package com.sunsplanter.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.sql.DataSource;/*** 1. 声明@Configuration注解,代表配置类* 2. 声明@EnableTransactionManagement注解,开启事务注解支持* 3. 声明@EnableAspectJAutoProxy注解,开启aspect aop注解支持.@Before/@After/@Around* 4. 声明@ComponentScan组件扫描* 5. 声明式事务管理. 1.实现对应的事务管理器(DataSourceTransactionManager) 2.开启事务注解支持*/@EnableTransactionManagement@EnableAspectJAutoProxy@Configuration@ComponentScan("com.sunsplanter.service")public class ServiceJavaConfig {//@Bean//IoC容器自动将property中的dataSource注入此中public DataSourceTransactionManager transactionManager(DataSource dataSource){DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;}}

mapper

package com.sunsplanter.config;import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import java.util.Properties;/*** description: 持久层配置和Druid和Mybatis配置 使用一个配置文件*/
@Configuration
public class MapperJavaConfig {/*** 配置SqlSessionFactoryBean,指定连接池对象和外部配置文件即可* @param dataSource 需要注入连接池对象* @return 工厂Bean*/@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){//实例化SqlSessionFactory工厂SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//设置连接池sqlSessionFactoryBean.setDataSource(dataSource);//settings [包裹到一个configuration对象,切记别倒错包]org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setMapUnderscoreToCamelCase(true);configuration.setLogImpl(Slf4jImpl.class);configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);sqlSessionFactoryBean.setConfiguration(configuration);//typeAliasessqlSessionFactoryBean.setTypeAliasesPackage("com.atguigu.pojo");//分页插件配置PageInterceptor pageInterceptor = new PageInterceptor();Properties properties = new Properties();properties.setProperty("helperDialect","mysql");pageInterceptor.setProperties(properties);sqlSessionFactoryBean.addPlugins(pageInterceptor);return sqlSessionFactoryBean;}/*** 配置Mapper实例扫描工厂,配置 <mapper <package 对应接口和mapperxml文件所在的包* @return*/@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();//设置mapper接口和xml文件所在的共同包mapperScannerConfigurer.setBasePackage("com.atguigu.mapper");return mapperScannerConfigurer;}}

数据源配置类 , 从properties中取

package com.sunsplanter.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceJavaConfig {@Value("${jdbc.user}")private String user;@Value("${jdbc.password}")private String password;@Value("${jdbc.url}")private String url;@Value("${jdbc.driver}")private String driver;//数据库连接池配置@Beanpublic DataSource dataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setUsername(user);dataSource.setPassword(password);dataSource.setUrl(url);dataSource.setDriverClassName(driver);return dataSource;}}

初始化类:

package com.sunsplanter.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class SpringIoCInit extends AbstractAnnotationConfigDispatcherServletInitializer {//指定root容器对应的配置类 , 即下面两层@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class<?>[] {MapperJavaConfig.class, ServiceJavaConfig.class, DataSourceJavaConfig.class };}//指定web容器对应的配置类@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class<?>[] { WebMvcJavaConfig.class };}//指定dispatcherServlet处理路径,通常为 /@Overrideprotected String[] getServletMappings() {return new String[] { "/" };}}
  1. 实体类
/*** description: 任务实体类*/
@Data
public class Schedule {private Integer id;private String title;private Boolean completed;
}
  1. 准备 R(返回结果类)
package com.sunsplanter.utilspublic class R {private int code = 200; //200成功状态码private boolean flag = true; //返回状态private Object data;  //返回具体数据public  static R ok(Object data){R r = new R();r.data = data;return r;}public static R  fail(Object data){R r = new R();r.code = 500; //错误码r.flag = false; //错误状态r.data = data;return r;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public boolean isFlag() {return flag;}public void setFlag(boolean flag) {this.flag = flag;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}
}
  1. 准备 PageBean
package com.sunsplanter.utils@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean<T> {private int currentPage;   // 当前页码private int pageSize;      // 每页显示的数据量private long total;    // 总数据条数private List<T> data;      // 当前页的数据集合
}

开始实现功能:

3.1 . 查询全部功能实现

/* 
需求说明查询全部数据页数据
请求urischedule/{pageSize}/{currentPage}
请求方式 get   
响应的json{"code":200,"flag":true,"data":{//本页数据data:[{id:1,title:'学习java',completed:true},{id:2,title:'学习html',completed:true},{id:3,title:'学习css',completed:true},{id:4,title:'学习js',completed:true},{id:5,title:'学习vue',completed:true}], //分页参数pageSize:5, // 每页数据条数 页大小total:0 ,   // 总记录数currentPage:1 // 当前页码}}
*/
  1. controller层 .
/*@CrossOrigin 注释在带注释的控制器方法上启用跨源请求*/
@CrossOrigin
//get请求方式
@RequestMapping("schedule")
@RestController
public class ScheduleController
{//控制层只做两件事:接收参数,返回结果 , 因此先声明一个业务层对象@Autowiredprivate ScheduleService scheduleService;//路径传参,用大括号括起来, 从形参中传@GetMapping("/{pageSize}/{currentPage}")public R showList(@PathVariable(name = "pageSize") int pageSize, @PathVariable(name = "currentPage") int currentPage){PageBean<Schedule> pageBean = scheduleService.findByPage(pageSize,currentPage);return  R.ok(pageBean);}
}    
  1. service
@Slf4j
@Service
public class ScheduleServiceImpl  implements ScheduleService {@Autowiredprivate ScheduleMapper scheduleMapper;/*** 分页数据查询,返回分页pageBean** @param pageSize* @param currentPage* @return*/@Override
//调用mapper对象执行查询语句,结果封装进实体对象的列表中-->public PageBean<Schedule> findByPage(int pageSize, int currentPage) {//1.设置分页参数PageHelper.startPage(currentPage,pageSize);//2.数据库查询List<Schedule> list = scheduleMapper.queryPage();//3.结果获取PageInfo<Schedule> pageInfo = new PageInfo<>(list);//4.pageBean封装PageBean<Schedule> pageBean = new PageBean<>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());log.info("分页查询结果:{}",pageBean);return pageBean;}}
  1. mapper

    mapper接口

public interface ScheduleMapper {List<Schedule> queryPage();
}    
mapperxml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace等于mapper接口类的全限定名,这样实现对应 -->
<mapper namespace="com.atguigu.mapper.ScheduleMapper"><select id="queryPage" resultType="schedule">select * from schedule</select>
</mapper>    

相关文章:

09前后端分离+SSM整合的小案例

前端的Node 后端的Tomcat&#xff0c;是前端程序的容器。前端的npm 后端的maven 1. 导入前端项目 node版本&#xff1a;16.16.0 配置阿里镜像 npm config set registry https://registry.npmjs.org/ 更新npm版本 npm install -g npm9.6.6 用vscode打开解压后的项目 , 右上角…...

模仿ProTable创建ProTable组件

不多说废话直接上代码 父组件 // index.jsx/*** description 此ProTable是根据ProComponents里的ProTable模仿封装的简易版本* */ import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from react import { Card, Table } from antd import…...

新品发布 | 多通道总线记录仪TLog1004,是你期待的吗?

新品发布 2024年1月12日&#xff0c;同星智能又发布一款多通道 CAN &#xff08;FD&#xff09;总线、LIN 总线接口logger设备&#xff0c;此款产品在TLog1002基础上进行了升级&#xff0c;同时内置 3 路数字输入和 2 路数字输出&#xff0c;便于多种信号测量和系统集成。可以满…...

Double数据类型保留3位小数

Double scrapGrn scrapQty * Double.parseDouble(lot.getCnvrsnFctr()) / 1000 ; // 保留3位小数 DecimalFormat decimalFormat new DecimalFormat("#.###"); String scrapGrnStr decimalFormat.format(scrapGrn); 345.12344 处理后 为 345.123 34…...

08- OpenCV:形态学操作(膨胀与腐蚀 、提取水平与垂直线)

目录 前言 一、膨胀&#xff08;Dilation&#xff09;与 腐蚀&#xff08;Erosion&#xff09; 二、形态学操作 1、开操作&#xff08;Opening&#xff09; 2、闭操作&#xff08;Closing&#xff09; 3、形态学梯度&#xff08;Morphological Gradient&#xff09; 4、…...

基于JavaWeb+SSM+Vue停车场微信小程序系统的设计和实现

基于JavaWebSSMVue停车场微信小程序系统的设计和实现 滑到文末获取源码Lun文目录前言主要技术系统设计功能截图订阅经典源码专栏Java项目精品实战案例《500套》 源码获取 滑到文末获取源码 Lun文目录 目录 1系统概述 1 1.1 研究背景 1 1.2研究目的 1 1.3系统设计思想 1 2相关…...

VUE---自定义指令

自定义指令&#xff1a;自己定义的指令&#xff0c;可以封装一些dom操作&#xff0c;扩展额外功能。可分为全局注册与 局部注册。 全局注册&#xff08;main.js中注册&#xff09;&#xff1a; Vue.directive(指令名称,{ bind(ele,binding) {}, // 只执…...

开发安全之:Cross-Site Scripting (XSS) 漏洞

近期&#xff0c;我会结合研发云陆续发布开发安全相关的文章&#xff0c;欢迎大家关注&#xff01; Overview echo json_encode($arr)&#xff1a;向一个 Web 浏览器发送了未验证的数据&#xff0c;从而导致该浏览器执行恶意代码。 Details Cross-Site Scripting (XSS) 漏洞…...

代码随想录算法训练营第二十四天| 77. 组合

77.组合 public List<List<Integer>> combine(int n, int k) {if (n < k) {return null;}List<List<Integer>> list new ArrayList<>();List<Integer> path new ArrayList<>();backSource(n, path, list, k);return list;}pu…...

虚拟歌姬学习:DiffSinger,让GitHub下载快的方法!

《三分钟上手DiffSinger》系列 ——基础篇https://www.bilibili.com/video/BV1ug4y1S7Dk/?spm_id_from333.337.search-card.all.click&vd_source124076d7d88eee393a1d8bf6fc787efa 下载DiffSinger 建议用edge浏览器还有steam&#xff0c;有时只是慢&#xff0c;但是还是…...

What is `StringEscapeUtils.escapeHtml4` does?

StringEscapeUtils.escapeHtml4 作用是将特殊字符转换为它们对应的HTML实体形式&#xff0c;从而防止这些字符在网页中被解析为HTML标签或脚本&#xff0c;有助于防止跨站脚本攻击&#xff08;XSS, Cross-Site Scripting&#xff09; 依赖 <!--org.apache.commons.text.St…...

Dubbo 的心脏:理解和应用多种协议【十三】

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Dubbo 的心脏&#xff1a;理解和应用多种协议【十三】 前言<dubbo:protocol> 基础<dubbo:protocol> 的定义和作用微服务中协议的重要性支持的协议类型配置示例 配置基本配置参数高级配置选…...

操作系统实验报告

目录 目录 实验一 一、实验结果 实验二 使用信号量实现进程互斥与同步 一、实验结果 1. 使用信号量实现有限缓冲区的生产者和消费者问题 2. 使用信号量实现读进程具有优先权的读者和写者问题 实验三 死锁和预防 一、实验要求 二、实验内容 三、实验结果 实验四 内…...

IPv6--ACL6(IPv6访问控制列表--基本ACL6配置)

ACL基本原理 ACL由一系列规则组成,通过将报文与ACL规则进行匹配,设备可以过滤出特定的报文。 ACL的组成 ACL编号: 在网络设备上配置ACL时,每个ACL都需要分配一个编号,称为ACL编号,用来标识ACL。不同分类的ACL编号范围不同,这个后面具体讲。 规则: 前面提到了,一个AC…...

C和指针课后答案

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pandas是什么&#xff1f;二、使用步骤 1.引入库2.读入数据总结 前言 第八章课后答案 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参…...

C语言——大头记单词

归纳编程学习的感悟&#xff0c; 记录奋斗路上的点滴&#xff0c; 希望能帮到一样刻苦的你&#xff01; 如有不足欢迎指正&#xff01; 共同学习交流&#xff01; &#x1f30e;欢迎各位→点赞 &#x1f44d; 收藏⭐ 留言​&#x1f4dd; 每一发奋努力的背后&#xff0c;必有加…...

根据自己修改后的容器制作镜像并上传docker hub

要将自己的镜像上传到Docker Hub&#xff0c;可以按照以下步骤进行操作&#xff1a; 首先&#xff0c;确保已经在本地构建好了需要上传的 Docker 镜像。如果还没有构建&#xff0c;可以使用 docker build 命令来创建镜像。 登录到 Docker Hub 账号。打开终端或命令提示符&…...

Maven 基础安装配置及使用

大家好我是苏麟 , 今天聊聊Maven . Maven Maven , 是Apache公司下基于Java开发的开源项目 . 我们构建一个项目需要用到很多第三方的类库&#xff0c;需要引入大量的jar包。一个项目Jar包的数量之多往往让我们瞠目结舌&#xff0c;并且Jar包之间的关系错综复杂&#xff0c;一…...

redis 持久化机制

client redis[内存] -----> 内存数据- 数据持久化-->磁盘 Redis官方提供了两种不同的持久化方法来将数据存储到硬盘里面分别是: RDB 快照(Snapshot) AOF (Append Only File) 只追加日志文件 1 快照(Snapshot) 1. 特点 这种方式可以将某一时刻的所有数据都写入硬盘中…...

MySQL(视图,存储函数,存储过程)

作业1&#xff1a; 作业实现&#xff1a; 首先创建学生表&#xff0c;课程表&#xff0c;以及学生选课表。 CREATE TABLE Student (Sno INT PRIMARY KEY,Sname VARCHAR(20) NOT NULL,Ssex CHAR(1) CHECK (Ssex IN (男, 女)),Sage INT,SDept VARCHAR(20) DEFAULT 计算机 );CRE…...

rockchip 平台 linux FIT 打包格式介绍

1 基础介绍 FIT&#xff08;flattened image tree&#xff09;是U-Boot⽀持的⼀种新固件类型的引导⽅案&#xff0c;⽀持任意多个image打包和校 验。FIT 使⽤ its (image source file) ⽂件描述image信息&#xff0c;最后通过mkimage⼯具⽣成 itb (flattened image tree blob) …...

虚拟机安装宝塔的坑

问题&#xff1a; 在虚拟机中centos7和centos8中安装宝塔之后&#xff0c;无法访问面板。 解决&#xff1a; 1.先关闭防火墙&#xff08;如果本机能够ping通相关端口&#xff0c;则不用关闭防火墙&#xff09; 2.最新的宝塔会自动开启ssl协议&#xff0c;需要手动关闭。…...

Ubuntu使用QtCreator + CMake 开发C/C++程序

平台 OS: Ubuntu 20.04 cmake: 3.16.3 IDE: Qt Creator 4.11.1 Based on Qt 5.14.1 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit) Built on Feb 5 2020 12:48:30 From revision b2ddeacfb5 Copyright 2008-2019 The Qt Company Ltd. All rights reserved. The program …...

【分布式监控】zabbix与grafana连接

1.在zabbix- server服务端安装grafana&#xff0c;并启动服务 先在官网下载软件 https://grafana.com/grafana/download/9.4.7?editionenterprise&pggraf&plcmtdeploy-box-1#可以翻译成中文介绍&#xff0c;很详细的教程 yum install -y https://dl.grafana.com/ent…...

02-编程猜谜游戏

本章通过演示如何在实际程序中使用 Rust&#xff0c;你将了解 let 、 match 、方法、关联函数、外部crate等基础知识。 本章将实现一个经典的初学者编程问题&#xff1a;猜谜游戏。 工作原理如下&#xff1a;程序将随机生成一个介于 1 和 100 之间的整数。然后&#xff0c;程序…...

Web3解密:区块链技术如何颠覆传统互联网

随着区块链技术的崛起&#xff0c;Web3正逐渐成为新一代互联网的代名词。它不再依赖中心化的权威机构&#xff0c;而是通过去中心化、透明、安全的特性&#xff0c;为用户带来更为开放和公正的互联网体验。本文将深入解密Web3&#xff0c;揭示区块链技术如何颠覆传统互联网的基…...

java小项目:简单的收入明细记事本,超级简单(不涉及数据库,通过字符串来记录)

一、效果 二、代码 2.1 Acount类 package com.demo1;public class Acount {public static void main(String[] args) {String details "收支\t账户金额\t收支金额\t说 明\n"; //通过字符串来记录收入明细int balance 10000;boolean loopFlag true;//控制循…...

域环境权限提升

Windows系统配置错误 在Windows系统中&#xff0c;攻击者通常会通过系统内核溢出漏来提权&#xff0c;但是如果碰到无法通过系统内核溢出漏洞法国提取所在服务器权限的情况&#xff0c;就会系统中的配置错误来提权。Windows系统中常见哦欸之错误包括管理员凭证配置错误&#x…...

【Debian】非图形界面Debian10.0.0安装xfce和lxde桌面

一、安装 1. Debian10.0.0安装xfce桌面 sudo apt update sudo apt install xfce4 startxfce4 2. Debian10.0.0安装lxde桌面 sudo apt-get install lxde安装后重启电脑。 二、说明 XFCE、LXDE 和 GNOME 是三个流行的桌面环境&#xff0c;它们都是为类 Unix 操作系统设计…...

极狐GitLab 线下『 DevOps专家训练营』成都站开班在即

成都机器人创新中心联合极狐(GitLab)隆重推出极狐GitLab DevOps系列认证培训课程。该课程主要面向使用极狐GitLab的DevOps工程师、安全审计人员、系统运维工程师、系统管理员、项目经理或项目管理人员&#xff0c;完成该课程后&#xff0c;学员将达到DevOps的专家级水平&#x…...