SDU项目实训——后台搭建——SpringBoot+Vue学习(三)

SpringBoot的增删改查

1、首先写增加,在UserMapper中添加

 @Insert("INSERT into sys_user(username, password,nickname,email,phone,address) VALUES (#{username}, #{password}," +
            " #{nickname}, #{email},#{phone}, #{address})")
    int insert(User user);

    int update(User user);

在UserController中添加:

 public Integer save(@RequestBody User user) {
        // 新增或者更新
        return userService.save(user);
    }

RequestBody就是前台传入json数据后,可以把json数据映射为user对象,可以用postman(测试接口的工具)来验证。postman下载地址:

https://app.getpostman.com/app/download/win64

测试添加一行数据以及结果:
在这里插入图片描述在这里插入图片描述
2、创建一个service包,写Userservice类,通过if语句判断,如果id不存在,就添加,如果存在,就更新。

package com.example.demo.service;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public int save(User user) {
        if (user.getId() == null) {  // user没有id,则表示是新增
            return userMapper.insert(user);
        } else { // 否则为更新
            return userMapper.update(user);
        }
    }

}

3、为了实现动态SQL,写一个User.xml的配置文件,先安装mybatisX工具,为了让springboot知道这个文件,需要在application.yml中加上mybatis配置代码:

mybatis:
  mapper-locations: classpath:mapper/*.xml  #扫描所有mybatis的xml文件
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

User.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.example.demo.mapper.UserMapper">

    <update id="update">
        update sys_user
        <set>
            <if test="username != null">
                username = #{username},
            </if>
            <!--            <if test="password != null">-->
            <!--                password = #{password}-->
            <!--            </if>-->
            <if test="nickname != null">
                nickname = #{nickname},
            </if>
            <if test="email != null">
                email = #{email},
            </if>
            <if test="phone != null">
                phone = #{phone},
            </if>
            <if test="address != null">
                address = #{address}
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

</mapper>

4、完善增删改查的代码,在Usercontroller中添加delete、index完成删除、查询功能。Usercontroller中的代码:

package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserMapper userMapper;

    @Autowired
   private UserService userService;

    // 新增和修改
    @PostMapping
    public Integer save(@RequestBody User user) {
        // 新增或者更新
       return userService.save(user);
    }

    // 查询所有数据
    @GetMapping
    public List<User> index() {
        List<User> all = userMapper.findAll();
        return all;
    }

    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id) {
        return userMapper.deleteById(id);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值