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

2018-09-18 06:39:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

一,在上一篇文章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

 更多文章,请查看目录

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java.util.ConcurrentModificationException异常原因及解决方法

下一篇:目前(2018年)在北京java程序员平均薪水是多少呢?