增删改查小案例-前后端联调

该文详细介绍了如何通过JavaSpringBoot框架,结合Mapper、Controller和Service层,实现前端通过API接口进行数据的增、删、改、查操作。示例中包括了实体类定义、Mapper接口及实现、Controller的HTTP方法处理以及Apifox工具的使用来模拟前端请求,展示了数据库交互的完整流程。
摘要由CSDN通过智能技术生成

如何实现通过前端请求访问的方式实现增删改呢?数据自备

1.创建项目

 2.基础代码(因数据库信息而变化)

实体类Member.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Member {

    private Integer id;
    private String term;
    private String username;
    private String student_number;
    private String gender;
    private String qq;
    private String phone;
    private String mail;
    private String join_time;
    private String grade_and_class;
    private String graduate_time;
    private String current_location;
    private String current_company;
    private String post;
    private String role;
    private LocalDateTime update_time;
    private Integer status;
    private LocalDateTime create_time;
    private Integer is_deleted = 0;


}

Result.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {

    private Object data;
    public static Result success(Object data){
        return new Result(data);
    }
}

MemberMapper.java接口

@Mapper
public interface MemberMapper {

   //代码

}

MemberContrller.java

@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

   //代码
}

MemberService.java(接口)

@Service
public interface MemberService {


   //代码部分
}

MemberServiceImpl.java

@Service
public class MemberServcieImpl implements MemberService {

  //代码部分



}



3.通过Apifox(相当于前端请求访)实现增删改查

1.查询所有成员的信息

mapper层:

    @Select("select *from ds_user")
    List<Member> list();

controller层:

 @GetMapping /*此处的路径为公共路径,
所以在公共类上标上注解即可:@RequestMapping("/member")*/
    public Result list(){
        log.info("查询所有成员信息");

        List<Member> memberList=memberService.list();
        return Result.success(memberList);
    }

service接口层:

    /*
        查询全部信息
         */
    List<Member>list();

impl层:

//根据ID查询成员信息
    @Override
    public List<Member> list(){
        return memberMapper.list();
    }

Apifox GET请求:

 

终端日志输出:

 

2. 根据ID删除成员信息

mapper层:

 /*
    DeleteById
     */
    @Delete("delete from  ds_user where id=#{id}")
    void deleteById(Integer id);

controller层:

 /*
    根据ID成员删除信息
     */
    @DeleteMapping("/{id}")//请求时在路径后加上要删除的id即可
    public Result delete(@PathVariable Integer id){
        log.info("根据ID删除成员信息:{}",id);
        memberService.delete(id);

        return null;
    }

service接口层:

   /*
    根据ID删除成员信息
     */
    void delete(Integer id);

impl层:

 //根据ID删除成员信息
    @Override
    public void delete(Integer id) {
        memberMapper.deleteById(id);
    }

Apifox DELETE请求:

 终端日志输出:(数据库中id=141的成员信息被删除)

3.添加成员信息

mapper层:

@Insert("insert into ds_user(term, username, student_number, gender, qq, phone, mail, join_time, grade_and_class, graduate_time, current_location, current_company, post, role, update_time, status, create_time, is_deleted)values " +
            "(#{term},#{username},#{student_number},#{gender},#{qq},#{phone},#{mail},#{join_time},#{grade_and_class},#{graduate_time},#{current_location},#{current_company},#{post},#{role},#{update_time},#{status},#{create_time},#{is_deleted})")
    void insert(Member member);

controller层:

    @PostMapping
    public Result add(@RequestBody Member member){
        log.info("添加成员信息:{}",member);
        memberService.add(member);
        return Result.success(member);
    }

 service接口层:

    /*
    新增成员信息
     */
    void add(Member member);

impl层:

    //添加成员信息
    @Override
    public void add(Member member) {
        memberMapper.insert(member);
    }

Apifox POST请求:(通过json格式传入参数值)

终端输出:

添加成功:

 

4.更新成员信息

mapper层:

   @Update("update ds_user" +
            " set  term=#{term},username=#{username},student_number=#{student_number},gender=#{gender}," +
            "qq=#{qq},phone=#{phone},mail=#{mail},join_time=#{join_time},grade_and_class=#{grade_and_class}," +
            "graduate_time=#{graduate_time},current_location=#{current_location},current_company=#{current_company}," +
            "post=#{post},role=#{role},update_time=#{update_time},status=#{status},create_time=#{create_time}," +
            "is_deleted=#{is_deleted} " +
            "where id=#{id}")
    void update(Member member);

controller层:

    @PutMapping
    public Result update(@RequestBody Member member){
        log.info("修改成员信息:{}",member);
        memberService.update(member);
        return Result.success(member);
    }

 service接口层:

    /*
    修改成员信息
     */
    void update(Member member);

impl层:

    /*
   修改成员信息
    */
    @Override
    public void update(Member member) {
        memberMapper.update(member);
    }

Apifox PUT请求:

 终端日志输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值