English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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 pom dependencies:
<!-- spring --> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-core</artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-beans/artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-context/artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-tx/artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-web/artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-webmvc/artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-jdbc/artifactId> <version>${spring.version}</version>/version> </dependency> <dependency> <groupId>org.springframework</groupId>/groupId> <artifactId>spring</artifactId>-test/artifactId> <version>${spring.version}</version>/version> <scope>test</scope>/scope> </dependency> <!-- mybatis package --> <dependency> <groupId>org.mybatis</groupId>/groupId> <artifactId>mybatis</artifactId>/artifactId> <version>3.2.8</version> </dependency> <!--mybatis spring plugin --> <dependency> <groupId>org.mybatis</groupId>/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> <!-- DataSource --> <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 of spring used is4.1.4version, according to the system requirements, we can choose a suitable version ourselves.
2The relevant configuration files are:
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 configuration property file --> <context:property-placeholder location="classpath:config.properties" /> <!--Automatically scan classes with @Service annotation and inject them as beans --> <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" /> <!-- Avoid downloading files when IE executes AJAX and returns 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 annotation features of Spring MVC, complete 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> <!-- Resolve model view names, that is, add prefixes and suffixes to model view names --> <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}"> /> <!-- Initial size of the connection pool --> <property name="initialSize" value="0"> /> <!-- Maximum number of connections used in the connection pool --> <property name="maxActive" value="20" /> <!-- Minimum number of idle connections in the connection pool --> <property name="minIdle" value="0"> /> <!-- The maximum waiting time to acquire a connection --> <property name="maxWait" value="60000" /> <property name="poolPreparedStatements" value="true"> /> <property name="maxPoolPreparedStatementPerConnectionSize"} value="33" /> <!-- Used to check 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 before a check is performed to determine which idle connections 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 files --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- Automatically scan the entity directory and omit 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 data source druid is used, you can refer to the code for the detailed configuration in the web.
The above-mentioned is the detailed explanation of the integration configuration example of springmvc and mybatis introduced by the editor to everyone, I hope it will be helpful to everyone. If you have any questions, please leave a message, the editor will reply to everyone in time. At the same time, I also thank everyone for their support of the Yelling Tutorial website!
Declaration: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been edited by human, nor does it assume relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.