当前位置: 代码迷 >> VC/MFC >> Mybatis基础入门(4)——与springMVC的集成
  详细解决方案

Mybatis基础入门(4)——与springMVC的集成

热度:183   发布时间:2016-05-02 03:54:36.0
Mybatis基础入门(四)——与springMVC的集成

前面的增删改查还没有融入到一个web项目中,这里在前面的基础上,集成spring管理相关的bean,并在web层集成springmvc。同样会有源码的下载。

一、目录结构:

 

二、集成spring:

    因为是一个web项目,且使用了spring作为粘合剂,相关的jar包就不多解释了,详见源码。项目上篇文章中的结构,这里把映射文件单独放到一个目录,而且添加了Controller的包,映射文件内容不变。注意到这里讲其他配置文件整合到了config包中。因为使用spring,所以Configuration.xml中的数据连接就移动到了applicationContext.xml文件中,当然可以独立到database.properties文件中。

    spring管理的事务、Mybatis的sqlSessionFactoryBean,以及项目要扫描的Mybatis的映射文件包路径属性,数据源等都在applicationContext.xml文件中:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  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.       

  16.  <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->     

  17.     <context:property-placeholder    location="classpath:/config/database.properties" />  

  18.           

  19. <!--     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  

  20.         destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"  

  21.         p:url="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8"   

  22.         p:username="root"   

  23.         p:password="123"  

  24.         p:maxActive="10"  

  25.         p:maxIdle="10">  

  26.     </bean> -->  

  27.       

  28.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

  29.       <property name="dataSource" ref="dataSource" />  

  30.     </bean>  

  31.       

  32.        

  33.   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   

  34.      <!--dataSource属性指定要用到的连接池-->   

  35.      <property name="dataSource" ref="dataSource"/>   

  36.      <!--configLocation属性指定mybatis的核心配置文件-->   

  37.      <property name="configLocation" value="classpath:config/Configuration.xml" />   

  38.      <!-- 所有配置的mapper文件 -->  

  39.      <property name="mapperLocations" value="classpath*:com/tgb/mybatis/mapper/*.xml" />  

  40.   </bean>   

  41.     

  42.   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

  43.      <property name="basePackage" value="com.tgb.mybatis.inter" />       

  44.   </bean>  

  45. </beans>   


三、集成springMVC:

    相关的jar包参见源码,springMVC充当了web端的框架,mvc-dispatcher-servlet.xml和web.xml中是相关的配置。

mvc-dispatcher-servlet.xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  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.   

  12.     <context:component-scan base-package="com.tgb.controller" />  

  13.     <mvc:annotation-driven />  

  14.       

  15.     <mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>    

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

  17.        

  18.     <bean  

  19.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

  20.         <property name="prefix">  

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

  22.         </property>  

  23.         <property name="suffix">  

  24.             <value>.jsp</value>  

  25.         </property>  

  26.     </bean>  

  27.   

  28. </beans>  


web.xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  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.     

  8.     

  9.   <filter>  

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

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

  12.     <init-param>  

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

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

  15.     </init-param>  

  16.     <init-param>  

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

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

  19.     </init-param>  

  20.   </filter>  

  21.   <filter-mapping>  

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

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

  24.   </filter-mapping>  

  25.     

  26.   <context-param>  

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

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

  29.   </context-param>  

  30.   <listener>  

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

  32.   </listener>  

  33.   <listener>  

  34.     <listener-class>  

  35.             org.springframework.web.context.ContextCleanupListener</listener-class>  

  36.   </listener>  

  37.     

  38.   <!-- mvc配置 -->  

  39.   <servlet>  

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

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

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

  43.   </servlet>  

  44.   <servlet-mapping>  

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

  46.     <url-pattern>/</url-pattern>  

  47.   </servlet-mapping>  

  48.    

  49. </web-app>  

 

web.xml中的启动顺序是:context-param—》listener—》filter—》servlet,如果servlet配置了load-on-startup参数(非负整数,含0,且越小越优先启动)才会在服务器启动的时候加载到内存。

 

控制层:

 UserController,相当于struts中的action,因为使用注解,省去了像struts中的一堆的配置,个人感觉这样对开发和维护都很方便:

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. @Controller   

  2. @RequestMapping("/article")  

  3. public class UserController {  

  4.     @Autowired  

  5.     IUserOperation userMapper;  

  6.   

  7.     @RequestMapping("/list")  

  8.     public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){  

  9.         List<Article> articles=userMapper.getUserArticles(1);   

  10.         ModelAndView mav=new ModelAndView("list");  

  11.         mav.addObject("articles",articles);  

  12.         return mav;  

  13.     }  

  14. }  

展示层:

界面使用el表达式,解析返回的数据:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <body>  

  2.     <c:forEach items="${articles}" var="item">    

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

  4.     </c:forEach>  

  5. </body>  



四、测试:

     这里使用的是tomcat7,部署之后,访问:http://localhost:8080/MyBaits_Spring_MVC//article/list可以看到下面的效果:

注意这里的url,端口后的地址是在controller中配置的,跟struts中稍微有些区别。

 

五、总结:

    到这里才算是一个比较简单的web项目,有了前面文章的铺垫,这里只需要添加spring和mvc的配置,并添加一个controller即可。

    Mybatis还有其他很多的功能,比如,动态语句,自动生成代码,相关的一些插件,如分页插件等,有了这些基础,会在后续的文章中,逐步贴出相关的源码。

 

整理的比较粗略,将代码分享给大家,【源码地址获取

  相关解决方案