MyBatis Learning (6) 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.

MyBatis Learning (6) 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.