characterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingutf-8forceRequestEncodingtrueforceResponseEncodingtruecharacterEncodingFilter
int insertStudent(Student student);
List selectStudents();
}
mapper文件
insert into student(name, email, age)
values (#{name}, #{email}, #{age})
service包
StudentService接口
int addStudent(Student student);
List findStudents();
实现类
package com.liar.service.impl;
import com.liar.dao.StudentDao;
import com.liar.entity.Student;
import com.liar.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
return studentDao.insertStudent(student);
}
@Override
public List findStudents() {
return studentDao.selectStudents();
}
}
controller包
package com.liar.controller;
import com.liar.entity.Student;
import com.liar.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/student")
public class MyController {
@Resource
private StudentService service;
@RequestMapping(value = "/addStudent.do")
public ModelAndView addStudent(Student student){
ModelAndView mv = new ModelAndView();
String message = "注册失败!";
//调用service
int nums = service.addStudent(student);
if(nums > 0){
//注册成功
message = student.getName() + "注册成功!";
}
//添加数据
mv.addObject("message",message);
//指定结果页面
mv.setViewName("result");
return mv;
}
@RequestMapping(value = "/queryStudent.do")
@ResponseBody
public List queryStudent(Student student){
//参数的检查,简单的数据处理
List students = service.findStudents();
return students;
}
}