本文在上一篇文章的基础上,继续完成Mybatis的“增、删、改”的操作。

修改3.3的Mybatis映射文件,添加三个新的sql语句:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <!--        namespace 所对应的是一个接口的全限定名-->
 4 <mapper namespace="com.example.demo.mapper.IUserMapper">
 5     <select id="getUserById" resultType="com.example.demo.model.Users">
 6         SELECT id,last_name,gender,email FROM tbl_employee WHERE id = #{id}
 7     </select>
 8     <update id="updateUser">
 9         UPDATE tbl_employee set last_name=#{last_name},gender=#{gender},email=#{email}
10         where id=#{id}
11     </update>
12     
13     <delete id="delUser">
14         DELETE from tbl_employee where id=#{id}
15     </delete>
16 
17     <insert id="addUser">
18         INSERT into tbl_employee values(#{id},#{last_name},#{gender},#{email})
19     </insert>
20 </mapper>

 

修改3.4节的接口文件,增加三个方法:

 1 package com.example.demo.mapper;
 2 
 3 import com.example.demo.model.Users;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public interface IUserMapper {
 8     //查询
 9     public Users getUserById(Integer id);
10 
11     //更新
12     public boolean updateUser(Users user);
13 
14     //删除
15     public boolean delUser(Integer id);
16 
17     //新增
18     public boolean addUser(Users user);
19 }

 

 

修改第4节的Mybatis接口文件,增加45、75、105行的三个方法,对应映射文件中的更新、删除以及新增操作:

 1 @Service
 2 public class Users {
 3 
 4     @Autowired
 5     IUserMapper userMapper;
 6 
 7     public com.example.demo.model.Users getUserById(Integer id) throws IOException {
 8         return userMapper.getUserById(id);
 9     }
10 
11     public boolean updateUser(com.example.demo.model.Users user){
12         boolean b = userMapper.updateUser(user);
13         return b;
14     }
15 
16     public boolean delUser(Integer id) throws IOException {
17         boolean b = userMapper.delUser(1);
18         return b;
19     }
20 
21     public boolean addUser(com.example.demo.model.Users user) throws IOException {
22         boolean b = userMapper.addUser(user);
23         return b;
24     }
25 }

 

修改第5节的Controller中的内容,增加三个Action:

 1 package com.example.demo.controller;
 2 
 3 import com.example.demo.model.Users;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 import java.io.IOException;
10 
11 @RestController
12 public class UserController {
13 
14     @Autowired
15     com.example.demo.service.Users userService;
16 
17     @RequestMapping("/user/{id}")
18     public String getUserById(@PathVariable Integer id) throws IOException {
19 
20         com.example.demo.model.Users users = userService.getUserById(id);
21         return users.getLast_name();
22     }
23 
24     @RequestMapping("/updateuser")
25     public String updateUser() throws IOException {
26         Users user = new com.example.demo.model.Users(1,"yangasen",1,"yangasen@hotmial.com");
27         boolean b = userService.updateUser(user);
28         return b + "";
29     }
30 
31     @RequestMapping("/deluser/{id}")
32     public String delUser(@PathVariable Integer id) throws IOException {
33         boolean b = userService.delUser(id);
34         return b + "";
35     }
36 
37     @RequestMapping("/newuser")
38     public String newUser() throws IOException {
39         Users user = new com.example.demo.model.Users(1,"yangasen",1,"yangasen@hotmial.com");
40         boolean b = userService.addUser(user);
41         return b + "";
42     }
43 }

 

测试:

更新操作:使用[http://localhost:8080/updateuser]

删除操作:使用[http://localhost:8080/deluser/1]

新增操作:使用[http://localhost:8080/newuser]

 

Spring Boot + MyBatis连接MySQL数据库的“增删改查”基本操作,已经完成。

posted on 2020-09-14 15:54  Sempron2800+  阅读(144)  评论(0编辑  收藏  举报