2021-01-25

学习SpringMVC的第三天

目录

学习SpringMVC的第三天

一、重定向

二、RESTful SpringMVC CRUD

1、环境搭建

二、实例:添加

三、链接


一、重定向

@RequestMapping("/testRedirect")
	public String testRedirect() {
		System.out.println("testRedirect");
		return "redirect:/index.jsp";
	}

二、RESTful SpringMVC CRUD

结构

1、环境搭建

(1)创建动态项目并导包(有些不是必须的)

(2)配置web.xml和springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">  
 <!-- 配置SpringVMC的DispatcherServlet -->  
    <servlet>  
        <servlet-name>springDispatcherServlet</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>  
  
    <!-- Map all requests to the DispatcherServlet for handling -->  
    <servlet-mapping>  
        <servlet-name>springDispatcherServlet</servlet-name>  
        <url-pattern>/</url-pattern>  
 	</servlet-mapping>
 	
 	<!-- 
 	配置org.springframework.web.filter.HiddenHttpMethodFilter: 可以把POST请求转为DELETE 或  POST请求
 	 -->
 	 <filter>
 	 	<filter-name>HiddenHttpMethodFilter</filter-name>
 	 	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 	 </filter>
 	 <filter-mapping>
 	 	<filter-name>HiddenHttpMethodFilter</filter-name>
 	 	<url-pattern>/*</url-pattern>
 	 </filter-mapping>
 	
 </web-app>  
<?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"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
            
    <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
    
    <!-- 配置视图解析处理器:如何把handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/views/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
 
    <!-- 配置视图BeanNameViewResolver解析器:使用视图名字来解析视图 -->
    <!-- 通过order属性来定义视图的优先级,order越小优先级越高 -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
    	<property name="order" value="100"></property>
    </bean>
 	<!-- 配置国际化资源文件 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    	<property name="basename" value="i18n"></property>
    </bean>
    <!-- 配置直接转发的页面 -->
    <mvc:view-controller path="/success" view-name="success"/>
    <!-- 在实际开发中通常都需配置mvc:annotation-driven -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
</beans>

(3)添加相关文件

package com.atguigu.springmvc.crud.entity;

public class Department {

	private Integer id;
	private String departmentName;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	public Department(int id, String departmentName) {
		super();
		this.id = id;
		this.departmentName = departmentName;
	}
	public Department() {
		super();
	}
	@Override
	public String toString() {
		return  departmentName;
	}
	
	
	
}
package com.atguigu.springmvc.crud.entity;

public class Employee {

	private Integer id;
	private String lastName;
	private String email;
	//1 male 0 female
	private Integer gender;
	private Department department;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public Employee(int id, String lastName, String email, int gender, Department department) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.email = email;
		this.gender = gender;
		this.department = department;
	}
	public Employee() {
		super();
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender
				+ ", department=" + department + "]";
	}
	
	
	
}
package com.atguigu.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.atguigu.springmvc.crud.entity.Department;

@Repository
public class DepartmentDao {
	
	private static Map<Integer, Department> departments = null;
	
	static {
		departments = new HashMap<Integer , Department>();
		
		departments.put(101,new Department(101,"D-AA"));
		departments.put(102,new Department(102,"D-BB"));
		departments.put(103,new Department(103,"D-CC"));
		departments.put(104,new Department(104,"D-DD"));
		departments.put(105,new Department(105,"D-EE"));
	}
	
	public Collection<Department> getDepartments() {
		return departments.values();
	}
	public Department getDepartments(Integer id) {
		return departments.get(id);
	}
}
package com.atguigu.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.atguigu.springmvc.crud.entity.Department;
import com.atguigu.springmvc.crud.entity.Employee;

@Repository
public class EmployeeDao {
	
	private static Map<Integer, Employee> employees = null;

	@Autowired
	private DepartmentDao departmentDao;
	
	static {
		employees = new HashMap<Integer,Employee>();
		
		employees.put(1001, new Employee(1001,"E-AA","aa@163.com",1,new Department(101,"D-AA")));
		employees.put(1002, new Employee(1002,"E-BB","bb@163.com",1,new Department(102,"D-BB")));
		employees.put(1003, new Employee(1003,"E-CC","cc@163.com",1,new Department(103,"D-CC")));
		employees.put(1004, new Employee(1004,"E-DD","dd@163.com",1,new Department(104,"D-DD")));
		employees.put(1005, new Employee(1005,"E-EE","ee@163.com",1,new Department(105,"D-EE")));
	}
	
	private static Integer initId = 1006;
	
	
	public void save(Employee employee) { 
		if(employee.getId() == null) {
			employee.setId(initId++); 
		}
		  
		employee.setDepartment(departmentDao.getDepartments(employee.getDepartment().getId()));
		employees.put(employee.getId(), employee);
	}
	  
	public Collection<Employee> getAll() {
		return employees.values();
	}
	public Employee get(Integer id) {
		return employees.get(id);
	}
	public void delete(Integer id) {
		employees.remove(id);
	}
	 
}

二、实例:添加

package com.atguigu.springmvc.crud.handlers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.atguigu.springmvc.crud.dao.DepartmentDao;
import com.atguigu.springmvc.crud.dao.EmployeeDao;
import com.atguigu.springmvc.crud.entity.Employee;

@Controller
public class EmployeeHandler {
	
	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private DepartmentDao departmentDao;

	@RequestMapping("/emps")
	public String list(Map<String, Object> map) {
		map.put("employees", employeeDao.getAll());
		return "list";
	}
	
	@RequestMapping(value="emp",method=RequestMethod.GET)
	public String input(Map<String, Object> map) {
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", new Employee());
		return "input";
	}
	
	@RequestMapping(value="/emp",method=RequestMethod.POST)
	public String save(Employee employee) {
		 employeeDao.save(employee);
		return "redirect:/emps";
	}
	
}
<!--index.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="emps">List All Employees</a>
</body>
</html>
<!--input.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 
		为什么使用form标签?
			可以更快速开发出表单页面,而且可以更方便的进行表单值得回显
		注意:
			可以通过modelAttribute属性指定绑定的模型属性,
			若没有指定该属性,则默认从request域对象中读取command的表单bean
			如果该属性值也不存在,则会发生错误。
	 -->
	 <form:form action="emp" method="POST" modelAttribute="employee">
	 	<!-- path属性对应HTML表单标签的name属性值 -->
	 	LastName:<form:input path="lastName"/>
	 	<br>
	 	Email:<form:input path="email"/>
	 	<%
	 		Map<String , String> genders = new HashMap();
	 		genders.put("1","male");
	 		genders.put("0","female");
	 		
	 		request.setAttribute("genders", genders);
	 	%>
	 	<br>
	 	Gender:<form:radiobuttons path="gender" items="${genders }"/>
	 	<br>
	 	Department:<form:select path="department.id" items="${departments }" itemLable="departmentName" itemValue="id"></form:select>
	 	<br>
	 	<input type="submit" value="Submit">
	 </form:form>
</body>
</html>
<!--list.jsp-->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:if test="${empty requestScope.employees }">
		没有任何员工信息
	</c:if>
	<c:if test="${!empty requestScope.employees }">
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<th>ID</th>
				<th>LastName</th>
				<th>Email</th>
				<th>Gender</th>
				<th>Department</th>
				<th>Edit</th>
				<th>Delete</th>
			</tr>
				
				<c:forEach items="${requestScope.employees }" var="emp">
					<tr>
						<td>${emp.id }</td>
						<td>${emp.lastName }</td>
						<td>${emp.email }</td>
						<td>${emp.gender == 0 ? 'Female' : 'Male' }</td>
						<td>${emp.department.departmentName }</td>
						<td><a href="">ESdit</a></td>
						<td><a href="">Delete</a></td>
					</tr>
				</c:forEach>
		</table>
	</c:if>
	
	<br><br>
	
	<a href="emp"> Add New Employee</a>
	
</body>
</html>

三、链接

链接:https://pan.baidu.com/s/1v7znGo3fIr0pJUOqK4gwlA 
提取码:o746 
复制这段内容后打开百度网盘手机App,操作更方便哦--来自百度网盘超级会员V2的分享

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值