本节操作将完成新增用户的api,并进行测试。
1、前提约束
- 安装好mysql数据库
- 数据库具有student database
- 账号root,密码zhangli【请根据实际情况确定】
- 在student database中有t_student表
- t_student表结构为 ( id int, name varchar(20)),且主键自增
2、创建net.wanho.entity.Student.java类,与t_stu对应,即实体类
package net.wanho.entity;
import java.io.Serializable;
public class Student implements Serializable {
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
3、创建net.wanho.mapper.StudentMapper.java接口。
package net.wanho.mapper;
import net.wanho.entity.Student;
public interface StudentMapper {
void add(Student student) throws Exception;
}
4、创建net/wanho/mapper/StudentMapper.xml【此文件应与上述接口同名】。
<?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>
</mapper>
5、创建net.wanho.service.StudentServiceI.java接口
package net.wanho.service;
import net.wanho.entity.Student;
public interface StudentServiceI {
void addStudent(Student student)throws Exception;
}
6、创建net.wanho.service.impl.StudentServiceImpl.java,即上述接口的实现类
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);
}
}
7、创建net.wanho.controller.StudentController.java,即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("msg","add success:"+student.getId());
}
catch(Exception e)
{
ret.put("status",100);
ret.put("msg","add error");
e.printStackTrace();
}
return ret;
}
}
8、测试1
打开浏览器输入http://localhost:8088/student/add/ali,反复执行,会看到返回的主键一直在自增。具体操作如下图所示:
测试新增学生api
也可以采用如下方式查看是否成功。
9、测试2
打开mysql命令行,查看表中数据,具体操作如下图所示:
mysql命令行查看数据是否插入
至此,我们完成了新增学生的api,并且完成了测试。