中国建筑招标投标网官网,浙江seo,网站死链如何处理,请科技公司做网站需要注意什么一、概述:
MVC(Model View Controller)是软件工程中的一种 软件架构模式 #xff0c;它把软件系统分为模型、视图和控制器三个基本部分。用一种业务逻辑、数据、界面显示分离的方法组织代码#xff0c;将业务逻辑聚集到一个部件里面#xff0c;在改进和个性化定制界面及用户…一、概述:
MVC(Model View Controller)是软件工程中的一种 软件架构模式 它把软件系统分为模型、视图和控制器三个基本部分。用一种业务逻辑、数据、界面显示分离的方法组织代码将业务逻辑聚集到一个部件里面在改进和个性化定制界面及用户交互的同时不需要重新编写业务逻辑。
M代表Model模型层具体功能如下:
1.存放和数据库对应的实体类以及一些用于存储非数据库表完整相关的VO对象;
2.存放一些对数据进行逻辑运算操作的一些业务处理代码;
V代表view视图层具体功能如下:
1.存放一些视图文件相关的代码,例如html、css以及js等
2.在前后端分离的项目中后端已经没有视图文件该层次已经衍化成独立的前端项目
C代表Controller控制层,具体功能如下:
1.接收客户端请求获得请求数据
2.将准备好的数据响应给客户端;
二、MVC模式下项目中的常见包:
M:
1.实体类包(pojo /entity /bean):专门存放和数据库对应的实体类和一些VO对象;
2.数据库访问包(dao/mapper):专门存放对数据库不同表格CURD方法封装的一些类;
3.服务包(service):专门存放对数据进行业务逻辑运算的一些类;
C:
控制层包(controller);
V:
1.web目录下的视图资源html、css、js、img等
2.前端工程化后在后端项目中已经不存在了; 三、项目搭建:
项目结构: 1.数据库准备:创建schedule_system数据库并执行如下语句
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS 8;
DROP TABLE IF EXISTS sys_schedule;
CREATE TABLE sys_schedule(
sid int NOT NULL AUTO_INCREMENT,
uid int NULL DEFAULT NULL,
title varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0908_ai_Ci NULL DEFAULT NULL,
completed int(1) NULL DEFAULT NULL,
PRIMARY KEY(sid)USING BTREE
)ENGINE InnoDB AUTO_INCREMENT 1 CHARACTER SET Utf8mb4 COLLATE utf8mb4_0900_ai_ci ROW_FORMAT Dynamic;
DROP TABLE IF EXISTS sys_user;
CREATE TABLE sys_user(
uid int NOT NULL AUTO_INCREMENT
username varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
user_pwd varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY(uid)USING BTREE,
UNIQUE INDEX username(username)USING BTREE
)ENGINE InnoDB CHARACTER SET utf8mb4 COLLATEutf8mb4 0900_ai_ci ROW_FORMAT Dynamic;
INSERT INTO sys_user VALUES(1,zhangsan,e10adc3949ba59abbe56e057f20f883e);
INSERT INTO sys_user VALUES(2,lisi,e10adc3949ba59abbe56e057f20f883e);
SET FOREIGN_KEY_CHECKS 1;
2.pojo包处理:使用lombok处理getter、setter、equals、hashcode、构造器
lombok使用步骤:
(1)检查idea是否已经安装lombok插件
(2)检查是否勾选enableannotationprocessing
(3)导入lombok依赖在实体类上添加注解
package com.atguigu.schedule.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
AllArgsConstructor//添加带有全部参数的构造器
NoArgsConstructor//添加无参构造器
Data//添加getter、setter、equals、hashcode
public class SysUser implements Serializable{private Integer uid;private String username;private String userPwd;
}
package com.atguigu.schedule.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
AllArgsConstructor
NoArgsConstructor
Data
public class SysSchedule implements Serializable{private Integer sid;private Integer uid;private String title;private Integer completed;
}
3.dao包的处理:dao类用于定义针对表格的CURD的方法
每张数据表都应该对应一个dao类该dao类专门用于封装对该数据表的操作内容
(1).创建BaseDao对象
package com.atguigu.schedule.dao;
import com.atguigu.schedule.util.JDBCUtil;
import java.lang.reflect.Field;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class BaseDao{//公共的查询方法 返回的是单个对象public TT baseQueryObject(ClassT clazz, String sql,Object ... args){T t null;Connection connection JDBCUtil.getConnection();PreparedStatement preparedStatement null;ResultSet resultSet null;int rows 0;try{// 准备语句对象preparedStatement connection.prepareStatement(sql);//设置语句上的参数for(int i0;iargs.length;i){preparedStatement.setObject(i 1,args[i]);}// 执行 查询resultSet preparedStatement.executeQuery();if(resultSet.next()){t (T) resultSet.getobject(1);}}catch(Exception e){e.printStackTrace();}finally {if(null ! resultSet){try{resultSet.close();}catch(SQLException e){e.printStackTrace();}}if(null ! preparedStatement){try{preparedStatement.close();}catch(SQLException e){e.printStackTrace();}}JDBCUtil.releaseConnection();}return t;}//公共的查询方法 返回的是对象的集合public T ListT baseQuery(Class clazz,String sql,Object ... args){ListT list new ArrayList();Connection connection JDBCUtil.getConnection();PreparedStatement preparedStatement null;ResultSet resultSet null:int rows 0;try {// 准备语句对象preparedStatement connection.prepareStatement(sql);//设置语句上的参数for(int i0;iargs.length;i){preparedStatement.setObject(i 1,args[i]);}// 执行 查询resultSet preparedStatement.executeQuery();ResultSetMetaData metaData resultSet.getMetaData();int columnCount metaData.getColumnCount();//将结果集通过反射封装成实体类对象while(resultSet.next()){//使用反射实例化对象Object obj clazz.getDeclaredConstructor().newInstance();for(int i1;icolumnCount;i){String columnName metaData.getColumnLabel(i);Object value resultSet.getObject(columnName);//处理datetime类型字段和java.util.Data转换问题if(value.getClass().equals(LocalDateTime.class)){value Timestamp.valueOf((LocalDateTime) value);}Field fieldclazz.getDeclaredField(columnName);field.setAccessible(true);field.set(obj,value);}list.add((T)obj);}} catch (Exception e){e.printStackTrace();}finally {if(null ! resultSet){try{resultSet.close();}catch(SQLException e){e.printStackTrace();}}if(null ! preparedStatement){try{preparedStatement.close();}catch(SQLException e){e.printStackTrace();}}JDBCUtil.releaseConnection();}return list;}// 通用的增删改方法public int baseUpdate(String sql,Object ... args){//获取连接Connection connection JDBCUtil.getConnection();PreparedStatement preparedStatement null;int rows 0;try {// 准备语句对象preparedStatementconnection.prepareStatement(sql);//设置语句上的参数for(int i0;iargs.length;i){preparedStatement.setObject(i1,args[i]);}//执行 增删改executeUpdaterows preparedStatement.executeUpdate();// 释放资源(可选)}catch(SQLException e){e.printStackTrace();}finally{if(null ! preparedstatement){try{preparedStatement.close();}catch(SQLException e){throw new RuntimeException(e);}}JDBCUtil.releaseConnection();}return rows;}
}
(2).dao层所有接口:
package com.atguigu.schedule.dao;
public interface SysUserDao {
}
package com.atguigu.schedule.dao;
public interface SysScheduleDao{
}
(3).dao层所有实现类:
package com.atguigu.schedule.dao.impl;
importcom.atguigu.schedule.dao.BaseDao;
import com.atguigu.schedule.dao.SysUserDao;
public class SysUserDaoImpl extends BaseDao implements SysUserDao {
}
package com.atguigu.schedule.dao.impl;
import com.atguigu.schedule.dao.BaseDao;
import com.atguigu.schedule.dao.SysScheduleDao;
public class SysScheduleDaoImpl extends BaseDao implements SysScheduleDao{
}
4.service包处理
(1)接口:
package com.atguigu.schedule.service;
public interface SysUserService{
}
package com.atguigu.schedule.service;
public interface SysScheduleService{
}
(2)实现类:
package com.atguigu.schedule.service.impl;
import com.atguigu.schedule.service.SysUserService;
public class SysUserServiceImpl implements SysUserService{
}
package com.atguigu.schedule.service.impl;
import com.atguigu.schedule.service.SysScheduleService;
public class SysScheduleServiceImpl implements SysScheduleService{
}
5.controller包处理:
(1)BaseController:
package com.atguigu.schedule.controller;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
public class BaseController extends HttpServlet {Overrideprotected void service(HttpServletRequest reg, HttpServletResponse resp) throws ServletException,IOException{String requestURI reg.getRequestURI();String [] split requestURI.split(/);String methodName split[split.length-1];Class clazz this.getClass();try{Method method clazz.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);method.setAccessible(true);method.invoke(this,req,resp);}catch(Exception e){e.printStackTrace();}}
}
(2)多个处理器继承BaseController:
package com.atguigu.schedule.controller;
import jakarta.servlet.annotation.WebServlet;
WebServlet(/user/*)
public class UserController extends BaseController{
}
package com.atguigu.schedule.controller;
import jakarta.servlet.annotation.WebServlet;
WebServlet(/schedule/*)
public class SysScheduleControllerextends BaseController{6.加密工具类的使用:
package com.atguigu.schedule.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public final class MD5Util {public static String encrypt(String strSrc){try{char hexChars[] {0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f};byte[]bytes strSrc.getBytes();MessageDigest md MessageDigest.getInstance(MD5);md.update(bytes);bytes md.digest();int j bytes.length;char[] chars new char[j * 2];int k 0;for(int i0;ibytes.length;i){byte b bytes[i];chars[k] hexChars[b 4 0xf];chars[k] hexChars[b 0xf];}return new String(chars);} catch (NoSuchAlgorithmException e){e.printStackTrace();throw new RuntimeException(MD5加密出错!!!);}}
}
7.注册业务的处理:
(1)controller:
package com.atguigu.schedule.controller;
import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.service.impl.SysUserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import iava.io.IOException;
WebServlet(/user/*)
public class SysUserController extends BaseContoller{private SysUserService userService new SysUserServiceImpl();protected void regist(HttpServletRequest reg, HttpServletResponse resp) throws ServletException,IOException{//1 接收客户端提交的参数String username reg.getParameter(username);String userPwd reg.getParameter(userPwd);//2 调用服务层方法,完成注册功能//将参数放入一个SysUser对象中在调用regist方法时传入SysUser sysUser new SysUser(null,username,userPwd);int rows userService.regist(sysUser);//3 根据注册结果(成功 失败)做页面跳转if(rows0){resp.sendRedirect(/registSuccess.html);}else{resp.sendRedirect(/registFail.html);}}
} }
(2)service:
package com.atguigu.schedule.service;
import com.atguigu.schedule.pojo.SysUser;
public interface SysUserService {int regist(SysUserregistUser);
}
package com.atguigu.schedule.service.impl;
import com.atguigu.schedule.dao.SysUserDao ;
import com.atguigu.schedule.dao.impl.SysUserDaoImpl;
import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.util.MD5Util;
public class SysUserServiceImpl implements SysUserService{private SysUserDaouserDao new SysUserDaoImpl();Overridepublic int regist(SysUser sysUser){//将用户的明文密码转换为斋文码sysUser.setUserPwd(MD5Util.encrypt(sysUser.getUserPwd()));//调用DAO层的方法将sysUser信息存入数据库return userDao.addSysUser(sysUser);}
}
(3)DAO:
package com.atguigu.schedule.dao;
import com.atguigu.schedule.pojo.SysUser;
public interface SysUserDao {int addSysUser(SysUser sysUser);
}
package com.atguigu.schedule.dao.impl;
import com.atguigu.schedule.dao.BaseDao;
import com.atguigu.schedule.dao.SysUserDao;
import com.atguigu.schedule.pojo.SysUser;
public class SysUserDaoImpl extends BaseDao implements SysUserDao {Overridepublic int addSysUser(SysUser sysUser){String sql insert into sys_user values(DEFAULT,?,?);return baseUpdate(sql,sysUser.getUsername(),sysUser.getUserPwd());}
}
8.登录业务处理
(1)controller
package com.atguigu.schedule.controller;
import com.atquigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.service.impl.SysUserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import iakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
WebServlet(/user/*)
public class SysUserController extends BaseContoller {private SysUserService userService new SysUserServiceImpl();protected void login(HttpServletRequest reg, HttpServletResponse resp) throws ServletException,IOException{//1.接收用户名和密码String username req.getParameter(username);String userPwd reg.getParameter(userPwd);//2.调用服务层方法,根据用户名查询用户信息SysUser loginUser userService.findByUsername(username);if(null loginuser){//跳转到用户名有误提示页resp.sendRedirect(/loginusernameError.html);}else if(!M5Util.encrypt(userPwd).equals(loginuser.getUserPwd())){//3 判断密码是否匹配//跳转到密码有误提示页resp.sendRedirect(/loginUserPwdError.html);}else{//4 跳转到首页resp.sendRedirect(/showSchedule.html);}}
}
(2)service
package com.atguigu.schedule.service;
import com.atguigu.schedule.pojo.SysUser;
public interface SysUserservice {SysUser findByUsername(String username);
}
package com.atquigu.schedule.service.impl;
import com.atguigu.schedule.dao.SysUserDao;
import com.atguigu.schedule.dao.impl.SysUserDaoImpl;
import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.util.MD5Util;
public class SysUserServiceImpl implements SysUserService{private SysUserDao userDao new SysUserDaoImpl();Overridepublic SysUser findByUsername(String username){// 调用服务层方法,继续查询return userDao.findByUsername(username):}
}
(3)dao
package com.atguigu.schedule.dao;
import com.atguigu.schedule.pojo.SysUser;
public interface SysUserDao {SysUser findByUsername(String username);
}
package com.atguigu.schedule.dao.impl;
import com.atguigu.schedule.dao.BaseDao;
import com.atguigu.schedule.dao.SysUserDao;
import com.atguigu.schedule.pojo.SysUser;
import java.util.List;
public class SysUserDaoImpl extends BaseDao implements SysUserDao{Overridepublic SysUser findByUsername(String username){String sql select uid,username, user_pwd userPwd from sys_user where username ?;ListSysUser userList baseQuery(SysUser.class, sql, username);return null ! userList userlist.size()8? userList.get(0):null;}
{
文章转载自: http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.cfcdr.cn.gov.cn.cfcdr.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.tlbhq.cn.gov.cn.tlbhq.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.cprbp.cn.gov.cn.cprbp.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.pfnlc.cn.gov.cn.pfnlc.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.dwdjj.cn.gov.cn.dwdjj.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.smj79.cn.gov.cn.smj79.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.sskns.cn.gov.cn.sskns.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn