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
<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:p= "http://www.springframework.org/schema/p"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "
Default-autowire= "ByName" default-lazy-init= "false" >
<!--This example uses the DBCP connection pool, you should pre-copy the DBCP jar package into the project's Lib directory. -
<context:property-placeholder location= "Classpath:/config/database.properties"/>
<!--<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource"
Destroy-method= "Close" p:driverclassname= "Com.mysql.jdbc.Driver"
P:url= "Jdbc:mysql://127.0.0.1:3306/mybatis?characterencoding=utf8"
P:username= "Root"
P:password= "123"
P:maxactive= "10"
P:maxidle= "Ten" >
</bean>
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<!--DataSource property specifies the connection pool to use--
<property name= "DataSource" ref= "DataSource"/>
<!--Configlocation property specifies the core configuration file for MyBatis--
<property name= "configlocation" value= "Classpath:config/configuration.xml"/>
<!--all configured mapper files--
<property name= "mapperlocations" value= "Classpath*:com/tgb/mybatis/mapper/*.xml"/>
</bean>
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "Com.tgb.mybatis.inter"/>
</bean>
</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
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<context:component-scan base-package= "Com.tgb.controller"/>
<mvc:annotation-driven/>
<mvc:resources mapping= "/static/**" location= "/web-inf/static/"/>
<mvc:default-servlet-handler/>
<bean
class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" >
<value>/WEB-INF/pages/</value>
</property>
<property name= "suffix" >
<value>.jsp</value>
</property>
</bean>
</beans>
Xml:
[HTML] view Plaincopy
<?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>MyBatis_Spring_MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>encodingFilter</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>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
Org.springframework.web.context.contextcleanuplistener</listener-class>
</listener>
<!--MVC configuration--
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</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
@Controller
@RequestMapping ("/article")
public class Usercontroller {
@Autowired
Iuseroperation Usermapper;
@RequestMapping ("/list")
Public Modelandview Listall (httpservletrequest request,httpservletresponse response) {
List<article> articles=usermapper.getuserarticles (1);
Modelandview mav=new modelandview ("list");
Mav.addobject ("articles", articles);
return MAV;
}
}
Presentation layer:
The interface uses an EL expression to parse the returned data:
[HTML] view Plaincopy
<body>
<c:foreach items= "${articles}" var= "Item" >
${item.id}--${item.title}--${item.content}<br/>
</c:forEach>
</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