完成删除学生信息api 10

1、前提约束

2、修改net.wanho.mapper.StudentMapper.java接口

新增delete方法申明:

package net.wanho.mapper;

import net.wanho.entity.Student;

public interface StudentMapper {

    void add(Student student) throws Exception;
    void get(int id) throws Exception;
    void update(Student student) throws Exception;
    void delete(int id) throws Exception;
}

3、修改net/wanho/mapper/StudentMapper.xml文件

新增delete方法对应的sql:

<?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="net.wanho.mapper.StudentMapper">
    <!--id的值与接口中方法名同名,parameterType即方法的参数类型,
        useGeneratedKeys即使用自增主键,keyProperty定义了主键-->
    <insert id="add" parameterType="net.wanho.entity.Student" useGeneratedKeys="true" keyProperty="id">
        insert into t_student(name) values(#{name})
    </insert>
    <select id="get" parameterType="int" resultType="net.wanho.entity.Student">
        select * from t_student where id=#{id}
    </select >
    <update id="update" parameterType="net.wanho.entity.Student" >
        update t_student set name=#{name} where id=#{id}
    </update>
    <delete id="delete" parameterType="int" >
        delete from t_student where id=#{id}
    </delete >
</mapper>

4、修改net.wanho.service.StudentServiceI.java接口

新增deleteStudent方法申明:

package net.wanho.service;

import net.wanho.entity.Student;

public interface StudentServiceI {
    void addStudent(Student student)throws Exception;
    Student getStudent(int id)throws Exception;
    void updateStudent(Student student)throws Exception;
    void deleteStudent(int id)throws Exception;
}

5、修改net.wanho.service.impl.StudentServiceImpl.java

新增deleteStudent方法的实现:

package net.wanho.service.impl;

import net.wanho.entity.Student;
import net.wanho.mapper.StudentMapper;
import net.wanho.service.StudentServiceI;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentServiceI {

    //注入StudentMapper
    @Resource
    private StudentMapper studentMapper;

    public void addStudent(Student student) throws Exception {
        studentMapper.add(student);
    }
    public Student getStudent(int id) throws Exception {
        return studentMapper.get(id);
    }
    public void updateStudent(Student student) throws Exception {
        studentMapper.update(student);
    }
    public void deleteStudent(int id) throws Exception {
        studentMapper.delete(id);
    }
}

6、修改net.wanho.controller.StudentController.java

创建deleteStudent API入口:

package net.wanho.controller;

import com.alibaba.fastjson.JSONObject;
import net.wanho.entity.Student;
import net.wanho.service.StudentServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class StudentController {

    @Resource
    private StudentServiceI studentService;

    @RequestMapping(value="/student/add/{name}",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject addStudent(Student student)
    {
        JSONObject ret = new JSONObject();
        try {
            studentService.addStudent(student);
            ret.put("status",200);
            ret.put("status",200);
            ret.put("msg","add success:"+student.getId());
        }
        catch(Exception e)
        {
            ret.put("status",100);
            ret.put("msg","add error");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 以url带参的方式传id到后台,需要使用PathVariable获取
     * @param id
     * @return
     */
    @RequestMapping(value="/student/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getStudent(@PathVariable("id") int id)
    {
        JSONObject ret = new JSONObject();
        try {
            Student student = studentService.getStudent(id);
            ret.put("status",200);
            ret.put("data",student);
        }
        catch(Exception e)
        {
            ret.put("status",100);
            ret.put("msg","get error");
            e.printStackTrace();
        }
        return ret;
    }

    @RequestMapping(value="/student/update",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject updateStudent(Student student)
    {
        JSONObject ret = new JSONObject();
        try {
            studentService.updateStudent(student);
            ret.put("status",200);
            ret.put("status",200);
        }
        catch(Exception e)
        {
            ret.put("status",100);
            ret.put("msg","add error");
            e.printStackTrace();
        }
        return ret;
    }

    @RequestMapping(value="/student/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject deleteStudent(@PathVariable("id") int id)
    {
        JSONObject ret = new JSONObject();
        try {
            studentService.deleteStudent(id);
            ret.put("status",200);
            ret.put("status",200);
        }
        catch(Exception e)
        {
            ret.put("status",100);
            ret.put("msg","add error");
            e.printStackTrace();
        }
        return ret;
    }
}

7、测试

打开mysql命令行以及浏览器,具体操作如下图所示:
测试删除学生信息
至此,我们完成删除学生信息的api以及测试。

猜你喜欢

转载自www.cnblogs.com/alichengxuyuan/p/12581702.html
10