La beauté de la simplicité, SpringMVC, Mybatis est une bonne solution d'intégration simple qui peut répondre aux besoins généraux du projet. Partagez les fichiers de configuration du projet pendant votre temps libre pour votre référence:
1. Premièrement, jetons un coup d'œil à la dépendance Pom:
<! - printemps -> <dependency> <proupId> org.springFramework </rombasid> <Ertifactid> printemps-core </ artifactid> <version> $ {printemps.version} </ version> </pedidency> <dependency> <proupId> org.springFramework </prowprid> <ErtifactId> Spring-Beans </ ArtifactId> <DERSE> $ {printemps.version} </-version> </dependency> <de Pendency> <ProupId> org.springFramework </rom grouped> <ArtefactId> Spring-Context </ ArtifactId> <DERNÉRATEUR> $ {Spring.Version} </DERNIERDE> </DENDENCENCE> <dep Endence> <proupId> org.springFramework </proupId> <Artifactid> Spring-Tx </RetifActid> <DERSE> $ {printemps.version} </ version> </pedency> <Depended cy> <proupId> org.springframework </rom grouped> <ArtefactId> printemps-web </ artifactid> <version> $ {printemps.version} </ version> </pedidency> <dependency> < GroupID> org.springFramework </prôdId> <ArtifactId> printemps-webmvc </ artifactId> <DERSE> $ {printemps.version} </ version> </pependance> <dependency> <group oupid> org.springframework </proupId> <Artifactid> printemps-jdbc </ artifactId> <version> $ {printemps.version} </ version> </pependance> <dependency> <groupe d> org.springframework </prouverId> <ArtefactId> printemps-test </ artifactId> <DERSE> $ {printemps.version} </ version> <ccope> Test </ccope> </Dependency> <! - Package Mybatis -> <dependency> <proupId> org.mybatis </rombandid> <ArtefactId> Mybatis </ artifactid> <version> 3.2.8 </ version> </dependency> <! - Mybatis Spring Plug-in -> <dependency> <proupId> org.mybatis </rombandid> <ArtefactId> Mybatis-Spring </refactive> <version> 1.2.2 </ version> </dependency> <! - MySQL Connection Connection -> <dependency> <proupId> mysql </proupId> <ErtifactId> MySQL-Connector-Java </ ArfactId> <Derson Source -> <dependency> <GroupId> com.alibaba </rompuprid> <letifactive> druid </ artifactive> <version> 1.0.12 </ version> </ Dependency> <Dedency> <ProupId> org.Aspectj </prouprid> <ptifactid> AspectjWeaver </Rfactid> <version> 1.8.4 </ version> </dedency> <! - Log4j -> <dependency> <proupId> Log4j </proupId> <Artifactid> LOG4J -> <dependency> <groupId> javax.servlet </prôdId> <Artifactid> servlet-api </lefactive> <version> 3.0-alpha-1 </ version> </dependency> <dependency> <proupId> javax.servlet </proupId> <pretifactid> JSTL </ artifactid> <version> 1.2 </ version> -> <dependency> <proupId> org.codehaus.jackson </rombasid> <ArtifactId> Jackson-Mapper-ASL </ artifactid> <version> 1.9.13 </-version> </dependency> <dependency> <proupId> com.alibaba </ grou pid> <artifactId> FastJson </ artifactid> <version> 1.2.3 </-version> </pependance> <dependency> <proupId> com.fasterxml.jackson.core </proupId> <Artifactid> jackson-Annotations </ artifactid > <version> $ {jackson.version} </ version> </dependency> <dependency> <proupId> com.fasterxml.jackson.core </prouverid> <lefactive> jackson-core </lefactive> <in version> $ {jackson.version} < / Version></Dependency><Dependance><groupId>com.fasterxml.jackson.core</groupId><artifActid>jackson-Databind</artifActid><Version>$ nos; Fichier Upload -> <Dedency> <ProupId> Commons-io </proupId> <Artifactid> Commons-io </ ArtiFactId> <Dersion> 2.4 </DERNIFRIENT> </ Dependency> <Dendency> <ProupId> Commons-Fileupload </prouprid> <ArtifActid> Commons-Fileupload </ artiFactId>Spring utilise la version 4.1.4, et nous pouvons choisir la version qui nous convient en fonction des besoins du système.
2. Fichiers de configuration connexes:
a) printemps.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.s pringframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><! - - introduction Fichier de propriétés de configuration -> <contexte: propriété-placeholder location = "classPath: config.properties" /> <! - Scan automatique contenant @Service l'injecte dans bean -> <context: composant-scan Base-Package = "com.demo.report.web.service" />
b) printemps-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"><! Si @Controller injecté sous le nom de Bean -> <Context: Component-Scan Base-Package = "com.demo.Report.web.Controller" /> <! - Évitez le fichier de téléchargement lorsque IE exécute Ajax, retournez à JSON -> <Bean id = "mappingjacksonhttpmessageConverter" class = "org.springframework.http.converter.json.mappingjackson2httpmessageconverter"> <propriété name = "supportDediaTypes"> <s list> <value> text / html; charset = utf-8 </ value> Démarrez la fonction d'annotation MVC Spring pour compléter le mappage des demandes et annotations Pojos -> <anEplass = "org.springframework.web.servlet.mvc.annotation.annotationMethodHandlerAdapter"> <propriété name = "MessageConverters"> <liste> <! - JSON Converter -> <Ref bean = "mappingjacksonhttpmessageconverter" /> </sist> </ propriété> </ank> <! - Résolve le nom de vue du modèle, c'est-à-dire le préfixe au nom du modèle -> <anterclass = "org.springframework.web.servlet.view.internalresourceviewResolver"> <propriété name = "ViewClass" value = "org.springframework.web.servlet.view.jstlview" /> <propriété name = "prefix" value = "" /> <propriété name = "suffixe" value = "" /> </ bean> <! - Configurer le multi-file upload <bean id = "multipartResolver" class = "org.springframework.web.multupart.commons.commonsmultipartreSolver"> <propriété name = "Defsencoding"> <value> utf-8 </value> </ propriété> <propriété name = "MAXUPLOADSIZE"> <Value> 32505856 </value> </ Property> name = "MaxinMemorySize"> <value> 4096 </value> </prophed> </bant> -> </bans>
c) printemps-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: schemalation = "http://www.spring 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.xsdd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><<! name = "dataSource" init-method = "init" destroy-méthod = "close"> <propriété name = "url" value = "$ {jdbc.url}" /> <propriété name = "username" value = "$ {jdbc.passwame}" /> <propriété name = "mot de passe" name = "initialSize" value = "0" /> <! - Nombre maximum de connexions utilisées dans le pool de connexions -> <propriété name = "maxactive" value = "20" /> <! - Minimum Free Connections -> <propriété name = "MINIDLE" Value = "0" /> <! - Obtenez le temps d'attente maximum pour la connexion -> <propriété name = "maxwait" value = "60000" /> <propriété = "PoolParsed" Value = "60000" /> <prewetwame Value = "true" /> <propriété name = "MaxPoolPreparedStationPerConnectionsize" value = "33" /> <! - Utilisé pour détecter SQL valide -> <propriété name = "ValidationQuery" value = "$ {validationQuery}" /> < name = "Test WhiteIdle" value = "true" /> <! - Configurez le temps qu'il faut pour effectuer un test pour détecter la connexion inactive qui doit être fermée, en millisecondes -> <propriété name = "TimeBetweenvictionRunsmillis" Value = "60000" /> <! name = "MineVictableIdleTimemillis" value = "25200000" /> <! - Activez la fonction de removeAbandOned -> <propriété name = "removeAbandOned" value = "true" /> <! - 1800 secondes, c'est-à-dire 30 minutes -> <propriété name = "reposanbandonEdTimeout" Value = "1800" /> <! - name = "logabandoned" value = "true" /> <! - Base de données de surveillance -> <propriété name = "filters" value = "Mergestat" /> </Ean> <! - MyBatis Fichier -> <bean id = "sqlSessionFactory"> <propriété name = "dataSource" Réfr. "DataSource" /> <! Configuration.xml -> <propriété name = "MapperLocations" value = "classPath: com / Demo / Report / web / Mapper / *. Xml" /> </ Bean> <Eban> <Eban> <propriété name = "Basepackage" value = "com.feidai.report.web.mapper" /> <propriété Name = "sqlSessionfactoryBeanname" Value = "Sqlf /> </ bean> <! - Configurer le gestionnaire de transaction -> <bean id = "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionManager"> <propriété name = "datasource" ref = "dataSource" /> </any>d) web.xml
<splay-name> springmvc_mybatis_demo </ display-name> <ftext-param> <param-name> contextConfigLocation </ param-name> <param-valeur> classpath: printemps.xml, classpath: spring-mybatis.xml </ param-val UE> </ Context-Param> <filter> <Imter-Name> EncodingFilter </filter-Name> <Filter-Class> org.springframework.web.filter.characterencodingFilter </filter-Class> <Init-Param> <param-name> Encoding </polter aram-name> <param-valeur> utf-8 </onsam-valie> </ init-param> <itnit-param> <param-name> forceencoding </onsam-name> <param-valeur> true </ paramter-name> </nitt-param> </filter> <filter-mapping> <filter-named-name > EncodingFilter </ Filter-Name> <Url-Pattern> / * </ url-Pattern> </ Filter-Mapping> <Dounerner> <Usiner-Class> org.springFramework.web.Context.ContextLoaderListener </ Louner-Class> </ounerner> <! - Empêchez la mémoire de ressort du débordement de l'écouteur -> <ouvoir> <écouteur-classe> org.springframework.web.util.introspectorcleanupListener </ auteur-class> </diner> <vrlet> <description> printemps mvc Servlet </, description> <servlet-name> repos </vrlett-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </vrlet-class> <Init-Param> <param-name> Conte xtConfiglocation </ param-name> <param-Value> CLASSPATH: Spring-Mvc.xml </ Param-Value> </Init-Param> <Chosing-on-Startup> 1 </ Load-on-Startup> </Servlet> <Servlet-Mapping> <Servle T-name> rest </ servlet-name> <url-potern> / </url-potern> </ servlet-mapping> <Servlet> <Servlet-Name> DruidStatView </ Servlet-name> <Servlet-Class> com.alibaba.druid.support .http.statViewServlet </ Servlet-Class> </ Servlet> <Servlet-Mapping> <Serplet-Name> DruidStatView </ Servlet-Name> <Url-Pattern> / Druid / * </ url-motif> </ servlet-mapping> <! - Configurer le temps d'attente de la session, Minutes de l'unité -> <session-Config> <session-timeout> 30 </ session-timeout> </ session-Config> <Welcome-File-list> <leen welcome-file> index.jsp </venke-file> </ welcome-file-list>
Utilisé la source de données Druid, veuillez vous référer au code pour une configuration détaillée sur le Web.
Ce qui précède est une explication détaillée de l'exemple de configuration intégré de SpringMVC et MyBatis présenté par l'éditeur. J'espère que cela vous sera utile. Si vous avez des questions, laissez-moi un message. L'éditeur vous répondra à temps. Merci beaucoup pour votre soutien pour le site Web du réseau Wulin!