MVC分层模式
MVC是软件开发的一种软件架构模式,把软件系统分为三部分:模型、视图、控制
1、MVC把一个应用的输入,处理,输出按照Model(模型),View(视图),Controller(控制)三层进行分离
(1)Model(模型):数据模型,提供要展示的数据,包含数据和行为,一般分离为数据对象和服务层
(2)View(视图):负责模型的展示,一般就是我们见到的用户界面
(3)Controller(控制):接收用户请求,传给模型处理,把模型处理后的数据给视图,起到控制调度的作用
2、MVC分层模式的优点:
-
(1)高内聚低耦合
-
(2)重用性高
-
(3)部署快
-
(4)可维护性高
-
(5)有利于软件工程化管理
3、MVC分层模式的文档结构:
(1)Model层:包含实体层、Dao层、Dao实现层、Service层、Service实现层、工具类层
分别位于不同的包中:
-
com.hrxy.entity
-
com.hrxy.dao
-
com.hrxy.dao.impl
-
com.hrxy.service
-
com.hrxy.service.impl
-
com.hrxy.util
-
com.hrxy.test
entity:实体层,放置一个个实体,及其相应的set、get方法。如果想要对数据库进行一些操作(比如说读取)的话,就要先写entity层。
dao:Dao是数据访问层,Dao的作用是封装对数据库的访问:增删改查,不涉及业务逻辑,只是达到按某个条件获得指定数据的要求。
service:业务逻辑层。它处理逻辑上的业务,而不去考虑具体的实现。至于为什么service层要使用接口来定义有以下几点好处:在java中接口是多继承的,而类是单继承的,如果你需要一个类实现多个service,你用接口可以实现,用类定义service就没那么灵活。
util:工具包
(2)View层:
(3)Controller层:
接下来我们以部门表为例进行讲解:
4、编写Entity层
package _15MVC.d01.cn.com.entity; /**部门实体 * @author LX * @create 2023-12-24 20:22 */ public class Departement { private int dep_no; private String deo_name; private String dep_loc; public Departement() { } public Departement(int dep_no, String deo_name, String dep_loc) { this.dep_no = dep_no; this.deo_name = deo_name; this.dep_loc = dep_loc; } public int getDep_no() { return dep_no; } public void setDep_no(int dep_no) { this.dep_no = dep_no; } public String getDeo_name() { return deo_name; } public void setDeo_name(String deo_name) { this.deo_name = deo_name; } public String getDep_loc() { return dep_loc; } public void setDep_loc(String dep_loc) { this.dep_loc = dep_loc; } }
5、编写Dao层接口和BaseDao
package _15MVC.d01.cn.com.dao; import java.sql.*; /**所有Dao层的父类:BaseDao * @author LX * @create 2023-12-24 20:26 */ public class BaseDao { protected Connection con; protected PreparedStatement pst; protected ResultSet rs; //打开连接 public void openCon() { try { Class.forName("com.mysql.jdbc.Driver"); con= DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql-03", "root","root"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } } //关闭资源 public void closeAll(){ try { if (rs!=null) {//防止空指针异常 rs.close(); } if (pst!=null) { pst.close(); } if (con!=null) { con.close(); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }
package _15MVC.d01.cn.com.dao; import _15MVC.d01.cn.com.entity.Departement; import java.util.List; /**部门表的Dao层接口,数据访问层 * @author LX * @create 2023-12-24 20:42 */ public interface DepartmentDao { public int insert(Departement dept) ; public int delete(Departement dept) ; public int update(Departement dept) ; public List<Departement> selectAll() ; public Departement selectById(Departement dept); }
6、编写Daod实现层impl
package _15MVC.d01.cn.com.dao.impl; import _15MVC.d01.cn.com.dao.BaseDao; import _15MVC.d01.cn.com.dao.DepartmentDao; import _15MVC.d01.cn.com.entity.Departement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /**部门表Dao层接口的实现类: * 继承BaseDao,实现与数据库相连 * 实现部门接口,实现方法,一个方法对应一个SQL语句 * @author LX * @create 2023-12-24 20:44 */ public class DepartmentDaoImpl extends BaseDao implements DepartmentDao { //增加的方法实现 public int insert(Departement dept) { int i=-1;//代表出现异常 openCon(); String sql="insert into department values(?,?,?)"; try { pst=con.prepareStatement(sql); pst.setInt(1,dept.getDep_no()); pst.setString(2,dept.getDeo_name()); pst.setString(3,dept.getDep_loc()); i=pst.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally {//无论如何都要关闭资源 closeAll(); } return i; } //删除的方法实现 public int delete(Departement dept) { int i=-1; openCon(); String sql="delete from department where dep_no=?"; try { pst=con.prepareStatement(sql); pst.setInt(1,dept.getDep_no()); i=pst.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { closeAll(); } return i; } //修改的方法实现 public int update(Departement dept) { int i=-1; openCon(); String sql="update department set dep_name=? where dep_no=?"; return 0; } //查询全部的方法实现 public List<Departement> selectAll(){ List<Departement> list=null; openCon(); String sql="select * from department"; try { pst=con.prepareStatement(sql); rs=pst.executeQuery(); list=new ArrayList<Departement>(); while (rs.next()){ Departement dept=new Departement(); dept.setDep_no(rs.getInt("dep_no")); dept.setDeo_name(rs.getString("dep_name")); dept.setDep_loc(rs.getString("dep_loc")); list.add(dept); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { closeAll(); } return list; } //通过id查询的方法实现 public Departement selectById(Departement dept) { Departement d=null; openCon(); String sql="select * from department where dep_no=?"; try { pst=con.prepareStatement(sql); pst.setInt(1,dept.getDep_no()); rs=pst.executeQuery(); while (rs.next()){//只返回一行数据也要接收在rs中进行判断 d=new Departement(); d.setDep_no(rs.getInt("dep_no")); d.setDeo_name(rs.getString("dep_name")); d.setDep_loc(rs.getString("dep_loc")); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { closeAll(); } return d; } }
7、编写service层接口
package _15MVC.d01.cn.com.service; import _15MVC.d01.cn.com.entity.Departement; import java.util.List; /**部门业务层,逻辑业务层 * @author LX * @create 2023-12-24 22:17 */ public interface DepartmentService { public boolean add(Departement departement) ;//save public boolean remove(Departement departement);//drop public boolean modify(Departement departement);//alter public List<Departement> findAll();// public Departement findById(Departement departement);// }
8、编写service接口的实现类impl层
package _15MVC.d01.cn.com.service.impl; import _15MVC.d01.cn.com.dao.DepartmentDao; import _15MVC.d01.cn.com.dao.impl.DepartmentDaoImpl; import _15MVC.d01.cn.com.entity.Departement; import _15MVC.d01.cn.com.service.DepartmentService; import java.util.List; /**service接口的实现类 * @author LX * @create 2023-12-24 22:25 */ public class DepartmentServiceImpl implements DepartmentService { //上调下,私有化 private DepartmentDao departmentDao=new DepartmentDaoImpl(); public boolean add(Departement departement) { /*int i=departmentDao.insert(departement); if (i>0){ return true; }else{ return false; }*/ /*boolean f=departmentDao.insert(departement)>0?true:false; return f;*/ return departmentDao.insert(departement)>0; } public boolean remove(Departement departement) { return departmentDao.delete(departement)>0; } public boolean modify(Departement departement) { return departmentDao.update(departement)>0; } public List<Departement> findAll() { return departmentDao.selectAll(); } public Departement findById(Departement departement) { return departmentDao.selectById(departement); } }
9、编写测试类
public class Test { public static void main(String[] args) { DepartmentService departmentService=new DepartmentServiceImpl(); Departement departement=null; /* System.out.println("-----------增加-----------"); departement=new Departement(7,"开发部","222"); if (departmentService.add(departement)) { System.out.println("添加成功!"); }else { System.out.println("添加失败!"); }*/ /*System.out.println("-----------删除-----------"); departement=new Departement(7,"开发部","222"); if (departmentService.remove(departement)) { System.out.println("删除成功!"); }else { System.out.println("删除失败!"); }*/ /*System.out.println("-----------修改-----------"); departement=new Departement(6,"运维部","223"); if (departmentService.modify(departement)) { System.out.println("添加成功!"); }else { System.out.println("添加失败!"); }*/ System.out.println("-----------查询全部-----------"); List<Departement> list=departmentService.findAll(); for (Departement d:list) { System.out.println(d.getDep_no()+"\t"+d.getDeo_name()+"\t"+d.getDep_loc()); } System.out.println("-----------通过id查询-----------"); departement=new Departement(6,"运维部","223"); departement=departmentService.findById(departement); System.out.println(departement.getDep_no()+"\t"+departement.getDeo_name()+"\t"+departement.getDep_loc()); } }