Spring+SpringMVC+SpringJDBC搭建web项目实现商品查询

准备工作:创建商品数据库,以及商品表

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #创建数据库 DROP DATABASE IF EXISTS goodsDB; CREATE DATABASE goodsDB; USE goodsDB; #创建商品表 goods #id number 商品编号,主键 #name Varchar2(50) 商品名称,非空 #Price Number 商品单价,非空 #Num number 商品库存,必须大于0 CREATE TABLE goods( id INT PRIMARY KEY, NAME VARCHAR(50), price DOUBLE NOT NULL, imgs VARCHAR(200), num INT ); #添加数据 #创建数据库 DROP DATABASE IF EXISTS goodsDB; CREATE DATABASE goodsDB; USE goodsDB; #创建商品表 goods #id number 商品编号,主键 #name Varchar2(50) 商品名称,非空 #Price Number 商品单价,非空 #Num number 商品库存,必须大于0 CREATE TABLE goods( id INT PRIMARY KEY, NAME VARCHAR(50), price DOUBLE NOT NULL, imgs VARCHAR(200), num INT ); #添加数据 INSERT INTO goods VALUES('10010','迪奥999',250,'dior999.gif',100); INSERT INTO goods VALUES('10020','阿玛尼405',288,'405.gif',50); INSERT INTO goods VALUES('10030','TF16',398,'tf16.jpg',80); INSERT INTO goods VALUES('10040','YSL1966',998,'ysl1966.jpg',20); SELECT * FROM goods; |

-----------Spring+SpringMVC+SpringJDBC搭建web项目实现商品查询-----------

  1. 创建maven的web项目
  2. 打开pom.xml导入相关jar包依赖坐标 以及 打包 插件

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <dependencies> <!--spring-mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc </artifactId> <version>6.0.6</version> </dependency> <!-- 注解 --> <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> </dependency> <!--javaee-web:servlet 校验注解 --> <dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-web-api</artifactId> <version>9.1.0</version> <scope>provided</scope> </dependency> <!--jstl--> <dependency> <groupId>jakarta.servlet.jsp.jstl</groupId> <artifactId>jakarta.servlet.jsp.jstl-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jakarta.servlet.jsp.jstl</artifactId> <version>2.0.0</version> </dependency> <!--jsckson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> </dependency> <!--jdbc--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!--spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>6.0.6</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!-- 声明式事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>6.0.6</version> </dependency> <!--aspectjwear--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>6.0.6</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> <!--logback 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> <plugins> <!-- 打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> </plugin> </plugins> |

注意:引入logback.xml的日志文件

3.外部配置文件jdbc.properties 属性配置文件到resources

|--------------------------------------------------------------------------------------------------------------------------------------------|
| jdbc.url=jdbc:mysql://localhost:3306/goodsDB?serverTimezone=UTC jdbc.userName=root jdbc.userPwd= jdbc.driverClass=com.mysql.cj.jdbc.Driver |

4.创建web容器初始化配置类:MyWebAppInit

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class MyWebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer { /** * 指定 root 容器对应的配置类 :service / dao 层的配置类 */ @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{Spring Config .class}; } /** *web 容器对应的配置类 : 指定 springmvc 的配置类 */ @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig .class}; } /** * 设置 dispatcherServlet 的处理路径 ! 一般情况下为 / 代表处理所有请求 ! */ @Override protected String[] getServletMappings() { return new String[]{"/ "}; } } |

  1. 创建SpringMVC的配置类: SpringMvcConfig.java

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @EnableWebMvc //开启mvc复合功能开关,启用SpringMVC框架,如注解驱动 @Configuration //配置类 @ComponentScan (basePackages = "com.bdqn.controller") //扫描controller控制层 public class SpringMvcConfig implements WebMvcConfigurer { /** * 配置视图解析器 */ @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/",".jsp"); } } |

  1. 创建Spring的配置类: SpringConfig

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Configuration // 配置 类注解 @ComponentScan ({"com.bdqn.dao","com.bdqn.service"}) // 注解扫描 @PropertySource ("classpath:jdbc.properties") // 加载 jdbc 的属性配置文件 @EnableTransactionManagement //开启事务管理器 public class SpringConfig { /** * 从配置文件中获取属性值 */ @Value("{jdbc.driverClass}") private String driver; @Value("{jdbc.url}") private String url; @Value("{jdbc.userName}") private String username; @Value("{jdbc.userPwd}") private String password; /** * 创建数据源对象 */ @Bean public DataSource dateSource(){ // 创建阿里连接池数据源对象 DruidDataSource dataSource=new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } /** * 创建 jdbcTemplate 模板对象 */ @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource){ JdbcTemplate jdbcTemplate=new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } /** * 装配事务管理实现对象 */ @Bean public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){ DataSourceTransactionManager transactionManager=new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource); return transactionManager; } } |

  1. 编写实体层 商品实体类 Goods

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Data @NoArgsConstructor @AllArgsConstructor public class Goods { private int id; private String name; private double price; private String imgs; private int num; } |

8.编写dao数据访问层

1.GoodsDao接口

|----------------------------------------------------------|
| public interface GoodsDao { List<Goods> selectAll(); } |

2.GoodsDaoImpl实现类

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Repository public class GoodsDaoImpl implements GoodsDao { @Autowired JdbcTemplate jdbcTemplate; /** * 查询所有商品信息 */ @Override public List<Goods> selectAll() { String sql="select * from goods"; List<Goods> list=jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Goods.class)); return list; } } |

9.编写service业务层

1.GoodsService接口

|-----------------------------------------------------------|
| public interface GoodsService { List<Goods> getAll(); } |

2.GoodsServiceImpl实现类

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Service public class GoodsServiceImpl implements GoodsService { @Autowired GoodsDao goodsDao; @Override public List<Goods> getAll() { return goodsDao.selectAll(); } } |

10.编写controller控制层

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Controller public class GoodsController { @Autowired GoodsService goodsService; /** * 获取 所有商品 控制器 */ @RequestMapping("/getAll") public ModelAndView getAll(){ ModelAndView mv=new ModelAndView(); List<Goods> list=goodsService.getAll(); // 设置数据模型,存储到 request 作用域 mv.addObject("list",list); // 设置跳转的逻辑视图 mv.setViewName("index"); return mv; } } |

  1. 编写index.jsp,将所有商品进行展示

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <table class="table col-12 table-hover table-bordered"> <thead class="thead-light"> <tr> <th scope="col"><input type="checkbox" name="all" />全选</td> </th> <th scope="col">商品编号</th> <th scope="col">商品名称</th> <th scope="col">单价</th> <th scope="col">图片</th> <th scope="col">库存</th> <th scope="col">库存</th> </tr> </thead> <tbody> <c:forEach items="{list}" var="goods"\> \ \\goods.id}" /></td> <td>{goods.id}\ \{goods.name}</td> <td>{goods.price}\ \\{goods.num}\ \ \删除\ \goods.id}" class="btn btn-primary btn-sm edit">修改</a> </td> </tr> </c:forEach> </tbody> </table> |

  1. el表达式无法正常显示,需要修改web.xml配置文件的版本为4.0

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <? xml version="1.0" encoding="UTF-8"?> <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"> </web-app> |

知识点:

1.放行静态资源

除jsp以外的请求都会进入到前端控制器,需要设置SpringMVC放行静态资源

静态资源包括:js、css、img等等

SpringMvcConfig配置类,进行配置

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /** * 放行静态资源 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { // 开启静态资源 configurer.enable(); } |

2.控制器方法返回值类型

|-----------------------------------------------------------------------------------------------------------------------------------------------|
| 1.ModelAndView 等于 数据模型+逻辑视图 addObject(name,value): 数据存储到request作用域 setViewName(url): 指定跳转地址逻辑视图 2.String 默认指定跳转地址逻辑视图名 3.void (原生servlet对象实现) |

3.参数绑定

1.基本类型参数

满足:请求参数名和方法参数名相同

如果请求参数名和方法参数名不相同,可以使用@RequestParam(value="请求参数名")

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 请求地址:<a class="page-link" href="getAll?pageNum= 2 ">2</a> @RequestMapping("/getAll") public ModelAndView getAll(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum){ } @RequestParam注解属性: value: 指定绑定请求参数名 required:必填项 defaultValue:默认值 |

2.对象类型参数

满足:请求参数名和对象的属性名相同,属性必须提供get/set方式

|----------------------------------------------------------------------------------------------------------------------------------|
| 请求地址:addGoods?id=1002&name=西瓜&price=99.9&imgs=西瓜.jpg&num=50 @RequestMapping("/goods") public String addGoods(Goods goods){ } |

3.一个参数名有多个参数值

如:复选框,发送选中的商品编号

|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| 请求地址:batchDelGoods?gids=10010&gids=10020 @RequestMapping("/batchDelGoods") public String batchDelGoods(@RequestParam("gids") List<Integer> gids){ } |

  1. JSON格式的数据 (postman测试)

@RequestBody主要用来接收前端传递给后端的json字符串中请求体中的数据的。

GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据。而是用POST方式进行提交在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

如果参数是放在请求体中,传入后台的话,那么后台要用@RequestBody才能接收到;如果不是放在请求体中的话,那么后台接收前台传过来的参数时,要用@RequestParam来接收。

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| JSON格式商品对象: {"id":1001,"name":"西瓜","price":9.9,"imgs":"2.jpg","num":100} @PostMapping (value = "/addGoods",produces = "text/html;charset=UTF-8") @ResponseBody public String addGoods(@RequestBody Goods goods){ System.out .println(goods); return "成功"; } |

  1. Model存储模型数据

|------------------------------------------------------------------------------------------|
| // 在形参位置声明Model类型变量,用于存储模型数据 ,作用域request级 public String handle( Model model) { } |

  1. 获取原生对象

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /** * 如果想要获取请求或者响应对象,或者会话等,可以直接在形参列表传入,并且不分先后顺序! * 注意: 接收原生对象,并不影响参数接收! */ public String handle(HttpSession session , HttpServletRequest request, HttpServletResponse response){ } |

  1. 获取Cookie对象

|------------------------------------------------------------------|
| public void handle(@CookieValue("JSESSIONID") String cookie) { } |

4.控制器跳转方式

|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1. 默认转发进行跳转 , 并且 自定义视图解析器生效 2. 指定跳转方式 1.加redirect: 前缀,指定重定向跳转 2.加forward: 前缀或不加,指定转发跳转 3.一旦指定跳转方式,自定义视图解析器生效, 总结:一旦往控制器跳转,视图地址必须加redirect: 或 forward: |

5.RestFul 风格传值

作用:将参数值作为url一个部分,简化页面url格式编写

示例:https://www.xinli001.com/info/100480332

演示:

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 请求地址:delGoods/10010 @RequestMapping("/delGoods/{id}") public String delGoods(@PathVariable("id") int id){ int row=goodsService.delete(id); return "redirect:/ getAll"; } |

6.ajax异步请求

异步请求,只响应数据,控制器需要恒不跳转,使用@ResponseBody注解

@ResponseBody作用:恒不跳转,将返回值按照特定的格式以流响应输出

1.如果返回值不满足key-value格式 (String类型),

设置响应头信息:text/html;charset=ISO-8859-1,并以流响应输出

注意:响应中文乱码,需要设置响应编码

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 前端异步请求: $.get("ajaxDelGoods",{"id":id},callback); /** * 异步删除商品信息 */ @RequestMapping(value = "/ajaxDelGoods",produces = "text/html;charset=UTF-8") @ResponseBody public String ajaxDelGoods(int id){ int row=goodsService.delete(id); return row>0?"成功":"失败"; } |

2.如果返回值满足key-value格式 (Map,对象,对象集合)

设置响应头信息:application/json;charset=UTF-8,并以流响应输出

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 前端异步请求:$.getJSON("ajaxGetGoods",{"id":id},callback); /** * 异步根据商品编号获取商品信息 */ @RequestMapping(value = "/ajaxGetGoods") @ResponseBody public Goods ajaxGetGoods(int id){ Goods goods=goodsService.getGoodsById(id); return goods; } |

7.在Web容器的初始化配置类配置springmvc提供解决中文乱码过滤器

MyWebAppInitiallizer.java

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /** * 配置统一字符编码的过滤器 */ @Override protected Filter[] getServletFilters() { CharacterEncodingFilter encodingFilter=new CharacterEncodingFilter(); encodingFilter.setEncoding("UTF-8"); return new Filter[]{encodingFilter}; } |

8. SpringMVC相关注解总结

|------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| 注解名 | 描述 |
| @EnableWebMvc | 注解驱动,启用SpringMVC配置.它提供了处理HTTP请求、管理控制器、处理视图以及管理其他与Web相关组件的功能。 如:处理器控制器、处理器适配器、json处理器等 |
| @Controller | 标识一个类为控制器的注解 |
| @RestController | 等于@Controller+@ResponseBody Rest风格的控制器,以JSON或XML形式返回响应结果 |
| @RequestMapping | 设置请求路径URL和处理请求控制器的映射关系 |
| @GetMapping | 用于映射Get请求到控制器方法 |
| @PostMapping | 用于映射Post请求到控制器方法 |
| @RequestParam | 获取请求参数值,将请求参数绑定到控制器方法的参数上 |
| @PathVariable | 获取RestFul风格的URL中值作为控制器方法的参数 |
| @RequestBody | 将post请求JSON格式的请求体的内容绑定到控制器方法的参数上 |
| @ResponseBody | 将控制器方法的返回值以流的形式响应输出 |

相关推荐
20岁30年经验的码农9 分钟前
爬虫基础
1024程序员节
licy__29 分钟前
计算机网络IP地址分类,子网掩码,子网划分复习资料
1024程序员节
Chris-zz1 小时前
Linux:磁盘深潜:探索文件系统、连接之道与库的奥秘
linux·网络·c++·1024程序员节
JasonYin~1 小时前
《探索 HarmonyOS NEXT(5.0):开启构建模块化项目架构奇幻之旅 —— 模块化基础篇》
1024程序员节
Teamol20202 小时前
求助帖:ubuntu22.10 auto install user-data配置了为何还需要选择语言键盘(如何全自动)
linux·ubuntu·1024程序员节
尘佑不尘2 小时前
shodan5,参数使用,批量查找Mongodb未授权登录,jenkins批量挖掘
数据库·笔记·mongodb·web安全·jenkins·1024程序员节
SeniorMao0073 小时前
结合Intel RealSense深度相机和OpenCV来实现语义SLAM系统
1024程序员节
网安_秋刀鱼3 小时前
CSRF防范及绕过
前端·安全·web安全·网络安全·csrf·1024程序员节
WW、forever3 小时前
【ArcGIS Pro实操第4期】绘制三维地图
1024程序员节