SpringBoot--列表更新功能

1、IDEA进行相关的配置,并且创建SpringBoot项目,目录如下:
在这里插入图片描述
2、domain中创建实体类,用于和数据库中的数据表关联起来,User。

package com.example.demo.domain;

public class User {
    // 对应数据表user中的各字段
    private String uuid;
    private String username;
    private String gender;
    private int age;
    private String phone;
    private String address;
    private String email;
    private float salary;

    // 鼠标右键选择generate,接着选择setter and getter,选中所有字段,然后点击Ok,便会自动为我们自动生成get和set方法。
    public String getUuid() {
        return uuid;
    }
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public float getSalary() {
        return salary;
    }
    public void setSalary(float salary) {
        this.salary = salary;
    }
}

3、mapper中创建接口(interface),如:UserMapper。

package com.example.demo.mapper;

import com.example.demo.domain.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface UserMapper {
    // 与数据库相关的操作,通过注解来实现
    // 更新功能
    @Update("update user set username=#{username},gender=#{gender},age=#{age},phone=#{phone},address=#{address},email=#{email},salary=#{salary} where uuid=#{uuid}")
    public void update(User user);
}

4、service中创建服务(对应着mapper中的接口),如:UserService。

package com.example.demo.service;

import com.example.demo.domain.User;
import com.github.pagehelper.PageInfo;
import java.util.List;

public interface UserService {
    // 改
    void update(User user);
}

5、service-impl中创建接口服务的实现(对应着service中的接口),如:UserServiceImpl。

package com.example.demo.service.impl;

import com.example.demo.domain.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Resource  // 接口只能有一个实现类(@Resource是JDK提供的,通过name名字查找;而Autowired是Spring提供的,通过type类型查找。)
    private UserMapper userMapper;

    // 鼠标放在public class UserServiceImpl implements UserService的红线处,点击implement methods,全部选中,则创建好了所有的方法
    // 更新
    @Override
    public void update(User user) {
        userMapper.update(user);
    }
}

6、controller中创建控制器,如:UserController。

package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import netscape.javascript.JSObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

@Controller  // ①返回JSON或XML数据;②页面模板
@RequestMapping("/user")  // 这个类的路径,定义为/user
public class UserController {
    @Resource(name = "UserService")
    private UserService csi;

    // 添加功能
    @RequestMapping("/save")  // 可用于Get或Post请求
    @ResponseBody  // 将java对象转为json格式的数据

    // 更新功能
    @PostMapping("/update")  // 只能用于Post请求
    @ResponseBody
    public void update(User user){
        csi.update(user);
    }
}

相关链接:
IntelliJ IDEA中构建Spring Boot的项目
Spring Boot中的配置文件(application.properties、application.yml与pom.xml)
ORM、JPA、Spring Data JPA和常用的五种访问数据库方式

SpringBoot–页面显示列表功能
SpringBoot–列表添加详情功能
SpringBoot–列表添加新增功能
SpringBoot–列表更新功能
SpringBoot–列表删除功能

SpringBoot的控制层注解(@Controller与@RestController)
SpringBoot的业务层注解(@Resource、@Autowired与@Qualifier)
SpringBoot的映射(@RequestMapping、@GetMapping与@PostMapping)
SpringBoot的函数参数(JavaBean对象、@RequestParam与@RequestBody)
SpringBoot的返回数据(@ResponseBody、返回templates下的网页 与 返回公共网页)

利用postman完成JSON串的发送功能(springboot)
利用postman完成数据的分页查询功能(springboot)
利用postman完成向数据库中添加数据的功能(springboot)

Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
Preparing: insert into user(uuid,username,gender,age,phone,address) values(?,?,?,?,?,?,?)
Could not autowire. No beans of ‘UserMapper‘ type found.
Error starting ApplicationContext. To display the conditions report re-run your application with
Loading class com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is com.mysql.cj.jdb
Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
This application has no explicit mapping for /error, so you are seeing this as a fallback.
BootStrap-SpringBoot的使用过程中前端table表格无法加载出数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕斯-ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值