Learn the things to write here, forget to come back at any time to check, good memory than bad blog, haha, good nonsense not to say, this article mainly introduces the integration of SPRINGMVC and MyBatis
Note: SPRINGMVC version 3.1.2. MyBatis version 3.2.2 Database is Oracle First look at the configuration of 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:p= "http://www.springframework.org/schema/p" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/bea NS Http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/s Chema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/s CHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.or G/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:compone Nt-scan base-package= "Test.service" ></context:component-scan> <!--database connection resource file--<bean id= "Propertyconfigurer" class= "org.springframework.beans.factory . config. Propertyplaceholderconfigurer "> <property name=" Locations "> <list> <value>classpath:jdbc.pro
perties</value> </list> </property> </bean>
Very simple, just configure the @service support and database connection resource files, of course you can also add some other need to initialize the resource files;
2. Configuration of the Spring-mvc.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:p= "http://www.springframework.org/schema/p" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/bea NS Http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/s Chema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/s CHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.or
G/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--Controller Sweep-- <context:component-scan base-package= "Test.controlLer "></context:component-scan> <!--avoid ie return json appears download-<bean id=" Mappingjacksonhttpmessageconvert Er "class=" org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> <property name=" Supportedmediatypes "> <list> <value>text/html;charset-UTF-8</value> </list> </ Property> </bean> <!--enable the annotation function for spring MVC--<bean class= "Org.springframework.web.servlet.mvc.ann Otation. Annotationmethodhandleradapter "> <property name=" messageconverters "> <list> <!--JSON converter--&G
T
<ref bean= "Mappingjacksonhttpmessageconverter"/> </list> </property> </bean> <!--velocity configuration requires the Views/page folder to be created under Webinfo to hold the view--<bean id= "Velocityconfigurer" class= "ORG.SPRINGFR Amework.web.servlet.view.velocity.VelocityConfigurer "> <property name=" Resourceloaderpath "value="/web-inf/ views/page/"/> <Property name= "Velocityproperties" > <props> <prop key= "Resource.loader" >file</prop> <PR Op key= "Class.resource.loader.class" >org.apache.velocity.runtime.resource.loader.fileresourceloader</prop > <prop key= "velocimacro.library" ></prop> <prop key= "input.encoding" >UTF-8</prop> & Lt;prop key= "output.encoding" >UTF-8</prop> <prop key= "Default.contenttype" >text/html;charset=utf-8 </prop> <prop key= "File.resource.loader.cache" >false</prop> <prop key= "file.resource.loader.m
Odificationcheckinterval ">36000</prop> </props> </property> </bean> <!--view Resolution-- <bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.velocity.VelocityViewResolver" > < Property name= "Cache" value= "true"/> <property name= "prefix" value= "/> <property name=" suffix "value=". htm "/> <property name=" ContentType "ValUe= "Text/html;charset=utf-8"/> <property name= "Exposespringmacrohelpers" value= "true"/> <property name= "Exposerequestattributes" value= "true"/> <property name= "exposesessionattributes" value= "true"/> <prope Rty name= "Requestcontextattribute" value= "req"/> </bean> <!--spring configuration of uploaded files--<bean id= "multipart Resolver "class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "> <property name=" Defaultencoding "> <value>UTF-8</value> </property> <property name=" Maxuploadsize "> &L t;value>32505856</value> </property> <property name= "Maxinmemorysize" > <value>4096</ value> </property> </bean> </beans>
The implementation of MVC, which is primarily configured with annotations, can be used in code @controller, velocity as a view parsing Jackson to automatically convert objects to JSON strings
3.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:p= "http://www.springframework.org/schema/p" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/bea NS Http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/s Chema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/s CHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.or G/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--Data Source--& Lt;bean id= "DataSource" destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSource" > <property name= "driverclassname" value= "${ Oracle.driverclassname} "/> <property name=" url "value=" ${oracle.url} "/> <property name=" username "value= "${oracle.username}"/> <property name= "password" value= "${oracle.password}"/> </bean> <!--mybat IS--<bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= " DataSource "ref=" DataSource "></property> <!--Map file--
<property name= "mapperlocations" value= "Classpath:test/xml/*.xml" ></property>
</bean>
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<!--DAO Scanning--
< Property Name= "Basepackage" value= "Test.dao" ></property>
<property name= " Sqlsessionfactorybeanname "value=" sqlsessionfactory "></property>
</bean>
</beans>
4.web.xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 "> <display-name>springMVC+mybatis</display-name> <welcome-file-list> <wel come-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>co Ntextconfiglocation</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> </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> <servlet> <servlet-name>springMvc</servlet-name> <SERVLET-CLASS>ORG.SPRINGF Ramework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfigl Ocation</param-name> <param-value>/WEB-INF/classes/spring-mvc.xml</param-value> </init-para m> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-na me>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </ Web-app>
The frame is basically finished. Next, continue the specific use of SPRINGMVC.