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<String,User> 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<String,User> 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<Map.Entry<String,User>> entries = dataMap.entrySet();
Iterator<Map.Entry<String,User>> iterator = entries.iterator();
int max = 3;
while(iterator.hasNext()){
Map.Entry<String, User> next = iterator.next();
int i = Integer.parseInt(next.getKey());
if(max<i){
max = i;
}
}
//将最大值做自增运算,然后作为key放到map中
dataMap.put(++max+"", user);
}
/*
* 修改用户
*/
public static void update(String id,User user) throws Exception{
dataMap.put(id, user);
}
/*
* 删除用户
*/
public static void delete(String id) throws Exception{
dataMap.remove(id);
}
}
UserController.java
package com.monkey1024.controller;
import java.util.HashMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.monkey1024.bean.User;
import com.monkey1024.util.DataUtil;
/*
* 实现简单的增删改查
*/
@Controller
public class UserController {
/*
* 查找所有用户
*/
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception{
HashMap<String,User> allUser = DataUtil.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("allUser",allUser);
mv.setViewName("user_list");
return mv;
}
/*
* 根据id查询
*/
@RequestMapping("/findById.do")
public ModelAndView findById(String id) throws Exception{
ModelAndView mv = new ModelAndView();
User user = DataUtil.findUserById(id);
HashMap<String,User> allUser = new HashMap<>();
allUser.put(id, user);
mv.addObject("id",id);
mv.addObject("alluser",allUser);
mv.setViewName("user_list");
return mv;
}
/*
* 添加
*/
@RequestMapping("/create.do")
public String create(User user) throws Exception{
DataUtil.create(user);
return "redirect:findAll.do";
}
/*
* 删除
*/
@RequestMapping("/delete.do")
public String delete(String id) throws Exception{
DataUtil.delete(id);
return "redirect:findAll.do";
}
/*
* 用户跳转到页面
*/
@RequestMapping("/goUpdate.do")
public ModelAndView goUpdate(String id) throws Exception{
User user = DataUtil.findUserById(id);
ModelAndView mv = new ModelAndView();
mv.addObject("user",user);
mv.addObject("id",id);
mv.setViewName("user_update");
return mv;
}
/*
* 修改操作
*/
@RequestMapping("/update.do")
public String update(String id,User user) throws Exception{
DataUtil.update(id,user);
return "redirect: findAll.do";
}
}