Su warm small talk Java framework (SSH, SSM, Springboot)

The basic concept of this series of popular java framework used to record the difference and contact, also recorded a solution to some of the problems in the course, encountered facilitate their view, but also facilitate access.

Haste makes waste To reach the haste makes waste!

A, SSH

1, the basic concept

SSH framework of the integrated JAVA EE in three frameworks, which are composed of Struts, Spring, Hibernate framework, is currently more popular open source java web framework.

SSH into systems integration framework (Struts2-- control; spring-- decoupling; hibernate-- operational database) from the duty to help developers build a clear structure in the short term, it can take a good, easy to maintain web applications . Use Struts as a whole, the basic framework of the system, responsible for the separation of MVC, the model part of the Struts framework, control of the business to jump, use hibernate framework provides support for persistence, spring to do management, management Struts and hibernate.

2、Struts2

(1) Basic Concepts

Struts2 MVC design pattern is based on a web application framework, corresponding to a the servlet, the MVC design pattern, Struts2 as a controller (Controller) to establish interaction model with a data view. Struts2 in Struts1 fusion webwork. struts2 webwork to as the core, the interceptor mechanism to handle the user's request, so that the business logic controller design can be completely disengaged from the servletAPI.

Runtime structures (2) Struts2 frame

Analysis: The client sends a request (HttpServletRequest) to the server, the server receives a request would first enter the web.xml configuration file to see if there is not configured filters, filter Struts2 have found, and then find struts.xml configuration file, struts. xml configuration file to define an action, and then went to find the action class that inherits ActionSupport interface, and implements execute () method returns a string "success" to struts.xml profile, action profile struts.xml the default action will execute call class () method, result received the returned string, result will call your designated jsp page presents the results, the final response to the client.

(3) Struts2 advantage

  • Implements the MVC pattern, clear hierarchy, the programmer only needs to focus on business logic implementation.
  • Rich tag library, greatly improving the efficiency of development.
  • Struts2 provides a wealth of interceptors to achieve.
  • A profile can grasp the relationship between the various parts of the entire system.
  • Exception handling mechanism, you need to configure an exception mapping in the configuration file, you can make an exception for handling the response.
  • Struts2 high scalability.
  • Aspect-oriented programming ideas in Struts2 also been well represented.
  • Reflecting the use of the interceptor, the interceptor is a function of a small module, the user can use these interceptors into one large interceptor, the same as the synthesis of a single interceptor interceptor, as long as it is configured into a Action in on it.

(4) Struts2 disadvantages:

  • More cumbersome verification, error return different multiple-field.
  • Security is too low
  • More trouble getting mass participation

2、Spring

(1) Basic Concepts

spring is an open source development framework is a lightweight Inversion of Control (IoC) and the facing section (AOP) of the container frame.

spring is mainly used to develop java applications, building web applications J2EE platform. Its core is to provide a new mechanism for managing business objects and their dependencies.

A flowchart (2) spring of

Analysis: The above is added on the basis of the structure of FIG Struts a flowchart spring, the spring is added listener web.xml configuration file, the configuration file is added struts.xml

“<constant name="struts.objectFactory" value="spring" />”

是告知Struts2运行时使用spring来管理对象,spring在其中主要做的就是注入实例,所有需要类的实例都由spring管理。

(3)spring的优点

  • 容器:spring是一个容器,包含并管理对象的生命周期和配置。可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。
  • 支持AOP:spring提供对AOP的支持,它允许将一些通用任务,如安全、事物、日志等进行集中式处理,从而提高了程序的复用性。
  • 轻量级框架:spring是轻量级框架,其基本的版本大约2M。
  • 控制反转:spring通过控制反转实现松耦合。对象们给他们依赖,而不是对象本身,方便解耦,简化开发。
  • 方便程序测试:spring提供了Junit4的支持,可以通过注解方便的测试spring程序。
  • 降低java EE API的使用难度:spring对java EE开发中非常难用的一些API(比如JDBC),都提供了封装,使这些API应用难度大大降低。
  • 方便集成各种优秀框架:spring内部提供了对各种优秀框架(如Struts、mybatis)的直接支持。
  • 支持声明式事务处理:只需要通过配置就可以完成对事务的管理,而无须手动编程。

(4)spring的缺点

  • 依赖反射,反射影响进程。
  • 太过于依赖设计模式。
  • 控制器过于灵活。
  • 不支持分布式应用。

3、hibernate

(1)基本概念

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。

(2)hibernate的核心构成和执行流程图

(3)hibernate的优点

  • 对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。
  • Hibernate是一个优秀的ORM实现。他很大程度的简化DAO层的编码工作,将软件开发人员从大量相同的数据持久层相关编程工作中解放出来,使开发更对象化了。
  • 透明持久化(persistent)带有持久化状态的、具有业务功能的单线程对象,此对象生存期很短。这些对象可能是普通的javabeans/POJO,(POJO概念,plain ordinary java object,简单的java对象,可以简单理解为简单的实体类entity。)这个对象没有实现第三方框架或接口,唯一特殊的是他们正与session关联。一旦这个session被关闭,这些对象就会脱离持久化状态,这样就可被应用程序的任何层自由使用。
  • 事务transaction应用程序用来指定原子操作单元范围的对象,它是单线程的,生命周期很短。它通过抽象将应用从底层具体的JDBC、JTA(java transaction API,JTA允许应用程序执行分布式事务处理,在两个或多个网络计算机资源访问并且更新数据,JDBC驱动程序的JTA支持极大地增强了数据访问能力)以及CORBA(公用对象请求代理程序体系结构,common object request broker architecture,简而言之,CORB允许应用程序和其它的应用程序通讯)事务隔离开。某些情况下,一个session之内可能包含多个transaction对象,事务边界的开启与关闭时必不可少的。
  • 它没有侵入性,是轻量级框架。
  • 移植性好,支持各种数据库,如果换个数据库只要在配置文件中变换配置就可以了,不用改变hibernate代码。
  • 缓存机制,提供一级缓存和二级缓存。

一级缓存:是session级别的缓存,一个session做了一个查询操作,它会把这个操作的结果放到一级缓存中,如果短时间内这个session又做了同一个操作,那么hibernate直接从一级缓存中拿出,而不会去连数据库取数据。

二级缓存:是sessionFactory级别的缓存,就是查询的时候会把结果缓存到二级缓存中,如果同一个sessionFactory创建的某个session执行了相同的操作,hibernate就会从二级缓存中拿出结果,而不会再去连接数据库。

(4)hibernate的缺点

  • 持久层封装过于完整,导致开发人员无法对SQL进行优化,无法灵活应用原生SQL。
  • 批量数据处理的时候较为弱势。
  • 框架中使用ORM原则,导致配置过于复杂,遇到大项目,维护问题不断。

为什么很多人不愿意用hibernate了?

尚硅谷Java视频_SSH整合&综合案例 视频教程

手动实现教程源码:

链接: https://pan.baidu.com/s/1BK0V1wxA-GQrWco10WEzeg 提取码: 2e3e 

二、SSM

SSM架构,是三层结合所成的框架,分别是Spring、SpringMVC、MyBatis所组成。Spring依赖注入来管理各层,面向切面编程管理事务,日志和权限。SpringMVC代表了model、view、controller接收外部请求,进行开发和处理。mybatis是基于jdbc的框架,主要用来操作数据库,并且将业务实体和数据表联系起来。

1、spring

详细介绍见SSH中spring。

2、SpringMVC

(1)基本概念

属于spring框架的一部分,用来简化MVC架构的web应用程序开发。

(2)SpringMVC的优点

  • 拥有强大的灵活性,非侵入性和可配置性
  • 提供了一个前端控制器dispatcherServlet,开发者无需额外开发控制器对象
  • 分工明确,包括控制器、验证器、命令对象、模型对象、处理程序映射视图解析器,每一个功能实现由一个专门的对象负责完成
  • 可以自动绑定用户输入,并正确的转换数据类型
  • 可重用的业务代码:可以使用现有的业务对象作为命令或表单对象,而不需要去扩展某个特定框架的基类。

(3)SpringMVC的缺点

  • servlet API耦合难以脱离容器独立运行
  • 太过于细分,开发效率低

3、mybatis

(1)基本概念

mybatis是一个简化和实现了java数据持久层的开源框架,它抽象了大量的JDBC冗余代码,并提供了一个简单易用的API和数据库交互。

(2)mybatis的优点

  • 与JDBC相比,减少了50%以上的代码量。
  • mybatis是最简单的持久化框架,小巧并且简单易学。
  • mybatis灵活,不会对应用程序或者数据库的限售设计强加任何影响,SQL写在XML里,从程序代码中彻底分离,降低耦合度,便于统一管理和优化,可重用。
  • 提供XML标签,支持编写动态SQL语句(XML中使用if,else)。
  • 提供映射标签,支持对象与数据库的ORM字段关系映射(在XML中配置映射关系,也可以使用注解)

(3)mybatis的缺点

  • SQL语句的编写工作量较大,对开发人员的SQL语句编写有一定的水平要求。
  • SQL语句过于依赖数据库,不能随意更换数据库。
  • 拼接复杂SQL语句时不灵活。

三、Springboot

1、springboot基本概念

springboot是一个全新的框架,简化Spring的初始搭建和开发过程,使用了特定的方式来进行配置,让开发人员不再需要定义样板化的配置。此框架不需要配置xml,依赖于maven这样的构建系统。

2、Springboot的优点

(1)减少了大量的开发时间并提高了生产力

(2)避免了编写大量的样板代码,注释和XML配置

(3)解决了spring的弊端

(4)代码少了、配置文件少了、不需要对第三方框架烦恼了、项目精简了,对整个团队的开发和维护来说,更大的节约了成本。

3、springboot的缺点

(1)修复bug较慢,报错时难以定位。

(2)集成度较高,不易于了解底层。

4、springboot总结

简单、快速、方便的搭建项目;对主流开发框架的无配置集成;极大提高了开发、部署效率。

5、springboot和spring的区别

(1)springboot可以建立独立的spring应用程序。

(2)内嵌了如tomcat,Jetty和Undertow这样的容器,也就是说可以直接跑起来,用不着再做部署工作。

(3)无需再像spring那样写一堆繁琐的XML配置文件

(4)可以自动配置spring

(5)提供的POM可以简化maven的配置

6、springboot和springMVC的区别

(1)SpringMVC是基于spring的一个MVC框架。

(2)springboot的基于spring的条件注册的一套快速开发整合包。

素小暖讲Spring Boot

 

推荐博文

素小暖讲Spring JdbcTemplate

为什么很多人不愿意用hibernate了?

【原创】纯干货,Spring-data-jpa详解,全方位介绍。

 

发布了110 篇原创文章 · 获赞 8 · 访问量 6896

Guess you like

Origin blog.csdn.net/guorui_java/article/details/104018528