Guide to Integrating Spring Framework, Spring MVC, and MyBatis

This article provides a step‑by‑step tutorial on building a Spring container, configuring Spring MVC with DispatcherServlet, setting up essential beans such as multipartResolver, custom handler mapping, handler adapter, view resolver, component scanning, and finally integrating MyBatis with SqlSessionFactoryBean and SqlSessionTemplate for a complete Java backend solution.

Java Captain
Java Captain
Java Captain
Guide to Integrating Spring Framework, Spring MVC, and MyBatis

The article explains how to assemble a Spring container by registering core listeners in <web.xml>, including ContextLoaderListener, RequestContextListener, and IntrospectorCleanupListener, and by adding a CharacterEncodingFilter to handle UTF‑8 encoding.

It then shows the registration of org.springframework.web.servlet.DispatcherServlet as the front controller, with its own spring-mvc.xml configuration file, using a servlet mapping like *.do.

Key beans defined in spring-mvc.xml include a multipartResolver for file uploads, a custom handlerMapping that extends RequestMappingHandlerMapping, and a handlerAdapter that configures message converters, a conversion service for date handling, and a customized Jackson ObjectMapper to format dates as strings.

The view layer is set up with an InternalResourceViewResolver that resolves JSP pages using the prefix / and suffix .jsp, and the configuration enables component scanning for @Controller, @RestController, and @ControllerAdvice annotated classes in the io.flysium package.

Finally, the guide demonstrates MyBatis integration by declaring SqlSessionFactoryBean (pointing to a data source and MyBatis config file) and SqlSessionTemplate beans, completing the end‑to‑end backend setup.

Optional configurations such as LocaleResolver, ThemeResolver, HandlerExceptionResolver, and additional view resolvers are mentioned for further customization.

Code snippets:

<!-- spring配置文件开始 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:webconfig/service-all.xml</param-value>
</context-param>
<listener>
    <!-- Spring负责监听web容器启动和关闭的事件 -->
    <!-- Spring ApplicationContext载入 -->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <!-- Spring监听HTTP请求事件 -->
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <!-- Spring 刷新Introspector防止内存泄露 -->
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>false</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring mvc配置开始 -->
<servlet>
    <servlet-name>Spring-MVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring-MVC</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- spring mvc配置结束 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8"/>
    <property name="maxUploadSize" value="104857600"/>
    <property name="maxInMemorySize" value="40960"/>
</bean>
<bean id="handlerMapping" class="io.flysium.framework.web.servlet.mvc.method.annotation.CustomHandlerMapping">
    <property name="order" value="-1"/>
</bean>
<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="order" value="-1"/>
    <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter"/>
        </list>
    </property>
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService">
                <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
            </property>
        </bean>
        <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" name="jacksonObjectMapper">
            <property name="featuresToDisable">
                <array>
                    <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
                </array>
            </property>
            <property name="simpleDateFormat" value="yyyy-MM-dd HH:mm:ss"/>
        </bean>
    </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<context:component-scan base-package="io.flysium" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="sqlSessionFactory"/>
</bean>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentspringMyBatisSpring MVCXML Configuration
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.