码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC中CRUD实例

时间:2018-03-04 17:15:16      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:hidden   filter   spring   ++   res   framework   ref   art   tla   

一:新建项目

1.结构

  

2.添加lib

 

3.配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>SpringMvcCRUD</display-name>
 4   <!-- 配置DispatchServlet -->
 5     <servlet>
 6         <servlet-name>DispatchServlet</servlet-name>
 7         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 8         <init-param>
 9             <param-name>contextConfigLocation</param-name>
10             <param-value>classpath:springmcv.xml</param-value>
11         </init-param> 
12         <load-on-startup>1</load-on-startup>
13     </servlet>
14     <!-- 表示所有的请求都会走DispatcherServlet -->
15     <servlet-mapping>
16         <servlet-name>DispatchServlet</servlet-name>
17         <url-pattern>/</url-pattern>
18     </servlet-mapping>
19     
20     <!-- 配置过滤器 ,把POST请求转为DELETE,PUT请求-->
21     <filter>
22         <filter-name>HiddenHttpMethodFilter</filter-name>
23         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
24     </filter>
25     <filter-mapping>
26         <filter-name>HiddenHttpMethodFilter</filter-name>
27         <url-pattern>/*</url-pattern>
28     </filter-mapping>
29 </web-app>

 

4.配置springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context 
 9         http://www.springframework.org/schema/context/spring-context-4.0.xsd
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12     <!-- 配置自定义扫描的包 -->               
13     <context:component-scan base-package="com.spring.it" ></context:component-scan>
14     
15     <!-- 配置视图解析器 -->
16     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17         <property name="prefix" value="/WEB-INF/views/" />
18           <property name="suffix" value=".jsp" />
19     </bean>    
20 </beans>

 

5.实体类Department

 1 package com.spring.it.enties;
 2 
 3 public class Department {
 4 
 5     private Integer id;
 6     private String departmentName;
 7 
 8     public Department() {
 9         
10     }
11     
12     public Department(int i, String string) {
13         this.id = i;
14         this.departmentName = string;
15     }
16 
17     public Integer getId() {
18         return id;
19     }
20 
21     public void setId(Integer id) {
22         this.id = id;
23     }
24 
25     public String getDepartmentName() {
26         return departmentName;
27     }
28 
29     public void setDepartmentName(String departmentName) {
30         this.departmentName = departmentName;
31     }
32 
33     @Override
34     public String toString() {
35         return "Department [id=" + id + ", departmentName=" + departmentName
36                 + "]";
37     }
38     
39 }

 

6.实体类Employee

 1 package com.spring.it.enties;
 2 
 3 import java.util.Date;
 4 import org.springframework.format.annotation.DateTimeFormat;
 5 import org.springframework.format.annotation.NumberFormat;
 6 
 7 public class Employee {
 8 
 9     private Integer id;
10     private String lastName;
11     private String email;
12     //1 male, 0 female
13     private Integer gender;
14     private Department department;
15     
16     public Employee(Integer id, String lastName, String email, Integer gender,
17             Department department) {
18         super();
19         this.id = id;
20         this.lastName = lastName;
21         this.email = email;
22         this.gender = gender;
23         this.department = department;
24     }
25 
26     public Employee() {
27         
28     }
29     public Integer getId() {
30         return id;
31     }
32 
33     public void setId(Integer id) {
34         this.id = id;
35     }
36 
37     public String getLastName() {
38         return lastName;
39     }
40 
41     public void setLastName(String lastName) {
42         this.lastName = lastName;
43     }
44 
45     public String getEmail() {
46         return email;
47     }
48 
49     public void setEmail(String email) {
50         this.email = email;
51     }
52 
53     public Integer getGender() {
54         return gender;
55     }
56 
57     public void setGender(Integer gender) {
58         this.gender = gender;
59     }
60 
61     public Department getDepartment() {
62         return department;
63     }
64 
65     public void setDepartment(Department department) {
66         this.department = department;
67     }
68     
69     @Override
70     public String toString() {
71         return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender
72                 + ", department=" + department + "]";
73     }
74 
75     
76 }

 

7.Dao---DepartmentDao

 1 package com.spring.it.dao;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import org.springframework.stereotype.Repository;
 8 import com.spring.it.enties.Department;
 9 
10 @Repository
11 public class DepartmentDao {
12 
13     private static Map<Integer, Department> departments = null;
14     
15     static{
16         departments = new HashMap<Integer, Department>();
17         
18         departments.put(101, new Department(101, "D-AA"));
19         departments.put(102, new Department(102, "D-BB"));
20         departments.put(103, new Department(103, "D-CC"));
21         departments.put(104, new Department(104, "D-DD"));
22         departments.put(105, new Department(105, "D-EE"));
23     }
24     
25     public Collection<Department> getDepartments(){
26         return departments.values();
27     }
28     
29     public Department getDepartment(Integer id){
30         return departments.get(id);
31     }
32     
33 }

 

8.EmployeeDao

 1 package com.spring.it.dao;
 2 
 3 import com.spring.it.enties.Department;
 4 import java.util.Collection;
 5 import java.util.HashMap;
 6 import java.util.Map;
 7 
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Repository;
10 import com.spring.it.enties.Employee;
11 @Repository
12 public class EmployeeDao {
13 
14     private static Map<Integer, Employee> employees = null;
15     
16     @Autowired
17     private DepartmentDao departmentDao;
18     
19     static{
20         employees = new HashMap<Integer, Employee>();
21 
22         employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
23         employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
24         employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
25         employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
26         employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
27     }
28     
29     private static Integer initId = 1006;
30     
31     public void save(Employee employee){
32         if(employee.getId() == null){
33             employee.setId(initId++);
34         }
35         
36         employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
37         employees.put(employee.getId(), employee);
38     }
39     
40     public Collection<Employee> getAll(){
41         return employees.values();
42     }
43     
44     public Employee get(Integer id){
45         return employees.get(id);
46     }
47     
48     public void delete(Integer id){
49         employees.remove(id);
50     }
51 }

 

9.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二:

SpringMVC中CRUD实例

标签:hidden   filter   spring   ++   res   framework   ref   art   tla   

原文地址:https://www.cnblogs.com/juncaoit/p/8505035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!