站酷网官网登录腾讯云主机安装wordpress
站酷网官网登录,腾讯云主机安装wordpress,招商网站平台,织梦做的网站首页幻灯片怎么不能显示MVC分层模式MVC是软件开发的一种软件架构模式#xff0c;把软件系统分为三部分#xff1a;模型、视图、控制1、MVC把一个应用的输入#xff0c;处理#xff0c;输出按照Model(模型)#xff0c;View(视图)#xff0c;Controller(控制)三层进行分离#xff08;1#xff0…MVC分层模式MVC是软件开发的一种软件架构模式把软件系统分为三部分模型、视图、控制1、MVC把一个应用的输入处理输出按照Model(模型)View(视图)Controller(控制)三层进行分离1Model(模型)数据模型提供要展示的数据包含数据和行为一般分离为数据对象和服务层2View(视图)负责模型的展示一般就是我们见到的用户界面3Controller(控制)接收用户请求传给模型处理把模型处理后的数据给视图起到控制调度的作用2、MVC分层模式的优点1高内聚低耦合2重用性高3部署快4可维护性高5有利于软件工程化管理3、MVC分层模式的文档结构1Model层包含实体层、Dao层、Dao实现层、Service层、Service实现层、工具类层分别位于不同的包中com.hrxy.entitycom.hrxy.daocom.hrxy.dao.implcom.hrxy.servicecom.hrxy.service.implcom.hrxy.utilcom.hrxy.testentity:实体层放置一个个实体及其相应的set、get方法。如果想要对数据库进行一些操作比如说读取的话就要先写entity层。dao:Dao是数据访问层Dao的作用是封装对数据库的访问增删改查不涉及业务逻辑只是达到按某个条件获得指定数据的要求。service:业务逻辑层。它处理逻辑上的业务而不去考虑具体的实现。至于为什么service层要使用接口来定义有以下几点好处在java中接口是多继承的而类是单继承的如果你需要一个类实现多个service你用接口可以实现用类定义service就没那么灵活。util:工具包2View层:3Controller层:4、编写Entity层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层接口和BaseDaoDao层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(); } } }BaseDao层public interface DepartmentDao { public int insert(Departement dept) ; public int delete(Departement dept) ; public int update(Departement dept) ; public ListDepartement selectAll() ; public Departement selectById(Departement dept); }6、编写Dao的实现层implpublic class DepartmentDaoImpl extends BaseDao implements DepartmentDao { //增加的方法实现 public int insert(Departement dept) { int i-1;//代表出现异常 openCon(); String sqlinsert into department values(?,?,?); try { pstcon.prepareStatement(sql); pst.setInt(1,dept.getDep_no()); pst.setString(2,dept.getDeo_name()); pst.setString(3,dept.getDep_loc()); ipst.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally {//无论如何都要关闭资源 closeAll(); } return i; } //删除的方法实现 public int delete(Departement dept) { int i-1; openCon(); String sqldelete from department where dep_no?; try { pstcon.prepareStatement(sql); pst.setInt(1,dept.getDep_no()); ipst.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { closeAll(); } return i; } //修改的方法实现 public int update(Departement dept) { int i-1; openCon(); String sqlupdate department set dep_name? where dep_no?; return 0; } //查询全部的方法实现 public ListDepartement selectAll(){ ListDepartement listnull; openCon(); String sqlselect * from department; try { pstcon.prepareStatement(sql); rspst.executeQuery(); listnew ArrayListDepartement(); while (rs.next()){ Departement deptnew 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 dnull; openCon(); String sqlselect * from department where dep_no?; try { pstcon.prepareStatement(sql); pst.setInt(1,dept.getDep_no()); rspst.executeQuery(); while (rs.next()){//只返回一行数据也要接收在rs中进行判断 dnew 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层接口public interface DepartmentService { public boolean add(Departement departement) ;//save public boolean remove(Departement departement);//drop public boolean modify(Departement departement);//alter public ListDepartement findAll();// public Departement findById(Departement departement);// }8、编写service接口的实现类impl层public class DepartmentServiceImpl implements DepartmentService { //上调下私有化 private DepartmentDao departmentDaonew DepartmentDaoImpl(); public boolean add(Departement departement) { /*int idepartmentDao.insert(departement); if (i0){ return true; }else{ return false; }*/ /*boolean fdepartmentDao.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 ListDepartement findAll() { return departmentDao.selectAll(); } public Departement findById(Departement departement) { return departmentDao.selectById(departement); } }9、编写测试类public class Test { public static void main(String[] args) { DepartmentService departmentServicenew DepartmentServiceImpl(); Departement departementnull; /* System.out.println(-----------增加-----------); departementnew Departement(7,开发部,222); if (departmentService.add(departement)) { System.out.println(添加成功); }else { System.out.println(添加失败); }*/ /*System.out.println(-----------删除-----------); departementnew Departement(7,开发部,222); if (departmentService.remove(departement)) { System.out.println(删除成功); }else { System.out.println(删除失败); }*/ /*System.out.println(-----------修改-----------); departementnew Departement(6,运维部,223); if (departmentService.modify(departement)) { System.out.println(添加成功); }else { System.out.println(添加失败); }*/ System.out.println(-----------查询全部-----------); ListDepartement listdepartmentService.findAll(); for (Departement d:list) { System.out.println(d.getDep_no()\td.getDeo_name()\td.getDep_loc()); } System.out.println(-----------通过id查询-----------); departementnew Departement(6,运维部,223); departementdepartmentService.findById(departement); System.out.println(departement.getDep_no()\tdepartement.getDeo_name()\tdepartement.getDep_loc()); } }