1、前提约束
2、修改net.wanho.mapper.StudentMapper.java接口
新增update方法:
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;
}
3、修改net/wanho/mapper/StudentMapper.xml文件
新增update方法对应的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>
</mapper>
4、修改net.wanho.service.StudentServiceI.java接口
新增updateStudent方法声明:
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;
}
5、修改net.wanho.service.impl.StudentServiceImpl.java
新增updateStudent方法的实现:
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);
}
}
6、修改net.wanho.controller.StudentController.java
创建updateStudent 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("msg","update ok");
}
catch(Exception e)
{
ret.put("status",100);
ret.put("msg","update error");
e.printStackTrace();
}
return ret;
}
}
7、测试
打开mysql命令行以及浏览器,具体操作如下图所示:
至此,我们完成了修改学生信息的api并做了测试。