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方法對應(yīng)的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即方法的參數(shù)類型两踏,
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方法的實(shí)現(xiàn):
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
創(chuàng)建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命令行以及瀏覽器泛粹,具體操作如下圖所示:
至此,我們完成了修改學(xué)生信息的api并做了測試肮疗。