MVC框架的有关问题
MVC框架的问题
我现在在学MVC框架,我知道V是视图,也就是HTML模板。C是控制器,由浏览器里输入路由URL,然后调用控制器的方法。但是我有一个疑问,操作数据库的语句是放在M,还是放在C,我看网上很多例也,也是把数据库操作放在C,这样让我有点迷惑,那M的作用是什么?
------解决方案--------------------
M model 一般译做 模型
用于体现业务逻辑,也就是对特定事物的操作
由于web应用都离不开数据库,而数据库最能体现出项目的特征
所以通常将对数据库的操作作为 M
------解决方案--------------------
MVC只不过是一个大致的分层方法,没必要那么死板。
M是模型层,就是一些类了,这个可以把你数据库里边的每个表做成一个MODEL。
V是视图层,模板文件了,也可以理解为HTML,但是绝对不是HTML,看你视图层是用什么模板引擎了。
C是控制层,就是你的引导文件将URL分解以后找到相对应的控制器和处理方法,然后在具体的方法里边调用M层的 类。处理完后转到具体的视图层,将数据填满,再发到用户浏览器。
上边说的比较简陋。
我具体做的时候将M层分为了两层(其实就是一层),其他的没变。
我将M层分为了一个简单类层,和一个服务层。比如说我数据库里边有一个people的表:
我将people写为一个类,这个类只对表属性提供了get和set方法。
然后对应的有一个peopleService类,这个类是提供了对people的操作,主要是CRUD操作及一些逻辑操作。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
