SPRINGMVC MyBatis Integrated Configuration Example _java

Source: Internet
Author: User
Tags aop file upload log4j

Simple beauty, Springmvc,mybatis is a very good simple integration scheme, to meet the general requirements of the project. Spare time to share the project configuration file for everyone to see:

1. First we look at the dependent Pom:

<!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactid>sprin
  g-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> <artifact Id>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> <groupi D>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> &LT;GROUPID&GT;ORG.M ybatis</groupid> <artifactId>mybatis</artifactId> <version>3.2.8</version> </ dependency> <!--mybatis Spring plugin--> <dependency> <groupId>org.mybatis</groupId> <ar Tifactid>mybatis-spring</artifactid> <version>1.2.2</version> </dependency> <!--mysql connection--> <dependency> <groupid>mysql</gro upid> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </ Dependency> <!--data source--> <dependency> <groupId>com.alibaba</groupId> <artifactid>d ruid</artifactid> <version>1.0.12</version> </dependency> <dependency> &LT;GROUPID&G T;org.aspectj</groupid> <artifactId>aspectjweaver</artifactId> <version>1.8.4</ Version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <a Rtifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <!--servlet--&
 Gt
  <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> <g Roupid>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> <de Pendency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations< /artifactid> <version>${jackson.version}</version> </dependency> <dependency> <group Id>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--> <de Pendency> <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 is a 4.1.4 version, and we can choose the version that is right for you, depending on your system needs.

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 >

 <!--Introducing the configuration properties file-->
 <context:property-placeholder location= "Classpath:config.properties"/>

 <!-- An automatic scan contains @service to inject it into the bean-->
 <context:component-scan base-package= "Com.demo.report.web.service"/>

b) Spring-mvc.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 scans all classes under the controller package if @controller is injected as a bean--> <context:component-scan Base-package= "Com.demo.report.web.controller"/> <!--avoid IE to execute AJAX when the return JSON appears to download files--> <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 the annotation feature of spring MVC to complete the mapping of request and annotation Pojo--> <bean C lass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name= "
  Messageconverters "> <list> <!--JSON converter--> <ref bean=" Mappingjacksonhttpmessageconverter "/> </list> </property> </bean> <!--parsing the model view name by adding the prefix--> <bean class= to the Model view name Org.springfra Mework.web.servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value=" "/> <property name=" Suffix "value=" "/> </bean> <!--Configure multiple file uploads <bean id=" Multipartresolver "class=" Org.springframework.web. Multipart.commons.CommonsMultipartResolver "> <property name=" DefauLtencoding "> <value>UTF-8</value> </property> <property name=" Maxuploadsize "> <value& gt;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=" 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 > <!--the configuration data source uses the Druid data source--> <bean name= "Datasour Ce "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} "/> <!--initialization connection size--> <property name=" iniTialsize "value=" 0/> <!--connection pool maximum number of connections--> <property name= "maxactive" value= ""/> <!--connection Pool minimum Idle- -> <property name= "minidle" value= "0"/> <!--get connection maximum wait time--> <property name= "maxwait" value= "60000"/ > <property name= "poolpreparedstatements" value= "true",/> <property name= " Maxpoolpreparedstatementperconnectionsize "value="/> <!--used to detect valid SQL--> <property name= "ValidationQue Ry ' value= ' ${validationquery} '/> <property name= ' Testonborrow ' value= ' false '/> <property ' name= ' Testonreturn "value= false"/> <property name= "Testwhileidle" value= "true"/> <!--configuration interval to be tested once Detects idle connections that need to be turned off, in milliseconds--> <property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!-- Configure the minimum time to live a connection in the pool, in milliseconds--> <property name= "Minevictableidletimemillis" value= "25200000"/> <!-- Turn on the removeabandoned feature--> <property name= "removeabandoned" value= "true"/> <!--1800 seconds, which is 30 minutes--&GT <property name= "removeabandonedtimeout" value= "1800"/> <!--output error log when closing abanded connection--> <property " 

 Logabandoned "value=" true "/> <!--monitoring database--> <property name=" Filters "value=" Mergestat "/> </bean> <!--mybatis file--> <bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > &LT;PR Operty name= "DataSource" ref= "DataSource"/> <!--Automatic Scan entity directory, dispense configuration.xml manual configuration--> <property nam E= "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> <!--configuration 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> & Lt;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 listeners--> <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, per minute--> <session-config> <session-timeout>30</ session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</ Welcome-file> </welcome-file-list>

Using the Druid data source, the detailed configuration in the Web can refer to the code.

The above is the SPRINGMVC mybatis integrated configuration of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.