JavaWeb开发技术学习笔记(六)——MVC设计模型与三层架构及其优化

MVC设计模式

M:Model ,模型 :一个功能。用JavaBean实现。

V:View,视图: 用于展示、以及与用户交互。使用html js css jsp jquery等前端技术实现

C:Controller,控制器 :接受请求,将请求跳转到模型进行处理;模型处理完毕后,再将处理的结果
返回给 请求处 。 可以用jsp实现, 但是一般建议使用 Servlet实现控制器。

Jsp->Java(Servlet)->JSP

在这里插入图片描述
在这里插入图片描述

三层架构

与MvC设计模式的目标一致:都是为了解耦合、提高代码复用
区别,二者对项目理解的角度不同。

三层组成

表示层(USL, User Show Layer;视图层)

前台:对应于MVC中的view,用于和用户交互、界面的显示
​ jsp js html css jquery等web前端技术
​ 代码位置: WebContent
后台:对用于MvC中 Controller,用于控制跳转、调用业务逻辑层
​ Servlet( SpringMvc struts2),位于xxx. servlet包中

业务逻辑层(BLL, Business logic layer; Service层)

​ 接收表示层的请求调用
​ 组装数据访问层,逻辑性的操作(增删改査,删:査+删),
​ 一般位于xxx. service包(也可以成为:xxx. manager,xx.b11)

数据访问层(DAL, Data Access layer;Dao层)

​ 直接访问数据库的操作,原子性的操作(增删改查)
​ 一般位于xxx.dao包

三层间的关系:

上层将请求传递给下层,下层处理后返回给上层

扫描二维码关注公众号,回复: 11600688 查看本文章

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h0K6GNjg-1587476027780)(F:\用户\MACROSS\大学\资料\WEB开发技术\Jαva Web配套资料\9三层优化\三层.png)]

三层优化

1.加入接口

​ 建议面向接口开发:先接口-再实现类
​ --service、dao加入接口
​ --接口与实现类的命名规范
​ 接口:interface, 起名 I实体类Service IStudentService
​ IStudentDao
​ 实现类:implements 起名 实体类ServiceImpl StudentServiceImpl
​ StudentDaoImpl
​ 接口: I实体类层所在包名 IStudentService、IStudentDao
​ 接口所在的包: xxx.service xx.dao

​ 实现类: 实体类层所在包名Impl StudentServiceImpl、StudentDaoImpl

​ 实现类所在的包:xxx.service.impl xx.dao.impl

以后使用接口/实现类时,推荐写法:
接口 x = new 实现类();
IStudentDao studentDao = new StudentDaoImpl();

2.DBUtil 通用的数据库帮助类,可以简化Dao层的代码量

帮助类 一般建议写在 xxx.util包

方法重构: 将多个方法 的共同代码 提炼出来,单独写在一个方法中,然后引入该方法即可

Apache DBUtils

下载commons-dbutils-1.7.jar,其中包含以下几个重点类:
DbUtils、QueryRunner、ResultSetHandler

DbUtils:辅助

QueryRunner:增删改查

update() 增删改 参考word文档


自动提交事务 update(sql,参数);update(sql);
手动提交事务 update(connection ,sql,参数);

手动提交事务
转账: zs -1000 -> ls +1000

query()查询


如果是查询,则需要ResultSetHandler接口,有很多实现类,一个实现类对应于一种 不同的查询结果类型
select * from student -> List
select count(1) from student ; int

Object[] , {1,zs}
实现类ArrayHandler :返回结果集中的第一行数据,并用Object[]接收
Object[] = {1 ,zs,23};

实现类ArrayListHandler:返回结果集中的多行数据, List<Object[]>

Student (1 ,zs)

BeanHandler :返回结果集中的第一行数据,用对象(Student)接收
BeanListHandler:返回结果集中的多行数据, List students, stu stu2 stu3
BeanMapHandler: 1:stu,2:stu2, 3:stu3

//反射会通过无参构造来创建对象

– Map

MapHandler:返回结果集中的第一行数据
{id=1 ,name=zs}
String,int
String,String

–>
String,Object

MapListHandler:返回结果集中的多行数据
{{id=2 ,name=ls},{id=3 ,name=ww}}

KeyedHanlder
{ls={id=2 ,name=ls},ww={id=3 ,name=ww}}

–>
ColumnListHander :把結果集中的某一列 保存到List中
2,ls
3,ww

{ls,ww}

–>
select count(1) from xxx ; 23
select name from student where id =2 ;
ScalarHandler :单值结果

问题:查询的实现类的参数问题
query(…, Object… params )

其中Object… params代表可变参数: 既可以写单值,也可以写一个数组
runner.query("… where id = ? and name like ? " ,new ArrayHandler(),new Object[]{1,"%s%"}) ;
runner.query("… where id = ?" ,new ArrayHandler(),1) ;

ORA-00904: “NAMEIKE”: 标识符无效 : SQL关键字写错, ojdbc.jar版本问题

,new Object[]{1,"%s%"}) ;
runner.query("… where id = ?" ,new ArrayHandler(),1) ;

ORA-00904: “NAMEIKE”: 标识符无效 : SQL关键字写错, ojdbc.jar版本问题

数据分页:5变量(属性)

1.数据总数

(select count(*) from xxx , 查数据库)

2.页面大小

(页面容量,每页显示的数据条数) (用户自定义)

3.总页数 (自动计算)

​ 800:10= 80页
​ 总页数= 数据总数 /页面大小

802:10= 800/10 +1 ;
总页数= 数据总数 /页面大小 + 1;

–>通式
总页数= 数据总数 % 页面大小==0 ?数据总数 /页面大小:数据总数 /页面大小 + 1;

注意:自动计算的时机:当 数据总数 和 页面大小都被赋值以后,自动计算总页数。

4.当前页码 (用户自定义)

5.实体类对象集合

(当前页的数据集合):依赖于数据库 (查数据库)
假设: 每页显示10条(页面大小=10)

MYSQL实现分页的sql:

limit开始,多少条
第0页
select * from student limit 0, 10
第1页
select * from student limit 10, 10
第2页
select * from student limit 20, 10
第n页
select * from student limit n 10, 10
select
* from student limit 页数*页面大小,页面大小

猜你喜欢

转载自blog.csdn.net/MACRosshaha/article/details/105668433