git clone https://github.com/JeffLi1993/spring-boot-core-book-demo.git
cd spring-boot-core-book-demo mvn clean install
2017-08-09 16:24:58.387 INFO 12193 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) 2017-08-09 16:24:58.395 INFO 12193 — [ main] s.b.core.ValidatingFormInputApplication : Started ValidatingFormInputApplication in 9.846 seconds (JVM running for 11.23)
@Entity
public class User implements Serializable {
/**
* 编号
*/
@Id
@GeneratedValue
private Long id;
/**
* 名称
*/
@NotEmpty(message = “姓名不能为空”)
@Size(min = 2, max = 8, message = “姓名长度必须大于 2 且小于 20 字”)
private String name;
/**
* 年龄
*/
@NotNull(message = “年龄不能为空”)
@Min(value = 0, message = “年龄大于 0”)
@Max(value = 300, message = “年龄不大于 300”)
private Integer age;
/**
* 出生时间
*/
@NotEmpty(message = “出生时间不能为空”)
private String birthday;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return “User{” +
“id=” + id +
“, name='” + name + ‘\” +
“, age=” + age +
“, birthday=” + birthday +
‘}';
}
}
@Entity
@Id
@GeneratedValue
/**
* 创建用户
* 处理 “/users” 的 POST 请求,用来获取用户列表
* 通过 @ModelAttribute 绑定参数,也通过 @RequestParam 从页面中传递参数
*/
@RequestMapping(value = “/create”, method = RequestMethod.POST)
public String postUser(ModelMap map,
@ModelAttribute @Valid User user,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
map.addAttribute(“action”, “create”);
return “userForm”;
}
userService.insertByUser(user);
return “redirect:/users/”;
}
/**
* 处理 “/users/{id}” 的 PUT 请求,用来更新 User 信息
*
*/
@RequestMapping(value = “/update”, method = RequestMethod.POST)
public String putUser(ModelMap map,
@ModelAttribute @Valid User user,
BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
map.addAttribute(“action”, “update”);
return “userForm”;
}
userService.update(user);
return “redirect:/users/”;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有