Spring MVC + MyBatis Integration Framework

Source: Internet
Author: User
Tags connection pooling


First put a few of the package (note: The following two Mybatis-spring-1.1.1.jar Mybatis-3.1.1.jar is because not import these two packets of SQL will not print in the console, feel a bit inconvenient when debugging, do not need to have nothing to do)


Here are a few more packages are used to use JSON, you can directly return the string on the Web page, if you do not import the direct access method, it will return a string name of the JSP


The file directory is as follows:



Then there are a few configuration files

Applicationcontext-dao.xml

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.s Pringframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www. Springframework.org/schema/mvc/spring-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www . springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http: Www.springframework.org/schema/aop/spring-aop-3.2.xsd Http://www.springframework.org/schema/tx HTTP://WW W.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--loading db.prOperties the contents of the file, the key in the Db.properties file must have some special rules--<context:property-placeholder location= "classpath: Db.properties "/> <!--configuring data sources, using DBCP connection pooling--<bean id=" DataSource "class=" org.apache.commons.dbcp.b            Asicdatasource "destroy-method=" Close "> <property name=" driverclassname "value=" ${jdbc.driver} "/> <property name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/&            Gt            <property name= "Password" value= "${jdbc.password}"/> <property name= "maxactive" value= "/>" <property name= "Maxidle" value= "5"/> </bean> <!--configuration Sqlsessionfactory--<            Bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <!--data Source- <property name= "DataSource" ref= "DataSource"/> <!--load MyBatis Global configuration file--<property NA Me= "Configlocation" Value= "Classpath:mybatis/sqlmapconfig.xml"/> </bean> <!--configuration Mapper scanner--<bean C            lass= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <!--Scan package path If you need to scan multiple packages in the middle with a comma-separated half-width <property name= "Basepackage" value= "Com.cqut.music.dao"/> <!--This cannot be used ref= "sqlsessionfactory" because the load above Configuration file causes this reference to be error-<property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory"/> </ Bean></beans>

Applicationcontext-service.xml

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.s Pringframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www. Springframework.org/schema/mvc/spring-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www . springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http: Www.springframework.org/schema/aop/spring-aop-3.2.xsd Http://www.springframework.org/schema/tx HTTP://WW W.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--scan callout @rePository Annotated Service--<context:component-scan base-package= "Com.cqut.music.service"/></beans> 
Applicationcontext-transaction.xml
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.s Pringframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www. Springframework.org/schema/mvc/spring-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www . springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http: Www.springframework.org/schema/aop/spring-aop-3.2.xsd Http://www.springframework.org/schema/tx HTTP://WW W.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--transaction manager for MybaTIS operations database Transaction control, Spring uses the JDBC transaction control class--<bean id= "TransactionManager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <!--data source DataSource configured in Applicationcontext-dao.xml-<property name= "DataSource" ref= "DataSource"/> </bean > <!--notifications-<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" > <tx:attrib Utes> <!--propagation behavior--<tx:method name= "save*" propagation= "REQUIRED"/> <             Tx:method name= "delete*" propagation= "REQUIRED"/> <tx:method name= "insert*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "REQUIRED"/> <tx:method name= "find*" propagation= "S            Upports "read-only=" true "/> <tx:method name=" get* "propagation=" SUPPORTS "read-only=" true "/> <tx:method name= "select*" propagation= "SUPPORTS" read-only= "true"/> </tx:attRibutes> </tx:advice> <!--AOP--<aop:config> <aop:advisor advice-ref= "Txadvice" pointcut= "Execution (* com.cqut.music.service.*.* (..))"/> </aop:config></beans>

Mvc-dispatcher-servlet.xml

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.s Pringframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www. Springframework.org/schema/mvc/spring-mvc-3.2.xsd Http://www.springframework.org/schema/context http://www . springframework.org/schema/context/spring-context-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http: Www.springframework.org/schema/aop/spring-aop-3.2.xsd Http://www.springframework.org/schema/tx HTTP://WW W.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--component Scanner scan this layer groupTo scan the processor--<context:component-scan base-package= "Com.cqut.music.controller" ></context:component-scan > <!--configuration notes for mappers and adapters and other configurations--<mvc:annotation-driven></mvc:annotation-driven> <!--handle static assets Source Problem-<mvc:default-servlet-handler/> <!--configuration View Resolver--<bean class= "Org.springframewor K.web.servlet.view.internalresourceviewresolver "> <property name=" prefix "value="/web-inf/"/> < Property name= "suffix" value= ". jsp"/> </bean></beans>

Sqlmapconfig.xml



<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration>    <!--global setting configuration, add-on as needed-    <!--configuration Aliases--    <typeAliases>        <!--batch Scan settings alias--        <package name= "com.cqut.music.entity"/>    </typeAliases>    <!-- Configuration mapper        Note: Due to the mapper scan using the Spring Consolidated Mybtais integration Package, there is no need to configure the following        : Mapper.xml and Mapper.java files have the same name and are in the same directory        < Mappers></mappers>     --></configuration>


of the Db.properties database

Jdbc.driver=com.mysql.jdbc.driver
Jdbc.url=jdbc:mysql://localhost:3306/music
Jdbc.username=root
Jdbc.password=root

Log4j.properties Print the log, it is recommended to see the log4j log how to configure, as needed to

# # # set log levels # # #
Log4j.rootlogger = INFO, stdout


# # # Export Console # # #
Log4j.appender.stdout = Org.apache.log4j.ConsoleAppender
Log4j.appender.stdout.Target = System.err
Log4j.appender.stdout.layout=org.apache.log4j.simplelayout


Log4j.logger.com.ibatis=debug
Log4j.logger.com.ibatis.common.jdbc.simpledatasource=debug
Log4j.logger.com.ibatis.common.jdbc.scriptrunner=debug
Log4j.logger.java.sql.connection=debug
Log4j.logger.java.sql.statement=debug
Log4j.logger.java.sql.preparedstatement=debug

The last one left, 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>Music</display-name> <welcome-file-list> <welcome-fi        Le>index.jsp</welcome-file> </welcome-file-list> <!--loading Spring containers--<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationcon Text-*.xml</param-value> </context-param> <!--support restful-style request URLs--<filter> < Filter-name>hiddenhttpmethodfilter</filter-name> <filter-class> Org.springframework.web.filter.hiddenhttpmethodfilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-patter N>/*</url-pattern> </filter-mapping> <!--filtering Chinese characters--<filter> &LT;FILTER-NAME&G T Characterencodingfilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> &L        T;url-pattern>/*</url-pattern> </filter-mapping> <!--Spring Container listener--<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </ listener> <!--Configuring front-end controllers-<servlet> <servlet-name>mUsic</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class> <!--load Front Controller profile context configuration location-<init-param> &LT;PARAM-NAME&GT;CONTEXTC Onfiglocation</param-name> <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value > </init-param> <!--with server boot-up <load-on-startup>1</load-on-startup> &L T;/servlet> <servlet-mapping> <servlet-name>Music</servlet-name> <!--Res Tful style URL--<url-pattern>/</url-pattern> </servlet-mapping></web-app>

Test is configured successfully: After writing the controller to see if you can use @controller this annotation, write a method here to test

After run server, to see if the log logs are printed correctly, I remember this should happen.


Try the test controller I wrote below:



Run:


This is finished, need to use what function to import the package, although it is easy to know Maven, but I tried before the time is always a problem, decided to use the form of import package, do not abandon me out

Spring MVC + MyBatis Integration Framework

Related Article

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.