之前我们的项目足够简单,所以使用的是两层架构,现在为了学习Spring,需要使用行业中常见的三层架构,关于分层开发的原则请看下图:
本次对项目的调整,主要是由之前的controller调用dao,改成controller调用service,service调用dao。项目结构:
涉及修改的代码:
StudentController.java:
package controller;
import dao.BanJiDao;
import dao.MybatisSqlSession;
import dao.StudentDao;
import entity.BanJi;
import entity.Student;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.BanJiService;
import service.StudentService;
import service.impl.BanJiServiceImpl;
import service.impl.StudentServiceImpl;
import java.util.List;
@Controller
public class StudentController {
@RequestMapping("show")
public ModelAndView search() {
StudentService stuService =new StudentServiceImpl();
List<Student> list = stuService.searchAll();
ModelAndView mv = new ModelAndView("show");
mv.addObject("stus", list);
return mv;
}
@RequestMapping("showAdd")
public ModelAndView showAdd() {
BanJiService bjService=new BanJiServiceImpl();
List<BanJi> bjList=bjService.searchAll();
ModelAndView mv = new ModelAndView("showAdd");
mv.addObject("bjs",bjList);
return mv;
}
@RequestMapping("add")
public String add(Student stu) {
StudentService stuService =new StudentServiceImpl();
boolean flag = stuService.add(stu);
if (flag) {
return "redirect:show.do";
}
return null;
}
@RequestMapping("showUpdate")
public ModelAndView showUpdate(int id) {
StudentService stuService =new StudentServiceImpl();
Student stu = stuService.search(id);
ModelAndView mv = new ModelAndView("showUpdate");
mv.addObject("stu", stu);
return mv;
}
@RequestMapping("update")
public String update(Student stu) {
StudentService stuService =new StudentServiceImpl();
boolean flag = stuService.update(stu);
if (flag) {
return "redirect:show.do";
}
return null;
}
@RequestMapping("delete")
public String delete(int id) {
StudentService stuService =new StudentServiceImpl();
boolean flag = stuService.delete(id);
if (flag) {
return "redirect:show.do";
}
return null;
}
}
StudentService.java:
package service;
import entity.Student;
import java.util.List;
public interface StudentService {
List<Student> searchAll();
Student search(int id);
boolean add(Student stu);
boolean update(Student stu);
boolean delete(int id);
}
StudentServiceImpl.java:
package service.impl;
import dao.MybatisSqlSession;
import dao.StudentDao;
import entity.Student;
import org.apache.ibatis.session.SqlSession;
import service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
@Override
public List<Student> searchAll() {
SqlSession sqlSession = MybatisSqlSession.getSqlSession();
StudentDao stuDao = sqlSession.getMapper(StudentDao.class);
List<Student> list=stuDao.searchAll();
sqlSession.commit();
sqlSession.close();
return list;
}
@Override
public Student search(int id) {
SqlSession sqlSession = MybatisSqlSession.getSqlSession();
StudentDao stuDao = sqlSession.getMapper(StudentDao.class);
Student stu = stuDao.search(id);
sqlSession.commit();
sqlSession.close();
return stu;
}
@Override
public boolean add(Student stu) {
SqlSession sqlSession = MybatisSqlSession.getSqlSession();
StudentDao stuDao = sqlSession.getMapper(StudentDao.class);
int rs = stuDao.add(stu);
sqlSession.commit();
sqlSession.close();
return rs>0;
}
@Override
public boolean update(Student stu) {
SqlSession sqlSession = MybatisSqlSession.getSqlSession();
StudentDao stuDao = sqlSession.getMapper(StudentDao.class);
int rs = stuDao.update(stu);
sqlSession.commit();
sqlSession.close();
return rs>0;
}
@Override
public boolean delete(int id) {
SqlSession sqlSession = MybatisSqlSession.getSqlSession();
StudentDao stuDao = sqlSession.getMapper(StudentDao.class);
int rs = stuDao.delete(id);
sqlSession.commit();
sqlSession.close();
return rs>0; }
}