搭建项目:https://blog.csdn.net/afsya/article/details/91548140
添加 config、controller、model。
页面框架可任意选择,这里以 bootstrap 为例。
在 pom 文件中加入 json、thymeleaf 包。
net.sf.json-lib
json-lib
2.4
jdk15
org.springframework.boot
spring-boot-starter-thymeleaf
在 pom 文件中配置 build 启动类。
org.springframework.boot
spring-boot-maven-plugin
com.manage.ManageApplication
manage 中的 UserController 主要负责调用 user-service 的服务。
@Controller
@RequestMapping("/user")
public class UserController {
@Value("${user.service-name}")
private String userServiceName;
@Autowired
private RestTemplate restTemplate;
@GetMapping("toUser")
public String toUser() {
return "user";
}
/**
查询用户列表
*/
@GetMapping("getUserList")
@ResponseBody
public String getUserList(@ModelAttribute User user) {
ResponseEntity<ResultData> responseEntity = restTemplate.postForEntity("http://" + userServiceName + "/user/getUserList", user, ResultData.class);
ResultData result = responseEntity.getBody();
JSONObject json = JSONObject.fromObject(result);
return json.toString();
}
/**
批量删除用户
*/
@PostMapping("delUser")
@ResponseBody
public String delUser(@RequestParam String ids) {
String param = ids;
ResponseEntity<ResultData> responseEntity = restTemplate.postForEntity("http://" + userServiceName + "/user/delUser", param, ResultData.class);
ResultData result = responseEntity.getBody();
return result.getCode() + "";
}
/**
查看用户详情
*/
@GetMapping("userDetail")
public String userDetail(@RequestParam Integer id, ModelMap map) {
User user = new User();
user.setId(id);
ResponseEntity<User> responseEntity = restTemplate.postForEntity("http://" + userServiceName + "/user/getUser", user, User.class);
user = responseEntity.getBody();
map.addAttribute("user", user);
return "user-detail";
}
/**
编辑时加载用户信息
*/
@GetMapping("editUser")
public String editUser(@RequestParam Integer id, ModelMap map) {
User user = new User();
user.setId(id);
ResponseEntity<User> responseEntity = restTemplate.postForEntity("http://" + userServiceName + "/user/getUser", user, User.class);
user = responseEntity.getBody();
map.addAttribute("user", user);
return "user-edit";
}
/**
保存用户信息
*/
@PostMapping("saveUser")
public String saveUser(@ModelAttribute User user, ModelMap map) {
ResponseEntity<ResultData> responseEntity = restTemplate.postForEntity("http://" + userServiceName + "/user/register", user, ResultData.class);
ResultData result = responseEntity.getBody();
if (result.getCode() > 0) {
return "redirect:/user/toUser";
}
map.addAttribute("message", result.getMessage());
return "user-edit";
}
}
添加 controller、service、dao、model。
在 pom 文件中加入 json 包。
net.sf.json-lib
json-lib
2.4
jdk15
在 pom 文件中配置 build 启动类。
org.springframework.boot
spring-boot-maven-plugin
com.userservice.UserServiceApplication
user-service 中的 controller、service、dao 与传统的 SpringMVC 项目一致,不再赘述,这里介绍一下几个实体。
public class Page {
private Integer id;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createDate;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateDate;
private Integer limit;
private Integer page;
private Integer offset;
// getter()、setter()
}
public class User extends Page {
private String username;
private String phone;
private String email;
private Integer status;
// getter()、setter()
}
public class ResultData {
private Integer code;
private String message;
private Integer total;
private List rows = new ArrayList();
// getter()、setter()
}