您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 1浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC基础-16-基于SpringMVC的restful架构风格

星夜孤帆 发布时间:2019-04-22 16:08:17 ,浏览量:1

pom.xml


  4.0.0
  com.monkey1024
  05mvc
  0.0.1-SNAPSHOT
  war
  
  		
		    com.alibaba
		    fastjson
		    1.2.4
		
        
            junit
            junit
            3.8.1
            test
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        
 
    
    
    	
        05mvc
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                    1.8
                    1.8
                    UTF-8
                
            
        
    

web.xml



  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    springMVC
    /
  

springmvc.xml



 	
 	
 		
 			
 				text/plain;charset=UTF-8
 				application/json;charset=UTF-8
 			
 		
 	
 	
 	
 		
 			
 		
 	
 	
 	
 	
 	
	
	
	
	
	
	
	
	
		
		
	
	
	
 
 

User.java

package com.monkey1024.bean;

import java.time.LocalDate;

import org.springframework.format.annotation.DateTimeFormat;

/*
 * 用户
 */
public class User {	
	private String name;
	
	private String phone;
	
	private String address;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private LocalDate birthday;
	
	public User(){}
	
	public User(String name, String phone, String address, LocalDate birthday){
		this.name = name;
		this.phone = phone;
		this.address = address;
		this.birthday = birthday;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public LocalDate getBirthday() {
		return birthday;
	}

	public void setBirthday(LocalDate birthday) {
		this.birthday = birthday;
	}

}

DataUtil.java

package com.monkey1024.util;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.monkey1024.bean.User;

/*
  * 模拟生成数据的工具类
 */
public class DataUtil {
	private static HashMap dataMap = new HashMap();
	
	//模拟初始化数据
	static{
		User user1 = new User("jack","18888888","北京",LocalDate.of(2012, 1, 1));
		User user2 = new User("paul","16666666","上海",LocalDate.of(2013, 1, 1));
		User user3 = new User("andy","19999999","深圳",LocalDate.of(2014, 1, 1));
		dataMap.put("1", user1);
		dataMap.put("2", user2);
		dataMap.put("3", user3);
	}
	
	/*
	 * 查询全部数据
	 */
	public static HashMap findAll(){
		return dataMap;
	}
	/*
	 * 根据id进行查询
	 */
	public static User findUserById(String id){
		return dataMap.get(id);
	} 
	
	/*
	 * 添加操作
	 */
	public static void create(User user) throws Exception{
		//遍历map找到key的最大值
		Set entries = dataMap.entrySet();
		Iterator iterator = entries.iterator();
		
		int max = 3;
		while(iterator.hasNext()){
			Map.Entry next = iterator.next();
			int i = Integer.parseInt(next.getKey());
			if(max            
关注
打赏
1636984416
查看更多评论
0.0408s