1、前提约束
2、技术选型
- springmvc+spring+mybatis
- pagehelper
3、修改net.wanho.mapper.StudentMapper.java接口
新增query方法申明:
package net.wanho.mapper;
import net.wanho.entity.Student;
import java.util.List;
public interface StudentMapper {
void add(Student student) throws Exception;
void update(Student student) throws Exception;
void delete(int id) throws Exception;
void get(int id) throws Exception;
List<Student> query() throws Exception;
}
4、修改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>
<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 >
<select id="get" parameterType="int" resultType="net.wanho.entity.Student">
select * from t_student where id=#{id}
</select >
<select id="query" resultType="net.wanho.entity.Student">
select * from t_student
</select >
</mapper>
5、修改net.wanho.service.StudentServiceI.java接口
新增queryStudents方法申明:
package net.wanho.service;
import net.wanho.entity.Student;
import java.util.List;
public interface StudentServiceI {
void addStudent(Student student)throws Exception;
void updateStudent(Student student)throws Exception;
void deleteStudent(int id)throws Exception;
Student getStudent(int id)throws Exception;
List<Student> queryStudents()throws Exception;
}
6、修改net.wanho.service.impl.StudentServiceImpl.java
新增queryStudents方法的实现:
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;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentServiceI {
//注入StudentMapper
@Resource
private StudentMapper studentMapper;
public void addStudent(Student student) throws Exception {
studentMapper.add(student);
}
public void updateStudent(Student student) throws Exception {
studentMapper.update(student);
}
public void deleteStudent(int id) throws Exception {
studentMapper.delete(id);
}
public Student getStudent(int id) throws Exception {
return studentMapper.get(id);
}
public List<Student> queryStudents() throws Exception {
return studentMapper.query();
}
}
7、修改net.wanho.controller.StudentController.java
创建queryStudents API入口:
package net.wanho.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import net.wanho.entity.Student;
import net.wanho.service.StudentServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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;
import java.util.List;
@Controller
public class StudentController {
@Resource
private StudentServiceI studentService;
@RequestMapping(value="/student/add",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;
}
@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;
}
@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("msg","delete ok");
}
catch(Exception e)
{
ret.put("status",100);
ret.put("msg","delete error");
e.printStackTrace();
}
return ret;
}
@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/query",method = RequestMethod.GET)
@ResponseBody
public JSONObject queryStudents(int pageNum,int pageSize)
{
JSONObject ret = new JSONObject();
try {
PageHelper.startPage(pageNum,pageSize);
List<Student> students = studentService.queryStudents();
PageInfo<Student> pageInfo = new PageInfo<Student>(students);
ret.put("status",200);
ret.put("data",pageInfo);
}
catch(Exception e)
{
ret.put("status",100);
ret.put("msg","query error");
e.printStackTrace();
}
return ret;
}
}
8、测试
打开mysql命令行以及浏览器,具体操作如下图所示:
至此,我们完成了分页查询用户信息的api,并做了测试。