您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 6浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC基础-15-使用SpringMVC实现简单的增删改查

星夜孤帆 发布时间:2019-04-22 12:00:10 ,浏览量:6

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.1633s