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

SpringMVC第七阶段:SpringMVC的增删改查(01)

SpringMVC的增删改查

1、准备单表的数据库

drop database if exists springmvc;create database springmvc;use springmvc; ##创建图书表
create table t_book(`id` int(11) primary key auto_increment, 	## 主键`name` varchar(50) not null,				## 书名 `author` varchar(50) not null,				## 作者`price` decimal(11,2) not null,				## 价格`sales` int(11) not null,					## 销量`stock` int(11) not null					## 库存
);## 插入初始化测试数据
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '怎样拐跑别人的媳妇' , '龙伍' , 68, 99999 , 52);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '木虚肉盖饭' , '小胖' , 16, 1000 , 50);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'C++编程思想' , '刚哥' , 45.5 , 14 , 95);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '蛋炒饭' , '周星星' , 9.9, 12 , 53);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '赌神' , '龙伍' , 66.5, 125 , 535);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '西游记' , '罗贯中' , 12, 19 , 9999);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '水浒传' , '华仔' , 33.05 , 22 , 88);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '操作系统原理' , '刘优' , 133.05 , 122 , 188);insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '数据结构 java版' , '封大神' , 173.15 , 21 , 81);## 查看表内容
select id,name,author,price,sales,stock from t_book;

2、SpringMVC的增,删,改,查

在这里插入图片描述

需要导入的jar包有:

druid-1.1.9.jar
junit_4.12.jar
mysql-connector-java-5.1.37-bin.jar
org.hamcrest.core_1.3.0.jar
spring-aop-5.2.5.RELEASE.jar
spring-beans-5.2.5.RELEASE.jar
spring-context-5.2.5.RELEASE.jar
spring-core-5.2.5.RELEASE.jar
spring-expression-5.2.5.RELEASE.jar
spring-jcl-5.2.5.RELEASE.jar
spring-jdbc-5.2.5.RELEASE.jar
spring-orm-5.2.5.RELEASE.jar
spring-test-5.2.5.RELEASE.jar
spring-tx-5.2.5.RELEASE.jar
spring-web-5.2.5.RELEASE.jar
spring-webmvc-5.2.5.RELEASE.jar

在src目录下记得配置 jdbc.properties属性配置文件:

在这里插入图片描述

SpringMVC的配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com"></context:component-scan><!--配置视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/book/" /><property name="suffix" value=".jsp" /></bean><!-- 加载jdbc.properties属性配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置数据库连接池 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="username" value="${user}" /><property name="password" value="${password}" /><property name="url" value="${url}" /><property name="driverClassName" value="${driverClassName}" /><property name="initialSize" value="${initialSize}" /><property name="maxActive" value="${maxActive}" /></bean><!-- 配置用于执行sql语句的jdbcTemplate工具类 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" /></bean></beans>

web.xml中的配置内容如下:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- 配置前端控制器 --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

3、创建实体bean对象

public class Book {private Integer id;private String name;private String author;private BigDecimal price;private Integer sales;private Integer stock;
}

4、创建BookDao以及测试

@Repository
public class BookDao {@AutowiredJdbcTemplate jdbcTemplate;public int saveBook(Book book) {String sql = "insert into t_book( `name`,`author`,`price`,`sales`,`stock`) values(?,?,?,?,?)";return jdbcTemplate.update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock());}public int deleteBookById(Integer id) {String sql = "delete from t_book where id = ?";return jdbcTemplate.update(sql, id);}public int updateBook(Book book) {String sql = "update t_book set `name`=?,`author`=?,`price`=?,`sales`=?,`stock`=? where id = ?";return jdbcTemplate.update(sql, book.getName(), book.getAuthor(),book.getPrice(), book.getSales(), book.getStock(), book.getId());}public Book queryBookById(Integer id){String sql = "select `name`,`author`,`price`,`sales`,`stock`,`id` from t_book where id = ?";return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Book>(Book.class), id );}public List<Book> queryBooks(){String sql = "select `name`,`author`,`price`,`sales`,`stock`,`id` from t_book";return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));}}

5、创建BookService以及测试

@Service
public class BookService {@AutowiredBookDao bookDao;public void saveBook(Book book){bookDao.saveBook(book);}public void deleteBookById(Integer id){bookDao.deleteBookById(id);}public void updateBook(Book book){bookDao.updateBook(book);}public Book queryBookById(Integer id){return bookDao.queryBookById(id);}public List<Book> queryBooks(){return bookDao.queryBooks();}}

6、拷贝CRUD页面到WebContent目录下

从SpringMVC第二个的目录里,笔记目录中的CRUD目录
在这里插入图片描述

7、配置springMVC的视图解析器到springmvc.xml配置文件中

<!--配置视图解析器
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/book/" /><property name="suffix" value=".jsp" />
</bean>

相关文章:

SpringMVC第七阶段:SpringMVC的增删改查(01)

SpringMVC的增删改查 1、准备单表的数据库 drop database if exists springmvc;create database springmvc;use springmvc; ##创建图书表 create table t_book(id int(11) primary key auto_increment, ## 主键name varchar(50) not null, ## 书名 author varchar(50) no…...

接口测试-Mock测试方法

一、关于Mock测试 1、什么是Mock测试&#xff1f; Mock 测试就是在测试过程中&#xff0c;对于某些不容易构造&#xff08;如 HttpServletRequest 必须在Servlet 容器中才能构造出来&#xff09;或者不容易获取的比较复杂的对象&#xff08;如 JDBC 中的ResultSet 对象&#…...

关于宝塔部署jar包和war包

文章目录 前言一、jar包部署二、war包部署1.maven如果打包不了使用命令打包2.安装Tomcat进行访问是否成功2.进入Tomcat目录进行配置war包 一、项目访问方法 前言 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、jar包部署 1.其实jar包没什么讲的&…...

SpringMVC框架面试专题(初级-中级)-第十节

欢迎大家一起探讨&#xff5e;如果可以帮到大家请为我点赞关注哦&#xff5e; 截止到本节关于SpringMVC的内容已经更新完毕&#xff0c;后续会更新SpringBoot框架的面试题&#xff1b;大家在背题的时候切记不要死记硬背&#xff0c;需要理解 这是什么&#xff1f;有什么操作&a…...

PCIe TLB事务层详解过程

目录 1.What is TLP 2.PCIe 4种不同的事务 2.1.Memory事务 ​​​​​​​2.2.IO事务 2.3.Configuration事务 2.4.Message...

RK3588平台开发系列讲解(项目篇)YOLOv5部署测试

平台内核版本安卓版本RK3588Linux 5.10Android 12文章目录 一、YOLOv5环境安装二、YOLOv5简单使用2.1、获取预训练权重文2.2、YOLOv5简单测试2.3、转换为rknn模型2.4、部署到 RK 板卡三、airockchip/yolov5简单测试3.1、转换成rknn模型并部署到板卡沉淀、分享、成长,让自己和他…...

基于变形模板的弱监督体图像分割

文章目录 Weakly Supervised Volumetric Image Segmentation with Deformed Templates摘要本文方法实验结果 Weakly Supervised Volumetric Image Segmentation with Deformed Templates 摘要 背景 有许多方法可以对网络进行弱监督训练来分割2D图像。依赖于对3D图像的2D切片的…...

python实现单例模式及其应用

单例模式是一种常见的设计模式&#xff0c;它保证一个类只能被实例化一次&#xff0c;并提供了一个全局访问点来获取这个唯一的实例。 在 Python 中&#xff0c;可以通过使用装饰器、元类或模块等方式实现单例模式。下面分别介绍这三种方法&#xff1a; 1.使用装饰器实现单例…...

SSM 如何使用 Seata 框架实现分布式事务?

SSM 如何使用 Seata 框架实现分布式事务&#xff1f; 分布式事务是现代分布式系统中必不可少的一部分&#xff0c;而 Seata 框架是一种常用的分布式事务处理方式。在 SSM 框架中&#xff0c;我们可以使用 Seata 框架来管理分布式事务。本文将介绍如何在 SSM 框架中使用 Seata …...

FreeRTOS任务相关API函数

任务创建和删除API函数 xTaskCreate() 创建任务。RAM BaseType_t xTaskCreate( TaskFunction_t pxTaskCode, //任务函数const char* const pcName, //任务名字const uint16_t usStackDepth,//任务堆栈大小void * const …...

VBA之正则表达式(42)-- 提取代码中变量名称

实例需求&#xff1a;待处理代码段如下所示&#xff0c;现在需要提取其中的变量名称。 Public pFactor As Integer Sub TestCode() Dim reg As New RegExp, a As Workbook Dim ms As VBScript_RegExp_55.MatchCollection Dim m As VBScript_RegExp_55.Match Dim i, j Dim x1, y…...

Unity Lightmapping Setting

如下图&#xff1a; Lightmapper: 使用什么硬件或算法渲染 Progressive CPU、Progressive GPU、Enlighten(新的算放目前用的比较少) 此数值会被用于分别乘以Direct Samples&#xff0c;Indirect Samples和Environment Samples这三个数值。这三个数值会被应用于…...

Android 12.0Camera2 静音时拍照去掉快门声音

1.概述 在12.0定制化开发时,在Camera2静音情况下有快门拍照声音,这就不符合使用规范了 静音的情况下拍照也不应该发出声音,所以在静音拍照流程中要求去掉快门声音 2.Camera2静音拍照去掉快门声音核心代码 Camera2拍照主要代码:/packages/apps/Camera2/src/com/android/cam…...

Win11硬盘分区

电脑重装了Win11系统&#xff0c;按WinE打开主文件夹&#xff0c;再点击此电脑&#xff0c;发现&#xff1a; 磁盘只有一个C盘。硬盘的所有空间都在该盘上了&#xff0c;那么我们怎么将其分区呢&#xff1f; Win11硬盘分区步骤&#xff1a; 步骤1&#xff1a; 按WinR输入dis…...

访客管理系统:Lobby Track Crack

Lobbytrack桌面 for 微软视窗 一个强大的、功能齐全的现场访客管理系统解决方案。在本地管理您的数据&#xff0c;网络工作站一起配置访客管理流程的各个方面。 扩展您的系统将本地 Web 模块 添加到您的 Lobbytrack 桌面系统&#xff0c;并允许您的员工使用本地 Intranet 上的 …...

Lidar AI Solution环境配置

目录 Lidar AI Solution环境配置前言1. Lidar AI Solution1.1 Pipeline overview1.2 GetStart 2. CUDA-BEVFusion2.1 3D目标检测(nuScenes验证集)2.2 演示2.3 模型和数据2.4 前置条件2.5 快速开始推理2.5.1 下载模型和数据到CUDA-BEVFusion文件夹2.5.2 配置environment.sh2.5.3…...

子串--子字符串 0528

210102 201012 A1A2…An An…A2A1 如何做&#xff0c; 翻转的是21&#xff0c;因为2>1; 翻转的是210&#xff0c;因为2>0; 翻转的是2101&#xff0c;因为2>1&#xff1b; 翻转的是21010&#xff0c;因为2>0&#xff1b; 翻转的是210102&#xff0c;因为22且1&…...

大数据教程【01.04】--excel的使用

更多信息请关注WX搜索GZH&#xff1a;XiaoBaiGPT Excel中的大数据处理 Excel是一款功能强大的电子表格软件&#xff0c;它广泛用于数据处理和分析。对于大数据处理&#xff0c;Excel提供了多种功能和工具&#xff0c;可以帮助用户处理大量的数据。在本教程中&#xff0c;我们…...

Java输入输出流

目录 一、数据流概念 1.输入输出的概念​ 2.流的概念 3.流的操作 二、常用的流分类 三、文件输入输出流 1.FileReader和FileWriter 2.FileInputStream和FileOutStream 四、复制文件 一、数据流概念 1.输入输出的概念​ 输入输出技术用于处理设备之间的数据传输&#x…...

1688商品ID采集一件代发详情页面数据

本篇博文介绍了对1688商品详情API的二次封装&#xff0c;将URL参数封装成Python函数&#xff0c;直接传入参数即可获取搜索结果&#xff0c;例如1688商品标题、价格、一件代发、sku属性和URL等。提供了详细的代码示例和接口调用Demo。 1688.item_get-获得1688商品详情数据 1.请…...

【数据结构】红黑树(Red-Black Tree)

前言在上一篇博客中&#xff0c;我们学习了 AVL 树&#xff0c;为了保持绝对的平衡&#xff0c;它在插入和删除时会疯狂地进行左旋和右旋。但在现代的Java集合框架中&#xff08;如 TreeMap、TreeSet&#xff0c;以及 Java 8 之后的 HashMap&#xff09;&#xff0c;并没有选择…...

TradingAgents-CN:基于多智能体LLM的中文金融交易决策框架技术指南

TradingAgents-CN&#xff1a;基于多智能体LLM的中文金融交易决策框架技术指南 【免费下载链接】TradingAgents-CN 基于多智能体LLM的中文金融交易框架 - TradingAgents中文增强版 项目地址: https://gitcode.com/GitHub_Trending/tr/TradingAgents-CN 项目价值定位&…...

Python高效开发技巧汇总

这是一篇关于Python开发的技术文章示例内容&#xff0c;可以替换为真实文章内容。...

Phi-4-mini-reasoning开源模型优势:轻量级+高精度+低GPU资源占用实测

Phi-4-mini-reasoning开源模型优势&#xff1a;轻量级高精度低GPU资源占用实测 1. 模型概述 Phi-4-mini-reasoning是一款专注于推理任务的文本生成模型&#xff0c;特别擅长处理数学题、逻辑题、多步分析和简洁结论输出。与通用聊天模型不同&#xff0c;它采用了"题目输…...

AI集成开发工程师的技术实践与转型之路

第一章:技术架构演进与AI融合趋势 1.1 传统开发范式的演进 现代软件开发正经历从单一业务系统向智能化业务系统的转型。传统的.NET技术栈作为企业级应用开发的基石,其技术架构也在不断演进: // 典型的三层架构示例 public class BusinessLogic {private readonly IDataAc…...

sinx/x在0到无穷积分的条件收敛性分析与证明

1. 从物理现象到数学问题&#xff1a;为什么研究sinx/x的积分&#xff1f; 我第一次接触sinx/x的积分是在信号处理课程中&#xff0c;这个看似简单的函数在傅里叶变换和频谱分析中扮演着关键角色。工程师们用它来描述理想低通滤波器的频率响应&#xff0c;物理学家则在衍射现象…...

深入解析CC Switch架构:构建AI开发工具统一管理引擎

深入解析CC Switch架构&#xff1a;构建AI开发工具统一管理引擎 【免费下载链接】cc-switch A cross-platform desktop All-in-One assistant tool for Claude Code, Codex, OpenCode, openclaw & Gemini CLI. 项目地址: https://gitcode.com/GitHub_Trending/cc/cc-swit…...

intv_ai_mk11部署避坑指南:端口映射失败、响应延迟、乱码重复等问题解决方案

intv_ai_mk11部署避坑指南&#xff1a;端口映射失败、响应延迟、乱码重复等问题解决方案 1. 环境准备与快速部署 1.1 系统要求 操作系统&#xff1a;Ubuntu 20.04/22.04 LTSGPU&#xff1a;NVIDIA显卡&#xff08;至少16GB显存&#xff09;内存&#xff1a;32GB以上存储&…...

Phi-4-mini-reasoning科研协作:Jupyter Notebook嵌入式推理插件

Phi-4-mini-reasoning科研协作&#xff1a;Jupyter Notebook嵌入式推理插件 1. 模型简介 Phi-4-mini-reasoning是一个基于合成数据构建的轻量级开源模型&#xff0c;专注于高质量、密集推理的数据处理能力。作为Phi-4模型家族的一员&#xff0c;它经过专门微调以提升数学推理…...

TPCH dbgen数据生成工具在Linux环境下的配置与实战

1. 环境准备&#xff1a;从零搭建TPCH测试环境 第一次接触TPCH dbgen工具时&#xff0c;我花了整整两天时间才搞明白所有依赖关系。这个工具虽然功能强大&#xff0c;但官方文档确实不够友好。下面把我踩过的坑都总结出来&#xff0c;让你能快速上手。 系统要求方面&#xff0c…...