• SpringBoot之整合Mybatis(增,改,删)


    一,在上一篇文章SpringBoot之整合Mybatis中,我们使用spring boot整合了Mybatis,并演示了查询操作。接下来我们将完善这个示例,增加增,删,改的功能。

    二,改动代码

      1.修改UserMapper.xml,修改之后的代码:

        

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.zcz.dao.UserDao">
        
        <sql id="base_column_list">
            id,name
        </sql>
        <select id="findAllUser" resultType="com.zcz.entity.User">
            select id,name from user
        </select>
         <select id="findUserById" resultType="com.zcz.entity.User">
             select 
            <include refid="base_column_list"></include>
            from user
            where id = #{id}
         </select>
         <update id="updateUserById" parameterType="com.zcz.entity.User">
             update user set name = #{name} where id = #{id}
         </update>
         <insert id="insertUser" parameterType="com.zcz.entity.User">
             insert into user(name) values (#{name})
         </insert>
         <delete id="deleteById" parameterType="java.lang.Integer">
             delete from user where id = #{id}
         </delete>
    </mapper>

      2.修改UserDao ,修改后的代码:

        

    package com.zcz.dao;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    
    import com.zcz.entity.User;
    
    @Mapper
    public interface UserDao {
        public List<User> findAllUser();
        public User findUserById(int id);
        public boolean updateUserById(User user);
        public boolean insertUser(User user);
        public boolean deleteById(int id);
    }

      3.修改UserService和UserServiceImpl

      UserService代码如下:

        

    package com.zcz.service;
    
    import java.util.List;
    
    import com.zcz.entity.User;
    
    public interface UserService {
        public List<User> getUsers();
        public User findUserById(int id);
        public boolean updateUserById(User user);
        public boolean insertUser(User user);
        public boolean deleteById(int id);
    }

      UserServiceImpl代码如下:

        

    package com.zcz.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.zcz.dao.UserDao;
    import com.zcz.entity.User;
    import com.zcz.service.UserService;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDao userDao;
        
        @Override
        public List<User> getUsers() {
            // TODO Auto-generated method stub
            return userDao.findAllUser();
        }
    
        @Override
        public User findUserById(int id) {
            // TODO Auto-generated method stub
            return userDao.findUserById(id);
        }
    
        @Override
        public boolean updateUserById(User user) {
            // TODO Auto-generated method stub
            userDao.updateUserById(user);
            return true;
        }
    
        @Override
        public boolean insertUser(User user) {
            // TODO Auto-generated method stub
            userDao.insertUser(user);
            return true;
        }
    
        @Override
        public boolean deleteById(int id) {
            // TODO Auto-generated method stub
            userDao.deleteById(id);
            return true;
        }
    
    }

    4.修改UserController.代码如下

      

    package com.zcz.controller;
    
    import java.util.List;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.zcz.entity.User;
    import com.zcz.service.UserService;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        
        @Autowired
        private UserService userService;
        
        @RequestMapping("/getUser")
        public List<User> getUsers(){
            return userService.getUsers();
        }
        
        @RequestMapping("/addUser")
        public void addUser(HttpServletRequest request) {
            String name = request.getParameter("name");
            User user = new User();
            user.setName(name);
            userService.insertUser(user);
        }
        @RequestMapping("/updateUser")
        public void updateUser(HttpServletRequest request) {
            String name = request.getParameter("name");
            int id = Integer.parseInt(request.getParameter("id"));
            User user = new User();
            user.setName(name);
            user.setId(id);
            userService.updateUserById(user);
        }
        
        @RequestMapping("/getUserOne")
        public User getUserOne(HttpServletRequest request) {
            int id = Integer.parseInt(request.getParameter("id"));
            return userService.findUserById(id);
        }
        @RequestMapping("/deleteUser")
        public void deleteUser(HttpServletRequest request) {
            int id = Integer.parseInt(request.getParameter("id"));
            userService.deleteById(id);
        }
    }

    三,接下来就是测试

      首先看一下数据库的数据:

        

      1,测试新增,访问:http://localhost:8080/user/addUser?name=zhangchengzi

        

      成功

      2,测试更新,访问:http://localhost:8080/user/updateUser?id=6&name=zhangchengzilala

        

      成功

      3.测试更具id查询,访问:http://localhost:8080/user/getUserOne?id=6

        

      成功

      4,测试删除,访问:http://localhost:8080/user/deleteUser?id=6

        

      成功

       5.我把源代码上传到了github仓库:https://github.com/ZCC1/learnSpringBootWithMybatis2.git。可以clone下来参考。欢迎star。

    好了,这样以来,Spring Boot整合Mybatis,常用的操作,增,删,改,查全部都实现了。

      


    原创不易,转载请注明出处:https://www.cnblogs.com/zhangchengzi/p/9662639.html

    更多干货,请查阅目录
    才疏学浅,如有错误,欢迎大家留言评论。
    如果大家觉得我的文章写的不错,“关注我”或者“推荐一下”吧,我会继续努力写出更好的文章。
    觉得哪里写的不好的,可以留言或者加我微信告诉我。
    另外我建立一个技术讨论群,欢迎大家来学习探讨,微信:fcg606808

  • 相关阅读:
    Mac配置docker阿里云加速器
    Docker初学笔记
    Mac下载安装Tomcat
    MySQL
    monkey
    Git基本使用
    yaml语法
    PAT_B数素数 (20)
    PAT_B1002数字分类
    PAT基础编程练习
  • 原文地址:https://www.cnblogs.com/zhangchengzi/p/9662639.html
Copyright © 2020-2023  润新知