The beauty of simplicity, springmvc, mybatis is a good simple integration solution that can meet general project needs. Share the project configuration files in your spare time for your reference:
1. First, let’s take a look at the dependency pom:
<!-- 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><de pendency><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><dependent cy><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><gr oupId>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 plug-in--><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>spring uses version 4.1.4, and we can choose the version that suits us according to the needs of the system.
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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><!--Introducing configuration properties file--><context:property-placeholder location="classpath:config.properties" /><!--Automatic scan containing @Service injects it into 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!-- Automatically scan all classes under the controller package. If @Controller injected as bean --><context:component-scan base-package="com.demo.report.web.controller" /><!-- Avoid download file when IE executes AJAX, return to JSON--><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!-- Start Spring MVC annotation function to complete the mapping of requests and annotations POJOs--><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><!-- json converter--><ref bean="mappingJacksonHttpMessageConverter" /></list></property></bean><!-- Resolve the model view name, that is, add prefix to the model view name --><beanclass="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 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><!-- The data source is configured using the Druid data source--><bean name="dataSource"init-method="init" destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- Initialize the connection size--><property name="initialSize" value="0" /><!-- Maximum number of connections used in the connection pool--><property name="maxActive" value="20" /><!-- Minimum free connections--><property name="minIdle" value="0" /><!-- Get the maximum waiting time for 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 how long it takes to perform a test to detect the idle connection that needs to be closed, in milliseconds --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- Configure the minimum time for a connection to survive in the pool, in milliseconds --><property name="minEvictableIdleTimeMillis" value="25200000" /><!-- Turn on removeAbandoned function-><property name="removeAbandoned" value="true" /><!-- 1800 seconds, that is, 30 minutes --><property name="removeAbandonedTimeout" value="1800" /><!-- Output error log when closing the abandoned connection--><property name="logAbandoned" value="true" /><!-- Monitoring database--><property name="filters" value="mergeStat" /></bean><!-- myBatis file--><bean id="sqlSessionFactory"><property name="dataSource" ref="dataSource" /><!-- Automatically scan the entity directory, eliminating manual configuration in Configuration.xml--><property name="mapperLocations" value="classpath:com/demo/report/web/mapper/*.xml" /></bean><bean><bean><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</p aram-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 from 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><servle t-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>
Used the druid data source, please refer to the code for detailed configuration in the web.
The above is a detailed explanation of the integrated configuration example of springmvc and mybatis introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for the Wulin Network website!