SpringBoot用户CRUD
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);
}
SpringBoot用户CRUD的更多相关文章
- 11. SpringBoot 之CRUD实例
SpringBoot静态页路径,可直接通过URL访问的: /META-INF/resources /resources /static /public 而 5. /template 只和模板引擎 ...
- SpringBoot Restful Crud
一个简单的Restful Crud实验 默认首页的访问设置: // 注册 自定义的mvc组件,所有的WebMvcConfigurer组件都会一起起作用 @Bean public WebMvcConfi ...
- springboot用户登陆密码两次md5加密
1.用户端:PASS = MD5(明文 + 固定salt) 2.服务端:PASS = MD5(用户输入 + 随机salt) 引入依赖包 <dependency> <groupId&g ...
- SpringBoot 中 JPA 的使用
详细连接 简书https://www.jianshu.com/p/c14640b63653 新建项目,增加依赖 在 Intellij IDEA 里面新建一个空的 SpringBoot 项目.具体步骤参 ...
- Restful Api CRUD 标准示例 (Swagger2+validator)
为什么要写这篇贴? 要写一个最简单的CRUD 符合 Restful Api 规范的 一个Controller, 想百度搜索一下 直接复制拷贝 简单修改一下 方法内代码. 然而, 搜索结果让我无 ...
- springboot的第一节课
快速开始spring boot应用 官方向导搭建boot应用 地址:http://start.spring.io/ 设置项目属性: 3.解压,拷贝到工作空间,导入maven项目 4.写Controll ...
- Spring Boot 系列教程9-swagger-前后端分离后的标准
前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...
- CRM权限管理
CRM权限管理 一.概念 权限管理就是管理用户对于资源的操作.本 CRM 系统的权限(也称作资源)是基于角色操作权限来实现的,即RBAC(Role-Based Access Control,基于角色的 ...
- Java权限管理(授权与认证)
CRM权限管理 有兴趣的同学也可以阅读我最近分享的:Shiro框架原理分析 (PS : 这篇博客里面介绍了使用Shiro框架的方式实现权限管理) https://www.cnblogs.com/y ...
随机推荐
- 0-mybatis目录
mybatis 第一天: 对原生态jdbc程序(单独使用jdbc开发)问题总结 框架原理 入门程序 用户的增.删.改.查 开发dao两种方法: 原始dao开发方法(程序需要编写dao接口和dao实现类 ...
- Hadoop- Hadoop环境搭建
Windows下Hadoop的安装 准备工具:64位的JDK,Hadoop安装包(我使用的是2.6.1) JDK下载地址 官网: http://www.oracle.com/technetwork/j ...
- 城市旅游ppt模板
城市旅游ppt模板,城市,旅游,旅行,休闲. 下载:http://www.huiyi8.com/lvyoumuban/ppt/
- LoadRunner监控图表与配置(二)监控运行状况和交易状况
1.在左侧Available Graphs视图中展开Runtime Graphs节点,选择其中一种类型添加至控制器运行标签的界面. 2.在图中显示的空白区域点击右键,在弹出的快捷菜单中选择config ...
- ajax异步上传文件FormDate方式,html支持才可使用
今天需要做一个头像的预览功能,所以我想到了异步上传文件. 总结几点: 异步上传难点: 文件二进制流如何获取 是否需要设置表单的头,就是content-Type那里.异步,所以无所谓了吧. 其他就差不多 ...
- hdu-5862 Counting Intersections(线段树+扫描线)
题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 洛谷P1584 魔杖
题目描述 Smart在春游时意外地得到了一种好东西——一种非常珍贵的树枝.这些树枝可以用来做优质的魔杖! 选择怎样的切割方式来制作魔杖非常重要,关键问题是——一把魔杖既不能太长.又不能太短,且制作出来 ...
- BZOJ2028:[SHOI2009]会场预约(平衡树版)
浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...
- HDU4348:To the moon
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227. ...
- 洛谷 P4512 [模板] 多项式除法
题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...