English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Example of Spring MVC and MyBatis Integration Configuration

Simplicity is beauty, springmvc, mybatis is a good simple integration solution that can meet the needs of general projects. In my leisure time, I share the project configuration file for everyone to refer to:

1.First, let's take a look at the dependency in the pom file:

<!-- spring -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${spring.version}</version>
  <scope>test</scope>
 </dependency>
 <!-- Mybatis package -->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis/artifactId>
  <version>3.2.8</version>
 </dependency>
 <!--Mybatis spring plugin -->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.2.2</version>
 </dependency>
 <!-- MySQL connection -->
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
 </dependency>
 <!-- Data source -->
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.12</version>
 </dependency>
 <dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.4</version>
 </dependency>
 <!-- log4j -->
 <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
 </dependency>
 <!-- servlet -->
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>3.0-alpha-1</version>
 </dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
 </dependency>
 <!-- json -->
 <dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version>
 </dependency>
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.3</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>${jackson.version}</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>${jackson.version}</version>
 </dependency>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.version}</version>
 </dependency>
 <!-- File upload -->
 <dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
 </dependency>
 <dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.2.2</version>
 </dependency>

The version used by spring is4.1.4version, according to the system requirements, we can choose a suitable version for ourselves.

2.Related configuration files:

    a)spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 <!--Introduce the configuration property file -->
 <context:property-placeholder location="classpath:config.properties" />
 <!--Automatically scan and inject @Service as a bean -->
 <context:component-scan base-package="com.demo.report.web.service" />

    b)spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.1.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
 <!-- Automatically scan all classes under the controller package, if @Controller is injected as a bean -->
 <context:component-scan base-package="com.demo.report.web.controller" />
 <!-- Prevent IE from executing AJAX and downloading files when returning JSON -->
 <bean id="mappingJacksonHttpMessageConverter"
 class="org.springframework.http.converter.json.MappingJackson"2HttpMessageConverter">
 <property name="supportedMediaTypes">
  <list>
  <value>text/html;charset=UTF-8</value>
  </list>
 </property>
 </bean>
 <!-- Enable the annotation feature of Spring MVC, complete the mapping of requests and annotated POJOs -->
 <bean
 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
 <property name="messageConverters">
  <list>
  <!-- JSON converter -->
  <ref bean="mappingJacksonHttpMessageConverter" />
  </list>
 </property>
 </bean>
 <!-- The resolution of the model view name, that is, adding prefixes and suffixes to the model view name -->
 <bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewClass"
  value="org.springframework.web.servlet.view.JstlView" />
 <property name="prefix" value="" />
 <property name="suffix" value=""}} />
 </bean>
 <!-- Configure multi-file upload 
 <bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="defaultEncoding">
  <value>UTF-8</value>
 </property>
 <property name="maxUploadSize">
  <value>32505856</value>
 </property>
 <property name="maxInMemorySize">
  <value>4096</value>
 </property>
 </bean>-->
</beans>

  c)spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
 ">
 <!-- Configure the data source, using Druid data source -->
 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
 init-method="init" destroy-method="close">
 <property name="url" value="${jdbc.url}" />
 <property name="username" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />
 <!-- The size of the initial connection -->
 <property name="initialSize" value="0" />
 <!-- The maximum number of connections that the connection pool can use -->
 <property name="maxActive" value="20" />
 <!-- Minimum idle connections in the pool -->
 <property name="minIdle" value="0"> />
 <!-- Maximum waiting time to acquire a connection -->
 <property name="maxWait" value="60000" />
 <property name="poolPreparedStatements" value="true"> />
 <property name="maxPoolPreparedStatementPerConnectionSize">
  value="33" />
 <!-- Used to detect valid SQL -->
 <property name="validationQuery" value="${validationQuery}"> />
 <property name="testOnBorrow" value="false"> />
 <property name="testOnReturn" value="false"> />
 <property name="testWhileIdle" value="true"> />
 <!-- Configure the interval for how often to perform a check, to check for idle connections that need to be closed, in milliseconds -->
 <property name="timeBetweenEvictionRunsMillis" value="60000" />
 <!-- Configure the minimum survival time of a connection in the pool, in milliseconds -->
 <property name="minEvictableIdleTimeMillis" value="25200000" />
 <!-- Enable the removeAbandoned feature -->
 <property name="removeAbandoned" value="true"> />
 <!-- 1800 seconds, that is30 minutes -->
 <property name="removeAbandonedTimeout" value="1800" />
 <!-- Output error logs when closing abandoned connections -->
 <property name="logAbandoned" value="true"> />
 <!-- Monitor the database -->
 <property name="filters" value="mergeStat"> />
 </bean>
 <!-- myBatis file -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <!-- Automatically scan the entity directory, eliminating manual configuration in the Configuration.xml -->
 <property name="mapperLocations" value="classpath:com"/demo/report/web/mapper/*.xml" />
 </bean>
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.feidai.report.web.mapper" />
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 </bean>
 <!-- Configure transaction manager -->
 <bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
 </bean>

    d)web.xml

<display-name>springmvc_mybatis_demo</display-name>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
 </context-param>
 <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>true</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <listener
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- Prevent Spring memory overflow listener -->
 <listener
 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
 </listener>
 <servlet
 <description>spring mvc servlet</description>
 <servlet-name>rest</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
  classpath:spring-mvc.xml
  </param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>rest</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 <servlet
 <servlet-name>DruidStatView</servlet-name>
 <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>DruidStatView</servlet-name>
 <url-pattern>/druid/*</url-pattern>
 </servlet-mapping>
 <!-- Configure session timeout time, unit minutes -->
 <session-config>
 <session-timeout>30</session-timeout>
 </session-config>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

The druid data source is used, and the detailed configuration in the web can be referred to in the code.

 This is the summary of the materials for the integration configuration of springmvc mybatis. We will continue to supplement relevant materials, thank you all for your support to this site!

Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not bear the relevant legal liability. If you find any copyright-infringing content, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like