基于Spring MVC和Mybatis的轻量级MVC框架研究外文翻译资料

 2022-12-19 17:08:50

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

I. 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, multitier 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 [1].

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 [2]. In addition, Mybatis is a database persistence layer framework, encapsulating Java Database Connectivity (JDBC) [3] 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.

II. RELATED WORK

A. 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.

1) Struts2

Struts2 is a new Web-tier application framework on the basis of merging Struts1 and WebWork [4]. 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.

2) Spring MVC

Spring is a lightweight container framework with the properties of inversion of control (IoC) and aspect-oriented programming (AOP) [5], which has seven well-defined modules. Spring MVC, one of seven modules, is a fullfeatured 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. Fig. 1 shows Request-Handling Pipeline of Spring MVC.

Figure 1. Request-Handling Pipeline of Spring MVC.

3) The comparisons between Struts2 and Spring MVC

  1. 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.
  2. 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 operating efficiency Spring MVC will be higher than Struts2.Selec

    剩余内容已隐藏,支付完成后下载完整资料


    基于Spring MVC和Mybatis的轻量级MVC框架研究

    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

    摘要:针对基于模型-视图-控制器(MVC)框架的Web应用程序的开发和运行效率,并根据实际业务环境和项目实践中的需求研究Web应用系统的框架。通过对Spring MVC框架和Mybatis框架的研究以及相关的核心技术,结合JSP和JSTL技术,实现了基于Spring MVC和Mybatis的轻量级Web应用框架的设计。

    关键词:Spring MVC;MyBatis;MVC;框架

    I. 引言

    近年来,随着互联网技术的快速发展以及人们根据现实需要不断进行改进和创新,基于Browser / Server(B / S)模式的多层Web架构正在逐步成熟。由于克服了传统Web应用程序开发方法的缺点,MVC模式现在被广泛使用。在MVC框架中,系统分为三层,分别为Model,View和Controller。这些层次相对独立,每个层都具有明确的功能。基于这些优点,MVC有利于模块化开发,大大提高了系统开发的效率、可维护性和代码可重用性,可适应日益复杂的多层应用系统的设计要求[1]。

    基于MVC的Web框架的开发在Java 2企业版(J2EE)中是前所未有的繁荣。它们其中有一些具有开发效率的优点,有一些具有操作效率的优点,有一些还具有安全性的优点。因此,我们可以结合两个或更多框架的优点来创建一个相对高效和轻量级的框架。

    Spring MVC分离了控制器,模型对象,调度程序和处理程序对象的角色,这使得它们更容易被自定义[2]。此外,Mybatis是一个数据库持久层框架,封装了Java数据库连接(JDBC)[3]并简化了操作数据库的过程,与Hibernate形成对比,提高了数据库的自由度和访问效率。采用Spring MVC和Mybatis技术,将这两种技术有机结合,构建轻量级的MVC开发框架,不仅可以有效提高系统开发效率,降低开发的复杂性,而且在安全性,稳定性方面也有很好的改进。和系统的稳健性。

    本文的结构如下。第二部分介绍了Struts2和Spring MVC,Hibernate和Mybatis的概述和比较。第三部分介绍了基于Spring MVC和Mybatis的系统实现方案。第四部分描述了基于日期的这些框架之间的比较,第五部分是本文的结论。

    II. 相关工作

    A. MVC 框架

    MVC由于理想地将业务逻辑和视图分离,已成为程序设计发展的趋势。随着人们继续研究MVC,MVC框架不断发展。目前,更成熟稳定的MVC框架主要包括Struts,Struts2,Webwork,Spring MVC等。下面介绍Struts2和Spring MVC。

    1) Struts2

    Struts2是一个基于合并Struts1和WebWork的新的Web层应用程序框架[4]。它以WebWork为核心,主要使用Java Server Page(JSP)和Servlet技术,并将Servlet,JSP,自定义标签和信息资源集成到一个统一的框架中。开发人员只需要开发相应的Action类,ActionFormBean和JSP组件,并开发应用Struts框架的项目。

    2) Spring MVC

    Spring是一个轻量级的容器框架,具有控制反转(IoC)和面向方面编程(AOP)[5]的特性,它有七个明确定义的模块。Spring MVC是七个模块之一,是构建Web应用程序的MVC的全功能轻量级实现。Spring MVC完美地实现了MVC的核心概念和功能,您可以通过简单的配置动态配置组件。Spring MVC的工作方式如下:当客户端发送HTTP请求时,核心控制器(DispatcherServlet)接收请求,并根据URL查找HandleMapping以将请求分派给相应的Controller。Controller调用适当的业务逻辑来处理请求,并在进程完成时将视图名称和ModelAndView对象返回给DispatcherServlet,最后通过ViewResolver将返回的ModelAndView呈现给相应的视图。图1显示了Spring MVC的请求处理流水线。

    图1. Spring MVC的请求处理路径。

    3) Struts2和Spring MVC之间的比较

    1. 运行效率: Struts2的配置文件数量大于Spring MVC; Spring MVC在使用时更简洁; Spring MVC的开发效率高于Struts2。
    2. 运行效率: Struts2的设计基于Class,对于请求,它产生类级拦截器,而Spring MVC的设计基于该方法,它使得方法级拦截器; Struts2将注入Action所在类的所有属性,而Spring MVC只注入当前Action的请求数据,因此Spring MVC的运行效率将高于Struts2.
    3. Spring MVC实现了业务逻辑和视图之间的完全分离。在处理业务逻辑之后,它返回一个字符串,没有关于视图层显示技术的任何关联标志,然后通过与ViewResolver中配置的字符串对应的显示技术显示数据。

    B. Hibernate和 Mybatis

    Mybatis和Hibernate是数据库持久层框架,对JDBC有不同程度的封装。与直接使用JDBC访问数据库相比,它们的开发效率得到了显着提高。

    1) Hibernate

    Hibernate是一个对象关系映射框架[6] [7],封装了JDBC。因此,在开发业务逻辑时,程序员可以采用面向对象的编程而不是面向数据库的编程,这使得它更加清晰地分工,提高了项目开发的开发效率。但是,学习Hibernate并不是一个低门槛,并且是一个更高的门槛才能精通。对于用户如何设计O / R映射以及如何在性能和对象模型之间取得平衡,它需要更多的经验和强大的能力。

    2) Mybatis

    Mybatis是一个优秀的半自动数据库持久层ORM框架,它支持常见的SQL查询,存储过程和高级映射等操作[8]。Mybatis使用简单的XML或注释来进行配置和原始映射,并将接口和Java POJO映射到数据库中的记录。

    Mybatis的工作流程如图2所示:

    图2. Mybatis的工作流程。

    III. 基于Spring MVC和MYBATIS的MVC框架的实现

    在基于Spring MVC和Mybatis的MVC框架的实现中,它使用Spring MVC框架表达了MVC模式,Mybatis作为数据库持久化框架,Excel,JSP,PDF,jQuery结合JSTL作为View技术,而Logback处理 程序运行时有大量日志。

    根据MVC的层次思想,在该框架的实现中,Controller层包括核心控制器和业务控制器,View层主要用JSP和Excel等实现.Model层包括数据访问对象(DAO)层, 服务层,Bean层和DAO层使用Mybatis来访问数据库。图3显示了框架结构。

    图3.框架结构

    A. 配置

    对于Spring MVC,需要完成以下配置[9] :( 1)在部署描述符文件中配置Core Controller; (2)在Spring MVC的配置文件中配置ViewResolver; (3)由于使用了注释,需要在Spring MVC的配置文件中启动组件扫描并激活注释扫描功能; (4)在Business Controller中进行HandleMapping配置。

    在使用Mybatis访问数据库之前,需要配置两种类型的文件。一个是主配置文件,它配置数据库连接信息并加载实体的Mapper配置文件,以及为实体定义别名; 另一个是Entity的Mapper配置文件,它定义了此参数结果CRUD操作的命名空间,并为每个操作指定了不同的id。

    B. Mybatis的工作流程

    完成上面两种类型的配置文件后,我们可以使用它们对数据库进行CRUD操作。以“插入用户记录”为例,工作流程描述如下:

    a) MybatisConnectionFactory.java: Build

    SqlSessionFactory instance

    Reader reader = Resources.getResourceAsReader ('mybatis-config.xml'); sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);

    1. BaseDAO.java: 在此类中定义基本操作方法。插入操作定义如下:

    public void insert(String operation,T o) {

    SqlSession session

    sqlSessionFactory.openSession();

    try {

    session.insert(operation, o); session.commit();

    } finally {

    session.close();

    }

    }

    =

    1. UserDAO.java: 继承 BaseDAO

    @Repository

    Public class UserDAO extends

    BaseDAOlt;Usergt;{

    Methods needed to be written or added.

    }

    1. UserServiceImpl.java: UserDAO中的调用方法

    在UserServiceImplto的实现类中,通过调用UserDAO中的insert()方法将用户记录插入到数据库中。

    public void insertuser(User o) {

    userDAO.insert('User.insert', o);

    }

    IV. 框架性能的完整性

    对于框架,可以从开发效率,运营效率,系统安全性,可维护性等方面检查其性能。由于已经描述了对本文相关框架部分性能的分析,因此将基于Spring MVC和经典框架SSH [10]以及Hibernate和Mybatis对MVC框架进行运行效率测试和分析。

    表I.测试是在相同的环境和相同的任务中进行的:

    CPU

    Intel(R) Pentium(R) Dual CPU E2140

    内存

    2.0G

    服务器

    Tomcat6.0

    操作系统

    Windows7

    数据库

    Mysql5.5

    请求

    查询User表的600条记录并通过JSP页面显示它们

    使用不同的框架执行了相同的请求60次,以使用过滤器测量每个请求的处理时间。Hibernate和Mybatis访问数据库的执行时间记录在DAO层中。测试结果如图5所示:

    图4.测试结果

    TABLE II. 平均运行时间要求:

    Spring

    MVC

    SSH

    Mybatis

    Hibernate

    平均时间(ms)

    34.339

    83.271

    16.678

    19.407

    图5.平均时间

    根据上图,很明显,基于Spring MVC和

    剩余内容已隐藏,支付完成后下载完整资料


    资料编号:[19972],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

课题毕业论文、外文翻译、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。