Research on Lightweight MVC Framework Based on Spring MVC and Mybatis
Dandan Zhang, Zhiqiang Wei,Yongquan Yang
Department of Computer Science Ocean University of China Qingdao, China
zddsol@gmail.com, weizhiqiang@ouc.edu.cn , i@yangyongquan.com
Abstract—For the development and operating efficiency of Web applications based on the Model-View-Controller (MVC) framework, and, according to the actual business environment and needs in the project practice, the framework of Web application system is studied in this paper. Through the research of Spring MVC framework and Mybatis framework as well as some related core techniques, combined with JSP and JSTL technology, this paper realizes the design of a lightweight Web application framework based on Spring MVC and Mybatis.
Keywords-Spring MVC; Mybatis; MVC; Framework
- INTRODUCTION
In recent years, with the rapid development of Internet technology and the continuous improvement and innovation made by people according to the needs in the reality, multi- tier Web architecture based on Browser/Server (B/S) mode is gradually maturing. Because of overcoming the drawbacks of the traditional Web application development approach, MVC pattern, is widely used now. In the MVC framework, the system is divided into three layers, named Model, View and Controller. These layers are relatively independent, and each has a clear function. On account of these feathers, MVC is beneficial to modularized development and greatly improves the efficiency, maintainability of system development and code reusability, which can adapt to design requirements of the increasingly complex multi-tier application system .
The development of Web framework based on MVC is unprecedented prosperity in Java 2 Enterprise Edition (J2EE). Some of them have advantage of development efficiency, some have advantage of operating efficiency, and also some have advantage of security. So we can combine advantages of two or more frameworks to create a relatively efficient and lightweight framework.
Spring MVC separates the roles of controllers, models objects, dispatcher, and handler objects, which makes it easier for them to be customized . In addition, Mybatis is a database persistence layer framework, encapsulating Java Database Connectivity (JDBC) and simplifying the processes of manipulating database, and contrast to Hibernate, the degree of freedom and the accessing efficiency for database has been improved. To Adopt Spring MVC and Mybatis technology and organically combine these two technologies to build a lightweight MVC development framework, which can not only effectively improve the efficiency of system development and reduce
the complexity of the development, but also have good improvements in the security, stability and robustness of the system.
This paper is organized as follows. An overview and comparison of Struts2 and Spring MVC, Hibernate and Mybatis is introduced in the second part. The scheme of carrying out a system based on Spring MVC and Mybatis is introduced in the third part. The forth part describes the comparison among these frameworks based on dates and Section5 is the conclusion of this paper.
- RELATED WORK
- MVC Framework
MVC, because of ideally separating business logic and view, has become a trend in the development of program design. As people continued to study MVC, MVC framework has been developed continuously. Presently, more mature and stable MVC framework mainly includes Struts, Struts2, Webwork, Spring MVC, etc. The following introduces Struts2 and Spring MVC.
-
- Struts2
Struts2 is a new Web-tier application framework on the basis of merging Struts1 and WebWork . It regards WebWork as the core, mainly using Java Server Page (JSP) and Servlet technology, and integrates Servlet, JSP, custom tags and information resources into a unified framework. Developers only need to develop the corresponding Action class, ActionFormBean and JSP components, and develop the project applying Struts framework.
-
- Spring MVC
Spring is a lightweight container framework with the properties of inversion of control (IoC) and aspect-oriented programming (AOP) , which has seven well-defined modules. Spring MVC, one of seven modules, is a full- featured lightweight realization of MVC to build Web applications. Spring MVC perfectly achieves core concepts and features of MVC, and you can dynamically configure components by simply configuring. Spring MVC works like this: When the client sends an HTTP request, the core controller (DispatcherServlet) receives the request, and lookup HandleMapping according to the URL to dispatch the request to the appropriate Controller. The Controller calls the appropriate business logic to process the request, and returns the view name and ModelAndView object to DispatcherServlet when process is completed, and finally by ViewResolver the returned ModelAndView is rendered to
the corresponding view.
-
-
The comparisons between Struts2 and Spring MVC
- Operational efficiency: The configuration file amount of Struts2 is bigger than Spring MVC; Spring MVC is more concise when using; the development efficiency of Spring MVC is higher than Struts2.
-
Operational efficiency: The design of Struts2 is based on Class, for the request, it makes class-level interceptors, and the design of Spring MVC is based on the method, it makes method-level interceptor; Struts2 will inject all the attributes of the class which the Action is in, and Spring MVC only injects request data of current Action, so the operati
剩余内容已隐藏,支付完成后下载完整资料
基于SpringMVC和MyBatis的轻量级MVC框架研究
Dandan Zhang, Zhiqiang Wei,Yongquan Yang
计算机科学系
中国青岛海洋大学
zddsol@gmail.com, weizhiqiang@ouc.edu.cn , i@yangyongquan.com
摘要—为了提高基于模型-视图-控制器(MVC)框架的Web应用程序的开发和运行效率,并根据实际业务环境和项目实践的需要,对Web应用系统的框架进行了研究。通过对SpringMVC框架和MyBatis框架的研究,以及相关的核心技术,结合JSP和JSTL技术,实现了基于SpringMVC和MyBatis的轻量级Web应用框架的设计。
关键词spring mvc;mybatis;mvc;framework
一、引言
近年来,随着互联网技术的快速发展和人们根据实际需要不断改进和创新,基于浏览器/服务器(B/S)模式的多层网络体系结构逐渐成熟。由于克服了传统Web应用程序开发方法的缺点,MVC模式目前得到了广泛的应用。在MVC框架中,系统分为模型层、视图层和控制器层。这些层相对独立,每个层都有一个清晰的功能。基于这些特点,MVC有利于模块化开发,大大提高了系统开发的效率、可维护性和代码重用性,能够适应日益复杂的多层应用系统的设计要求。.
基于MVC的Web框架的开发是Java 2企业版(J2EE)的空前繁荣。有的具有开发效率优势,有的具有运行效率优势,有的具有安全性优势。因此,我们可以将两个或多个框架的优势结合起来,创建一个相对高效和轻量级的框架。
SpringMVC分离了控制器、模型对象、调度器和处理程序对象的角色,这使得它们更容易被定制。此外,MyBATIS是一个数据库持久层框架,封装了Java数据库连接(JDBC),简化了操作数据库的过程,并与Hibernate进行对比,提高了数据库的自由度和访问效率。采用Spring MVC和Mybatis技术,将这两种技术有机结合,构建一个轻量级的MVC开发框架,不仅能有效提高系统开发效率,降低开发的复杂度,而且对系统的安全性、稳定性和鲁棒性也有很好的提高。
本文的组织结构如下。第二部分对Struts2和SpringMVC、Hibernate和Mybatis进行了概述和比较。第三部分介绍了基于SpringMVC和Mybatis的系统实现方案。第四部分介绍了基于日期的框架比较,第五部分是本文的结论。
二、相关工作
a.MVC框架
MVC由于实现了业务逻辑和视图的理想分离,已经成为程序设计发展的一个趋势。随着人们对MVC的不断研究,MVC框架不断发展。目前,比较成熟稳定的MVC框架主要包括struts、struts2、webwork、spring mvc等,下面介绍struts2和spring mvc。
-
- Struts2
Struts2是在Struts1和Webwork合并的基础上开发的一种新的Web层应用框架。它以WebWork为核心,主要采用Java Server Page(JSP)和Servlet技术,将Servlet、JSP、自定义标签和信息资源集成到一个统一的框架中。开发人员只需要开发相应的action类、actionformbean和jsp组件,并使用struts框架开发项目。
-
- Spring MVC
Spring是一个轻量级的容器框架,具有控制反转(IOC)和面向方面编程(AOP)的特性,它有七个定义良好的模块。SpringMVC是七个模块中的一个,它是MVC构建Web应用程序的全功能轻量级实现。SpringMVC完美地实现了MVC的核心概念和特性,您可以通过简单的配置来动态地配置组件。SpringMVC的工作方式是这样的:当客户端发送一个HTTP请求时,核心控制器(DispatcherServlet)接收该请求,并根据URL查找handleMapping以将该请求发送到相应的控制器。控制器调用适当的业务逻辑来处理请求,并在进程完成时将视图名称和ModelAndView对象返回给DispatcherServlet,最后由ViewResolver将返回的ModelAndView呈现给相应的视图。
-
-
Struts2 和 Spring MVC的比较
- 运行效率: Struts2配置文件量大于spring mvc,使用时spring mvc更简洁,spring mvc的开发效率高于struts2。
- 运行效率: Struts2的设计是基于类的,对于请求,它做类级的拦截器,Spring MVC的设计是基于方法,它做方法级的拦截器;Struts2将注入动作所在类的所有属性,Spring MVC只注入当前动作的请求数据,所以SPRI的运行效率ng mvc将高于struts2。选择:突出显示所有作者和关联行。
- SpringMVC实现了业务逻辑和视图之间的完全分离。业务逻辑处理后,返回一个字符串,没有任何与视图层显示技术相关联的标志,然后通过在viewresolver中配置的字符串对应的显示技术显示数据。
-
Struts2 和 Spring MVC的比较
- Hibernate和Mybatis
mybatis和hibernate是数据库持久层框架,具有不同程度的JDBC封装。与直接使用JDBC访问数据库相比,它们的开发效率有了显著提高。
-
- Hibernate
Hibernate是一个对象关系映射框架,封装了JDBC。因此,在开发业务逻辑时,程序员可以采用面向对象的编程,而不是面向数据库的编程,这使得项目开发中的分工更加清晰,开发效率得到提高。然而,学习冬眠并不是一个很低的门槛,而是一个更高的门槛来精通。它需要用户在如何设计O/R映射以及如何在性能和对象模型之间取得平衡方面有更多的经验和强大的能力。
-
- Mybatis
MyBatis是一个优秀的半自动数据库持久层ORM框架,它支持常见的SQL查询、存储过程和高级映射,以及其他操作。MyBATIs使用简单的XML或注释来进行配置和原始映射,并将接口和Java POJO映射到数据库中的记录。
三、基于SpringMVC和MyBatis的MVC框架的实现
在基于Spring MVC和MyBatis的MVC框架实现中,利用Spring MVC框架给出了MVC模式的表达式,MyBatis作为数据库持久性框架,Excel、JSP、PDF、jQuery结合JSTL作为视图技术,并在程序运行时进行了大量的日志处理。
根据MVC的层思想,在该框架的实现中,控制器层由核心控制器和业务控制器组成,视图层主要由JSP和Excel等实现,模型层包括数据访问对象(DAO)层、服务层、bean层,DAO层使用mybatis访问数据库。
- 配置
对于SpringMVC,需要进行以下配置:(1)在部署描述符文件中配置核心控制器;(2)在SpringMVC的配置文件中配置viewresolver;(3)由于使用了注解,需要启动组件扫描,激活SpringMVC配置文件中的注解扫描功能。(4) 在业务控制器中执行handleMapping配置。
- Mybatis的工作流程
在完成上述两种类型的配置文件之后,我们可以使用它们对数据库执行CRUD操作。以“插入用户记录”为例,工作流描述如下:
-
- MybatisConnectionFactory.java: 生成sqlsessionFactory实例
Reader reader = Resources.getResourceAsReader ('mybatis-config.xml');
sqlSessionFactory=new
SqlSessionFactoryBuilder().build(reader);
-
- BaseDAO.java: 定义此类中的基本操作方法。插入操作定义如下:
session.insert(operation, o);
session.commit();
} finally { session.close();
}
}
try {
SqlSession session =
sqlSessionFactory.openSession();
public void insert(String operation,T o) {
-
- UserDAO.java: 继承 BaseDAO
BaseDAOlt;Usergt;{
extends
Methods needed to be written or added.
}
UserDAO
Public class
@Repository
-
- UserServiceImpl.java: userdao中的调用方法
在UserServiceImpl的实现类中,通过调用UserDAO中的insert()方法将用户记录插入数据库。
public void insertuser(User o) { userDAO.insert('User.insert', o);
}
四、框架性能评估
对于一个框架,它的性能可以从开发效率、操作效率、系统安全性、可维护性等方面进行检查。本文已经对相关框架的部分性能进行了分析,对基于Spring MVC和经典框架ssh的MVC框架以及Hibernate和Mybatis的MVC框架进行了运行效率的测试和分析。
表1. 测试是在相同的环境和大致相同的任务中完成的:
CPU
Intel(R) Pentium(R) Dual CPU E2140
Memory
2.0G
Web Container
Tomcat6.0
Operating System
Windows7
Database
Mysql5.5
Request
查询600条用户表记录,通过jsp页面显示
使用不同的框架执行相同的请求60次,用过滤器测量每个请求的处理时间。DAO层记录了休眠和mybatis访问数据库的执行时间。测试结果如图1所示。:
表2. 请求的平均运行时间
Spring
MVC
SSH
Mybatis
Hibernate
Average Time(ms)
34.339
83.271
16.678
19.407
图1.平均时间
从上图可以看出,基于SpringMVC和MyBatis的MVC框架的运行效率明显高于ssh,增长了近58.76%。Mybatis的操作效率比Hibernate高14.06%。总之,基于SpringMVC和MyBatis的框架是一个相对轻量的MVC框架。
五、结论 剩余内容已隐藏,支付完成后下载完整资料
资料编号:[19823],资料为PDF文档或Word文档,PDF文档可免费转换为Word
-
-
The comparisons between Struts2 and Spring MVC
以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。