MyBatis Basic Primer (iv)-Integration with SPRINGMVC

Source: Internet
Author: User

The previous additions and deletions have not been incorporated into a Web project, which is based on the previous one, integrating spring management-related beans and integrating SPRINGMVC on the Web tier. There will also be the source of the download.

First, the directory structure:

Second, integrated spring:

Because it is a Web project, and the use of spring as a binder, the relevant jar package is not much explained, see the source code. The structure of the project's previous article, where the mapping file is placed separately into a directory, and the controller's package is added, and the contents of the map file are unchanged. Note that the other configuration files are integrated into the config package. Because of the use of spring, the data connection in Configuration.xml is moved to the Applicationcontext.xml file, which can, of course, be isolated to the Database.properties file.

Spring-managed transactions, MyBatis Sqlsessionfactorybean, and the map file package path properties of MyBatis to be scanned by the project, data sources, etc. are in the Applicationcontext.xml file:

[HTML] view Plaincopy

  1. <?xml version= "1.0" encoding= "Utf-8"?>

  2. <beans xmlns= "Http://www.springframework.org/schema/beans"

  3. Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

  5. xmlns:tx= "Http://www.springframework.org/schema/tx"

  6. xmlns:context= "Http://www.springframework.org/schema/context"

  7. xmlns:p= "http://www.springframework.org/schema/p"

  8. Xsi:schemalocation= "

  9. Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  10. HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  11. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

  12. Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

  13. Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "

  14. Default-autowire= "ByName" default-lazy-init= "false" >

  15. <!--This example uses the DBCP connection pool, you should pre-copy the DBCP jar package into the project's Lib directory. -

  16. <context:property-placeholder location= "Classpath:/config/database.properties"/>

  17. <!--<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource"

  18. Destroy-method= "Close" p:driverclassname= "Com.mysql.jdbc.Driver"

  19. P:url= "Jdbc:mysql://127.0.0.1:3306/mybatis?characterencoding=utf8"

  20. P:username= "Root"

  21. P:password= "123"

  22. P:maxactive= "10"

  23. P:maxidle= "Ten" >

  24. </bean>

  25. <bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >

  26. <property name= "DataSource" ref= "DataSource"/>

  27. </bean>

  28. <bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >

  29. <!--DataSource property specifies the connection pool to use--

  30. <property name= "DataSource" ref= "DataSource"/>

  31. <!--Configlocation property specifies the core configuration file for MyBatis--

  32. <property name= "configlocation" value= "Classpath:config/configuration.xml"/>

  33. <!--all configured mapper files--

  34. <property name= "mapperlocations" value= "Classpath*:com/tgb/mybatis/mapper/*.xml"/>

  35. </bean>

  36. <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >

  37. <property name= "Basepackage" value= "Com.tgb.mybatis.inter"/>

  38. </bean>

  39. </beans>


Third, integrated SPRINGMVC:

For the relevant jar package see source code, SPRINGMVC acts as a web-side framework, and mvc-dispatcher-servlet.xml and XML are related configurations.

Mvc-dispatcher-servlet.xml:

[HTML] view Plaincopy

  1. <beans xmlns= "Http://www.springframework.org/schema/beans"

  2. xmlns:context= "Http://www.springframework.org/schema/context"

  3. Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

  4. Xsi:schemalocation= "

  5. Http://www.springframework.org/schema/beans

  6. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  7. Http://www.springframework.org/schema/context

  8. Http://www.springframework.org/schema/context/spring-context-3.0.xsd

  9. Http://www.springframework.org/schema/mvc

  10. Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

  11. <context:component-scan base-package= "Com.tgb.controller"/>

  12. <mvc:annotation-driven/>

  13. <mvc:resources mapping= "/static/**" location= "/web-inf/static/"/>

  14. <mvc:default-servlet-handler/>

  15. <bean

  16. class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >

  17. <property name= "prefix" >

  18. <value>/WEB-INF/pages/</value>

  19. </property>

  20. <property name= "suffix" >

  21. <value>.jsp</value>

  22. </property>

  23. </bean>

  24. </beans>


Xml:

[HTML] view Plaincopy

  1. <?xml version= "1.0" encoding= "UTF-8"?>

  2. <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 ">

  3. <display-name>MyBatis_Spring_MVC</display-name>

  4. <welcome-file-list>

  5. <welcome-file>index.jsp</welcome-file>

  6. </welcome-file-list>

  7. <filter>

  8. <filter-name>encodingFilter</filter-name>

  9. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

  10. <init-param>

  11. <param-name>encoding</param-name>

  12. <param-value>UTF-8</param-value>

  13. </init-param>

  14. <init-param>

  15. <param-name>forceEncoding</param-name>

  16. <param-value>true</param-value>

  17. </init-param>

  18. </filter>

  19. <filter-mapping>

  20. <filter-name>encodingFilter</filter-name>

  21. <url-pattern>/*</url-pattern>

  22. </filter-mapping>

  23. <context-param>

  24. <param-name>contextConfigLocation</param-name>

  25. <param-value>classpath*:config/applicationContext.xml</param-value>

  26. </context-param>

  27. <listener>

  28. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  29. </listener>

  30. <listener>

  31. <listener-class>

  32. Org.springframework.web.context.contextcleanuplistener</listener-class>

  33. </listener>

  34. <!--MVC configuration--

  35. <servlet>

  36. <servlet-name>mvc-dispatcher</servlet-name>

  37. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  38. <load-on-startup>1</load-on-startup>

  39. </servlet>

  40. <servlet-mapping>

  41. <servlet-name>mvc-dispatcher</servlet-name>

  42. <url-pattern>/</url-pattern>

  43. </servlet-mapping>

  44. </web-app>

The boot order in Web. XML is: context-param-"listener-" filter-"servlet, if the servlet is configured with a Load-on-startup parameter (non-negative integer, with 0, And the smaller the higher the first boot) will be loaded into memory when the server is started.

Control layer:

Usercontroller, equivalent to the action in struts, because the use of annotations eliminates a bunch of configurations like struts, and personal feeling is convenient for development and maintenance:

[Java] view plaincopy

  1. @Controller

  2. @RequestMapping ("/article")

  3. public class Usercontroller {

  4. @Autowired

  5. Iuseroperation Usermapper;

  6. @RequestMapping ("/list")

  7. Public Modelandview Listall (httpservletrequest request,httpservletresponse response) {

  8. List<article> articles=usermapper.getuserarticles (1);

  9. Modelandview mav=new modelandview ("list");

  10. Mav.addobject ("articles", articles);

  11. return MAV;

  12. }

  13. }

Presentation layer:

The interface uses an EL expression to parse the returned data:

[HTML] view Plaincopy

    1. <body>

    2. <c:foreach items= "${articles}" var= "Item" >

    3. ${item.id}--${item.title}--${item.content}<br/>

    4. </c:forEach>

    5. </body>



Four, test:

Using TOMCAT7, after deployment, Access: Http://localhost:8080/MyBaits_Spring_MVC//article/list can see the following effect:

Note that the URL here, the address behind the port is configured in the controller, slightly different from struts.

Five, Summary:

Here is a relatively simple Web project, with the foreshadowing of the previous article, only need to add spring and MVC configuration, and add a controller.

MyBatis There are many other features, such as dynamic statements, automatic generation of code, related to some plug-ins, such as page plug-ins, with these foundations, will be in the subsequent articles, gradually post the relevant source code.

collation of the relatively rough, the code to share to everyone, " Source Address Get "

MyBatis Basic Primer (iv)-Integration with SPRINGMVC

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.