mvc指的是m(model 模型) v(view 视图) c(controller 控制器)三个模块组成的代码项目。它规定了一种Javaweb的编程规范。
在Web MVC模式下,模型无法主动推数据给视图,如果用户想要视图更新,需要再发送一次请求(即请求-响应模 型)。
MVC想做到代码之间的耦合度降低,能够各司其职。
三层架构 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI)、业务逻辑层(BLL)、数据访问层 (DAL)。区分层次的目的即为了“高内聚,低耦合”的思想。
1、表现层(UI):通俗讲就是展现给用户的界面,即用户在使用一个系统的时候他的所见所得。 jsp/html
2、业务逻辑层(BLL):针对具体问题的操作,也可以说是对数据层的操作,对数据业务逻辑处理。 servlet,service
3、数据访问层(DAL):该层所做事务直接操作数据库,针对数据的增添、删除、修改、更新、查找等。dao
表现层实现的代表作品是Struts,springmvc框架,
业务层实现的代表作品是Spring,
持久层实现的代表作品是Hibernate,mybatis。
层就相当于一个黑盒子,我们不用知道它内部怎么实现,只需要知道如何去调用它就行了。每层只与上下相邻的两层打交道。当一层内部由于技术变迁发生变化时,只要接口不变,其他层不用做任何改变。分层之后灵活性提高, 也便于团队分工开发。
联系:
区别:
项目文件图例 与 执行顺序
JavaBean student类
package com.java.bean;
//实体类(javabean)
//类名=表名 列名=属性名
public class Student {
private int studentId;
private String studentNum;
private String stuName;
private int stuAge;
public Student() {
}
public Student(int studentId, String studentNum, String stuName, int stuAge) {
this.studentId = studentId;
this.studentNum = studentNum;
this.stuName = stuName;
this.stuAge = stuAge;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentNum() {
return studentNum;
}
public void setStudentNum(String studentNum) {
this.studentNum = studentNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}
dao接口:
public interface StudentDao {
public List<Student> getall();
}
dao实现类:
package com.java.dao.impl;
import com.java.bean.Student;
import com.java.dao.StudentDao;
import com.java.util.DruidUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDaoImpl extends DruidUtil implements StudentDao {
@Override
public List<Student> getall() {
List list = new ArrayList();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
String sql = "select * from student";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
Student student = new Student();
student.setStudentId(resultSet.getInt("studentid"));
student.setStudentNum(resultSet.getString("studentnum"));
student.setStuName(resultSet.getString("stuname"));
student.setStuAge(resultSet.getInt("stuage"));
list.add(student);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(connection,preparedStatement,resultSet);
}
return list;
}
}
service接口:
package com.java.service;
import com.java.bean.Student;
import java.util.List;
//定义操作数据库的方法
public interface StudentService {
public List<Student> getall();
}
service实现类:
package com.java.service.impl;
import com.java.bean.Student;
import com.java.dao.StudentDao;
import com.java.dao.impl.StudentDaoImpl;
import com.java.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao dao = new StudentDaoImpl();
@Override
public List<Student> getall() {
return dao.getall();
}
}
druid工具:
package com.java.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DruidUtil {
private static DataSource ds;
static{
try {
Properties ppt = new Properties();
ppt.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(ppt);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从连接池中取出一个连接给用户
* @return
*/
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
public static void close(Connection conn, Statement state, ResultSet rs){
try {
rs.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
state.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
conn.close();
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
studentServlet:
package com.java.web;
import com.java.bean.Student;
import com.java.service.StudentService;
import com.java.service.impl.StudentServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
//@WebServlet(value = "/getallstudent")
public class StudentServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接受参数
//2.调取service方法
StudentService studentService = new StudentServiceImpl();
List<Student> students = studentService.getall();
//3.跳转页面
req.setAttribute("students",students);
System.out.println("servlet完成");
req.getRequestDispatcher("show.jsp").forward(req,resp);
}
}