1.准备
http://start.spring.io/ 这里地址去直接生成你需要的项目信息,如何你本身ide以及集成了springboot 那么可以直接生成项目信息
需要的知识:java,spring,springmvc,thymelafe
2开始
根据图片项目结构信息可以推断,我们需要写下面几个类:
package com.dgw.boot.dgw.domain; /**
* @author DGW-PC
* @data 2018年7月17日
*/
public class User{ private Long id;
private String name;
private String email; public User() { } public User(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
}
Dao beans
public interface UserRepository { User savaOrUpdateUser(User user); void deleteUser(long id); User getUserById(long id); List<User> listUsre(); } @Repository
public class UserRepositoryImpl implements UserRepository { private static AtomicLong count=new AtomicLong();
private ConcurrentMap<Long, User> useMap=new ConcurrentHashMap<>(); @Override
public User savaOrUpdateUser(User user) {
Long id = user.getId();
if(id==null) {
id=count.incrementAndGet();
user.setId(id);
}
this.useMap.put(id, user);
return user;
} @Override
public void deleteUser(long id) {
this.useMap.remove(id);
} @Override
public User getUserById(long id) {
return this.useMap.get(id);
} @Override
public List<User> listUsre() {
return new ArrayList<>(this.useMap.values());
} }
控制器:
/**
* 从 用户存储库 获取用户列表
* @return
*/
private List<User> getUserlist() {
return userRepository.listUser();
} /**
* 查询所用用户
* @return
*/
@GetMapping
public ModelAndView list(Model model) {
model.addAttribute("userList", getUserlist());
model.addAttribute("title", "用户管理");
return new ModelAndView("users/list", "userModel", model);
} /**
* 根据id查询用户
* @param message
* @return
*/
@GetMapping("{id}")
public ModelAndView view(@PathVariable("id") Long id, Model model) {
User user = userRepository.getUserById(id);
model.addAttribute("user", user);
model.addAttribute("title", "查看用户");
return new ModelAndView("users/view", "userModel", model);
} /**
* 获取 form 表单页面
* @param user
* @return
*/
@GetMapping("/form")
public ModelAndView createForm(Model model) {
model.addAttribute("user", new User());
model.addAttribute("title", "创建用户");
return new ModelAndView("users/form", "userModel", model);
} /**
* 新建用户
* @param user
* @param result
* @param redirect
* @return
*/
@PostMapping
public ModelAndView create(User user) {
user = userRepository.saveOrUpateUser(user);
return new ModelAndView("redirect:/users");
} /**
* 删除用户
* @param id
* @return
*/
@GetMapping(value = "delete/{id}")
public ModelAndView delete(@PathVariable("id") Long id, Model model) {
userRepository.deleteUser(id); model.addAttribute("userList", getUserlist());
model.addAttribute("title", "删除用户");
return new ModelAndView("users/list", "userModel", model);
} /**
* 修改用户
* @param user
* @return
*/
@GetMapping(value = "modify/{id}")
public ModelAndView modifyForm(@PathVariable("id") Long id, Model model) {
User user = userRepository.getUserById(id); model.addAttribute("user", user);
model.addAttribute("title", "修改用户");
return new ModelAndView("users/form", "userModel", model);
}