1.SpringMVC的处理流程
- 一、其核心操作流程:
- 1.所有的请求,都会交给DispatcherServlet核心控制器处理;DispatcherServlet核心控制器将接收到的请求交给HandlerMapping处理;
- 2.由 DispatcherServlet 控制器寻找一个或多个 HandlerMapping,找到处理请求的 Controller。
- 3.DispatcherServlet 将请求提交到 Controller。
- 4.Controller 调用业务逻辑处理后返回 ModelAndView。
- 5.DispatcherServlet 寻找一个或多个 ViewResolver 视图解析器,找到 ModelAndView 指定的视图。
- 6.视图负责将结果显示到客户端。
包含 4 个 Spring MVC 接口,即 DispatcherServlet、HandlerMapping、Controller 和 ViewResolver。
Spring MVC 所有的请求都经过 DispatcherServlet 来统一分发,在 DispatcherServlet 将请求分发给 Controller 之前需要借助 Spring MVC 提供的 HandlerMapping 定位到具体的 Controller。
HandlerMapping 接口负责完成客户请求到 Controller 映射。
Controller 接口将处理用户请求,一旦 Controller 处理完用户请求,将返回 ModelAndView 对象给 DispatcherServlet 前端控制器,ModelAndView 中包含了模型(Model)和视图(View)。
从宏观角度考虑,DispatcherServlet 是整个 Web 应用的控制器;从微观考虑,Controller 是单个 Http 请求处理过程中的控制器,而 ModelAndView 是 Http 请求过程中返回的模型(Model)和视图(View)。
ViewResolver 接口(视图解析器)在 Web 应用中负责查找 View 对象,从而将相应结果渲染给客户。
2. SpringJDBC的用途以及使用方式
一、用途:
SpringJDBC主要作用相当于MyBatis技术,是对JDBC的操作技术,对数据库进行的一系列的操作行为,大大简化了对数据库的操作。
Spring JDBC抽象框架提供了对JDBC操作的完整封装,包括:指定数据库连接参数、打开数据库连接、
预编译并执行SQL语句、遍历查询结果、处理抛出的任何异常、处理事务、关闭数据库连接等。
二、使用方式:
①导入SpringJDBC的相关依赖;
②Spring框架提供了JDBCTemplate类,该类继承自JdbcAccessor类,实现自JdbcOperations接口。
在JDBCTemplate类中,提供了大量的更新、查询数据库的方法,通过这些方法,我们可以操作数据库。
- execute()
execute(String sql):通过该方法,可以执行sql语句;
- update():用于执行更新、插入、删除操作;
- query():用于执行数据库执行操作。
③在xml文件中完成相关的配置信息:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n33" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">web.xml配置文件:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springcore.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app></pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">springCore.xml配置文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeDAO" class="com.apesource.dao.EmployeeDAO">
<property name="jdbcTemplate" ref="jdbcTemplateBean"/>
</bean>
<bean id="dataSourceBean" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://47.108.65.254:3306/ApesourceAggregateData?useSSL=false&serverTimezone=UCT"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="apesource_stu"/>
<property name="password" value="Apesource6!6"/>
</bean>
<bean id="jdbcTemplateBean" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceBean"/>
</bean>
</beans>
</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n49" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">springMvc.xml配置文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="/home.do" class="com.apesource.web.controller.HomeController">
<property name="employeeDAO" ref="employeeDAO"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans></pre>
④自定义Controller类(这里定义的是HomeController)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n52" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">package com.apesource.web.controller;
import com.apesource.dao.EmployeeDAO;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
public class HomeController implements Controller {
private EmployeeDAO employeeDAO;
public void setEmployeeDAO(EmployeeDAO employeeDAO) {
this.employeeDAO = employeeDAO;
}
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("请求到啦---------------------");
employeeDAO.listEmployee();
ModelAndView result = new ModelAndView();
result.setViewName("index");
result.addObject("code", UUID.randomUUID().toString());
result.addObject("emp",employeeDAO.listEmployee());
return result;
}
}
</pre>
⑤数据访问层DAO层的设计
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public List<EmployeeInfo> listEmployee(){
String sql = "select * from employee_info";
//参数1:要执行的SQL
//2.RowMapper对于数据执行的处理逻辑
List<EmployeeInfo> employeeInfoList = jdbcTemplate.query(sql,new EmployeeRowMapper());
return employeeInfoList;
}
public List<EmployeeInfo> listEmployeeByName(String empName){
String sql = "select * from employee_info where emp_name = ?"; List<EmployeeInfo> employeeInfoList
= jdbcTemplate.query(sql,new EmployeeRowMapper(),empName);
return employeeInfoList;
}
}
class EmployeeRowMapper implements RowMapper<EmployeeInfo>{
@Override
public EmployeeInfo mapRow(ResultSet rs, int i) throws SQLException { EmployeeInfo employeeInfo = new EmployeeInfo(); employeeInfo.setEmpId(rs.getInt("emp_id")); employeeInfo.setEmpName(rs.getString("emp_name")); employeeInfo.setEmpDept(rs.getString("emp_dept")); employeeInfo.setEmpSalary(rs.getDouble("emp_salary")); employeeInfo.setIdCardNo(rs.getString("id_card_no")); employeeInfo.setPhoneNumber(rs.getString("phone_number"));
return employeeInfo;
}
}
</pre>
⑥建立实体类/通过数据库自动生成实体类
⑦搭建Tomcat环境,并将项目添加至Tomcat中