Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Thinking of Spring4+SpringMVC+MyBatis integration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This paper mainly briefly explains the idea of framework integration.

1. The construction of Spring framework

This is simple, just register org.springframework.web.context.ContextLoaderListener in the web container and specify spring to load the configuration file, and the spring container is built. (of course, the core jar package of org.springframework needs to be introduced)

Of course, in order to make it easier to use and support J2EE applications, we usually add the following:

Spring listens for HTTP request event: org.springframework.web.context.request.RequestContextListener

ContextConfigLocation

Classpath:webconfig/service-all.xml

Org.springframework.web.context.ContextLoaderListener

Org.springframework.web.context.request.RequestContextListener

Org.springframework.web.util.IntrospectorCleanupListener

EncodingFilter

Org.springframework.web.filter.CharacterEncodingFilter

Encoding

UTF-8

ForceEncoding

False

EncodingFilter

/

2. The construction of Spring MVC

First of all, we know that the core of Spring MVC is org.springframework.web.servlet.DispatcherServlet, so it is necessary to register it in the web container. (of course, org.springframework 's web, mvc packages and their dependencies on jar packages need to be introduced.)

Spring-MVC

Org.springframework.web.servlet.DispatcherServlet

ContextConfigLocation

Classpath:spring/spring-mvc.xml

one

Spring-MVC

.do

At the same time, in order to better use MVC,spring-mvc.xml, you need to configure the following:

1) (optional) Multi-part request parser (MultipartResolver) configuration. Class libraries commons-io and commons-fileupload are required for uploading files.

2) (optional) localize (LocaleResolver) configuration

3) (optional) topic parser (ThemeResolver) configuration

4) (required) processor mapper (HandlerMapping) configuration, which can be configured with multiple configurations, usually using RequestMappingHandlerMapping or custom

Here we customize a processor mapper that inherits and overrides RequestMappingHandlerMapping and supports @ RequestMapping to automatically load class names or methods as url path matches without any path parameters.

CustomHandlerMapping implementation: @ Overridebr/ > @ Override

RequestMappingInfo info = createRequestMappingInfoDefault (method)

If (info! = null) {

RequestMappingInfo typeInfo = createRequestMappingInfoDefault (handlerType)

If (typeInfo! = null)

Info = typeInfo.combine (info)

}

Return info

}

Private RequestMappingInfo createRequestMappingInfoDefault (AnnotatedElement element) {

RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation (element

RequestMapping.class)

RequestCondition condition = (element instanceof Class)

? GetCustomTypeCondition ((Class) element)

: getCustomMethodCondition ((Method) element)

/ * *

Map the request with class name and method name, refer to @ RequestMapping

No parameters need to be added by default (for example: / className/methodName.do)

, /

String defaultName = (element instanceof Class)

? (Class) element) .getSimpleName ()

: ((Method) element) .getName ()

Return requestMapping = = null

? Null

: createRequestMappingInfo (requestMapping, condition, defaultName)

}

Protected RequestMappingInfo createRequestMappingInfo (RequestMapping annotation

RequestCondition customCondition, String defaultName) {

String [] patterns = resolveEmbeddedValuesInPatterns (annotation.value ())

If (patterns! = null & & (patterns.length = = 0)) {

Patterns = new String [] {defaultName}

}

Return new RequestMappingInfo (

New PatternsRequestCondition (patterns, getUrlPathHelper (), getPathMatcher ()

This.useSuffixPatternMatch, this.useTrailingSlashMatch

This.fileExtensions)

New RequestMethodsRequestCondition (annotation.method ())

New ParamsRequestCondition (annotation.params ())

New HeadersRequestCondition (annotation.headers ())

New ConsumesRequestCondition (annotation.consumes (), annotation.headers ())

New ProducesRequestCondition (annotation.produces (), annotation.headers ())

This.contentNegotiationManager)

CustomCondition)

}

5) (required) processor adapter (HandlerAdapter) configuration, can be configured multiple, mainly to configure messageConverters, its main function is to map the foreground to participate in handler processing method parameters. Generally extend RequestMappingHandlerAdapter, or customize. If we need to handle json requests, we must extend them here. At the same time, we need to pay attention to the conversion of date format.

In addition, Spring 4.2 new features, coupled with the annotation will automatically inject @ ControllerAdvice, you can define RequestBodyAdvice, ResponseBodyAdvice, you can more easily customize the parameters.

Yyyy-MM-dd HH:mm:ss

Text/html;charset=UTF-8

Application/json;charset=UTF-8

6) (optional) Handler exception parser (HandlerExceptionResolver) configuration, you can configure more than one. You can customize how to handle Controller exceptions after they are thrown. Generally, you need to log or give feedback.

7) (optional) request to View name translator (RequestToViewNameTranslator) configuration, which RequestToViewNameTranslator can use to get viewName based on Request when the View returned by the processor is empty.

8) (optional) View parser (ViewResolver) configuration, which can be configured to define the prefix and suffix of the redirected file, and the view mode configuration, which mainly aims at the view path resolution when @ Controller returns ModelAndView, and adds a prefix and suffix to the string of the method return behind the controller to become an available url address.

Finally, add component scanning to Controller to reduce xml configuration and add comments directly to the Java code.

3. Mybatis integration

To integrate mybatis into Spring framework, we need mybatis jar package and mybatis-spring integration jar package. Then register the configuration org.mybatis.spring.SqlSessionFactoryBean (requires a data source and specify a Mybatis configuration file) and org.mybatis.spring.SqlSessionTemplate in the Spring container.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report