赞
踩
新手上路,通过相关要求简单的对mysql数据库进行增删改查操作,废话不多说,直接来代码
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.33</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- server:
- port: 3308
-
- spring:
- datasource:
- url: jdbc:mysql://本地ip:3306/userbase
- username: root
- password: 1234
-
-
-
- public class User {
- private int userid;
- private String username;
- private String gender;
- private int age;
-
-
- public User() {
-
- }
-
- public User(int userid, String username, String gender, int age) {
- this.userid = userid;
- this.username = username;
- this.gender = gender;
- this.age = age;
- }
-
- /**
- * 获取
- *
- * @return id
- */
- public int getUserid() {
- return userid;
- }
-
- /**
- * 设置
- *
- * @param id
- */
- public void setId(int id) {
- this.userid = id;
- }
-
- /**
- * 获取
- *
- * @return username
- */
- public String getUsername() {
- return username;
- }
-
- /**
- * 设置
- *
- * @param username
- */
- public void setUsername(String username) {
- this.username = username;
- }
-
- /**
- * 获取
- *
- * @return gender
- */
- public String getGender() {
- return gender;
- }
-
- /**
- * 设置
- *
- * @param gender
- */
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- /**
- * 获取
- *
- * @return age
- */
- public int getAge() {
- return age;
- }
-
- /**
- * 设置
- *
- * @param age
- */
- public void setAge(int age) {
- this.age = age;
- }
-
- public String toString() {
- return "User{id = " + userid + ", username = " + username + ", gender = " + gender + ", age = " + age + "}";
- }
-
-
-
- }
-
- @RestController
- public class UserController {
- @Autowired
- private JdbcTemplate jdbcTemplate;
-
- @GetMapping("/getusers")
- public List<Map<String, Object>> getUsers() {
- String sql = "select * from userbase.memberlist";
- return jdbcTemplate.queryForList(sql);
- }
-
- @GetMapping("/getuser")
- public List<Map<String, Object>> getUser(@RequestParam("id1") int id1, @RequestParam("id2") int id2) {
- String sql = "select * from userbase.memberlist where id=? or id=?";
- return jdbcTemplate.queryForList(sql, id1, id2);
- }
-
- @PostMapping("/adduser")
- public String addUser(@RequestBody User user) {
- String sql = String.format("insert into userbase.memberlist(id,zonecode,name,nickname,age,gender,memo) values(%d,%d,'%s','%s',%d,'%s','%s')",
- user.getId(), user.getZonecode(), user.getName(), user.getNickname(), user.getAge(), user.getGender(), user.getMemo());
- jdbcTemplate.execute(sql);
- return "数据添加成功!";
- }
-
-
- @PutMapping("/updateuser")
- public String updateUser(@RequestBody User user) {
- String sql = String.format("update userbase.memberlist set zonecode=%d,name='%s',nickname='%s',age=%d,gender='%s',memo='%s' where id=%d",
- user.getZonecode(), user.getName(), user.getNickname(), user.getAge(), user.getGender(), user.getMemo(), user.getId());
- jdbcTemplate.execute(sql);
- return "数据修改成功!";
- }
-
-
- @DeleteMapping("/deleteuser")
- public String deleteUser(@RequestBody User user) {
- String sql = String.format("delete from userbase.memberlist where id=%d", user.getId());
- jdbcTemplate.execute(sql);
- return "id:" + user.getId() + " 的数据删除成功!";
- }
-
-
- }
以上相关注解自行搜索百度
下载postman,自行百度
以上为图片,仅供参考,若要进行调试请记得的选择请求方式(GET,POST,PUT,DELETE)
根据以上代码就可以 简单的对mysql数据库里的数据库进行操作,如果有疑问的话请在评论区下进行留言,如果觉得对你有帮助,请为这篇文章点个赞,让它能够帮助更多人
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。