SSM框架实战详细教程(十四)贯穿项目实战之三层架构

        之前我们的项目足够简单,所以使用的是两层架构,现在为了学习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;    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值