MyBatis Combat Course (mybatis in Action) vi: integration with spring MVC

Source: Internet
Author: User

the previous articles have already covered the integration of MyBatis and Spring. But this time, all the projects are not web engineering yet, Although I have been creating Web Projects. Today will be directly integrated with MyBatis and spring mvc, The source code is downloaded at the end of this article. there are several main configurations
1. Web. XML config Spring dispatchservlet, for example: mvc-dispatcher
2. Mvc-dispatcher-servlet.xml file configuration
3. Spring applicationcontext.xml file configuration (related to database, integrated with MyBatis sqlsessionfaction, scan all MyBatis mapper files, etc.)
4. Writing Controller Classes
5. Write the page Code.
there is an approximate image, the entire drawing is as Follows:
[/code]

1. Web. XML config Spring dispatchservlet, for example: mvc-dispatcher
Program code
<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>
<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>

2. Configure the Mvc-dispatcher-servlet.xml file in the same directory as the Web. xml, the file name must correspond to the servlet name of the Dispatcherservlet you configured in Web. xml. the contents are:
Program code
<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.yihaomen.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>

3. Configure the spring configuration file in the Source directory config directory applicationcontext.xml
Program code
<!--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= "password"
p:maxactive= "ten" > "p:maxidle="
</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/yihaomen/mapper/*.xml"/>
</bean>

<bean class= "org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "basepackage" value= "com.yihaomen.inter"/>
</bean>


I do not know why, once I use the mapperscannerconfigurer to scan all the mapper interface, the database configuration DataSource can not be used to read Database.properties Files. Error: Cannot load JDBC driver class ' ${jdbc.driverclassname} ', someone on the web said it could be solved with Sqlsessionfactionbean injection under spring 3.1.1, but I use spring 3.1.3 still has a problem, so the database connection information is directly configured in the XML file.

4. Writing the controller layer
Program code
Package com.yihaomen.controller;
Import java.util.List;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import org.springframework.web.servlet.ModelAndView;
Import com.yihaomen.inter.IUserOperation;
Import com.yihaomen.model.Article;

@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;
}
}

5. Page File:
[code]
<c:foreach items= "${articles}" var= "item" >
${item.id}--${item.title}--${item.content}<br/>
</c:forEach>


Operation Result:


of course there are mybatis configure.xml configuration files, Similar to the previous one, the only difference is not to configure the following: <mapper resource= "com/yihaomen/mapper/user.xml" />, all of these are handed to the sqlsessionfactory at the time of configuration, by <property name= "mapperlocations" value= "classpath*:com/yihaomen/mapper/ *.xml "/> to Import.
    
source: http://www.yihaomen.com/article/java/318.htm

From for Notes (Wiz)

MyBatis Combat Course (mybatis in Action) vi: integration with spring MVC

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.