springboot集合MySQL删除_Maven + Springboot + Eureka + MySQL + Mybatis整合实现微服务增删改查...

今天技术之家陪你一起学习Maven + Springboot + Eureka + MySQL + Mybatis整合实现微服务增删改查:

效果

39d4eb6cf4f4fbf68519f67f43df80eb.png

17f2d7fa9e1fb15d42b09cbeaeaccdbe.png

1b2d126d8bf8e761be5605499af7598f.png

改造 manage 项目

添加 config、controller、model。

eed69ea94f49a4e620745c77645ad23d.png

页面框架可任意选择,这里以 bootstrap 为例。

2c7c9a9313f397afc57ae353ef94cfae.png

在 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("$")

private String userServiceName;

@Autowired

private RestTemplate restTemplate;

@GetMapping("toUser")

public String toUser() {

return "user";

}

/**

查询用户列表

*/

@GetMapping("getUserList")

@ResponseBody

public String getUserList(@ModelAttribute User user) {

ResponseEntity 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 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 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 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 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";

}

}

改造 user-service 项目

添加 controller、service、dao、model。

7f764980af1514557601915e05550f89.png

在 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 项目一致,不再赘述,这里介绍一下几个实体。Page:封装分页用到的属性,以及公共属性。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()

}User:用户,继承 Page。public class User extends Page {

private String username;

private String phone;

private String email;

private Integer status;

// getter()、setter()

}ResultData:封装服务之间互相调用的结果。public class ResultData {

private Integer code;

private String message;

private Integer total;

private List rows = new ArrayList();

// getter()、setter()

}

技术之家希望本篇文章对你有用!!

欢迎来到技术之家,

如需转载,烦请保留本文链接和出处:http://www.jszja.com/contents/14/1438.html

您的支持将是我们前进的动力!如对本篇文章有疑问或建议,请通过本站下方邮箱联系我们,让技术之家每天进步一点点!(●'◡'●)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值