当前位置:   article > 正文

springboot项目+mysql 实现对表的增删改查(入门)_springboot 创建项目实现mysql数据表user的增删改查

springboot 创建项目实现mysql数据表user的增删改查

1.前言

新手上路,通过相关要求简单的对mysql数据库进行增删改查操作,废话不多说,直接来代码

2.配置类文件

pom.xml: 

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-jdbc</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. <version>8.0.33</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. </dependencies>

application.yml(创建,或者直接改后缀):

  1. server:
  2. port: 3308
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://本地ip:3306/userbase
  6. username: root
  7. password: 1234

3.创建实体类

  1. public class User {
  2. private int userid;
  3. private String username;
  4. private String gender;
  5. private int age;
  6. public User() {
  7. }
  8. public User(int userid, String username, String gender, int age) {
  9. this.userid = userid;
  10. this.username = username;
  11. this.gender = gender;
  12. this.age = age;
  13. }
  14. /**
  15. * 获取
  16. *
  17. * @return id
  18. */
  19. public int getUserid() {
  20. return userid;
  21. }
  22. /**
  23. * 设置
  24. *
  25. * @param id
  26. */
  27. public void setId(int id) {
  28. this.userid = id;
  29. }
  30. /**
  31. * 获取
  32. *
  33. * @return username
  34. */
  35. public String getUsername() {
  36. return username;
  37. }
  38. /**
  39. * 设置
  40. *
  41. * @param username
  42. */
  43. public void setUsername(String username) {
  44. this.username = username;
  45. }
  46. /**
  47. * 获取
  48. *
  49. * @return gender
  50. */
  51. public String getGender() {
  52. return gender;
  53. }
  54. /**
  55. * 设置
  56. *
  57. * @param gender
  58. */
  59. public void setGender(String gender) {
  60. this.gender = gender;
  61. }
  62. /**
  63. * 获取
  64. *
  65. * @return age
  66. */
  67. public int getAge() {
  68. return age;
  69. }
  70. /**
  71. * 设置
  72. *
  73. * @param age
  74. */
  75. public void setAge(int age) {
  76. this.age = age;
  77. }
  78. public String toString() {
  79. return "User{id = " + userid + ", username = " + username + ", gender = " + gender + ", age = " + age + "}";
  80. }
  81. }

4.Controller类

  1. @RestController
  2. public class UserController {
  3. @Autowired
  4. private JdbcTemplate jdbcTemplate;
  5. @GetMapping("/getusers")
  6. public List<Map<String, Object>> getUsers() {
  7. String sql = "select * from userbase.memberlist";
  8. return jdbcTemplate.queryForList(sql);
  9. }
  10. @GetMapping("/getuser")
  11. public List<Map<String, Object>> getUser(@RequestParam("id1") int id1, @RequestParam("id2") int id2) {
  12. String sql = "select * from userbase.memberlist where id=? or id=?";
  13. return jdbcTemplate.queryForList(sql, id1, id2);
  14. }
  15. @PostMapping("/adduser")
  16. public String addUser(@RequestBody User user) {
  17. String sql = String.format("insert into userbase.memberlist(id,zonecode,name,nickname,age,gender,memo) values(%d,%d,'%s','%s',%d,'%s','%s')",
  18. user.getId(), user.getZonecode(), user.getName(), user.getNickname(), user.getAge(), user.getGender(), user.getMemo());
  19. jdbcTemplate.execute(sql);
  20. return "数据添加成功!";
  21. }
  22. @PutMapping("/updateuser")
  23. public String updateUser(@RequestBody User user) {
  24. String sql = String.format("update userbase.memberlist set zonecode=%d,name='%s',nickname='%s',age=%d,gender='%s',memo='%s' where id=%d",
  25. user.getZonecode(), user.getName(), user.getNickname(), user.getAge(), user.getGender(), user.getMemo(), user.getId());
  26. jdbcTemplate.execute(sql);
  27. return "数据修改成功!";
  28. }
  29. @DeleteMapping("/deleteuser")
  30. public String deleteUser(@RequestBody User user) {
  31. String sql = String.format("delete from userbase.memberlist where id=%d", user.getId());
  32. jdbcTemplate.execute(sql);
  33. return "id:" + user.getId() + " 的数据删除成功!";
  34. }
  35. }

以上相关注解自行搜索百度

5.调试

下载postman,自行百度

以上为图片,仅供参考,若要进行调试请记得的选择请求方式(GET,POST,PUT,DELETE)

6.结言

根据以上代码就可以 简单的对mysql数据库里的数据库进行操作,如果有疑问的话请在评论区下进行留言,如果觉得对你有帮助,请为这篇文章点个赞,让它能够帮助更多人

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/576597
推荐阅读
相关标签
  

闽ICP备14008679号