星空网 > 软件开发 > Java

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

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

一、目录结构:

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

 

二、集成spring:

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

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

[html] view plaincopyMybatis基础入门(四)——与springMVC的集成Mybatis基础入门(四)——与springMVC的集成

  1. <?

  2. <beans 

  3.     

  4.     

  5.     

  6.     

  7.     

  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"   

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

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

  30.     </bean>  

  31.       

  32.        

  33.   <bean id="sqlSessionFactory" >   

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

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

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

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

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

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

  40.   </bean>   

  41.     

  42.   <bean >  

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

  44.   </bean>  

  45. </beans>   


三、集成springMVC:

    相关的jar包参见源码,springMVC充当了web端的框架,mvc-dispatcher-servlet.

mvc-dispatcher-servlet.

[html] view plaincopyMybatis基础入门(四)——与springMVC的集成Mybatis基础入门(四)——与springMVC的集成

  1. <beans 

  2.     

  3.     

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

  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.

[html] view plaincopyMybatis基础入门(四)——与springMVC的集成Mybatis基础入门(四)——与springMVC的集成

  1. <?

  2. <web-app 

  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.

  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.

 

控制层:

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

[java] view plaincopyMybatis基础入门(四)——与springMVC的集成Mybatis基础入门(四)——与springMVC的集成

  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 plaincopyMybatis基础入门(四)——与springMVC的集成Mybatis基础入门(四)——与springMVC的集成

  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可以看到下面的效果:

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

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

 

五、总结:

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

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

 

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




原标题:Mybatis基础入门(四)——与springMVC的集成

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

【独立站】+Shopify店铺主题二次开发(1) - 配置待开发的店铺:https://www.kjdsnews.com/a/1388021.html
加拿大注册商标流程多久能够完成?:https://www.kjdsnews.com/a/1388022.html
加拿大注册商标国内生产分装是否可行?:https://www.kjdsnews.com/a/1388023.html
加拿大商标购买指南实现商标保护的最佳方式:https://www.kjdsnews.com/a/1388024.html
加拿大商标登记指南步骤与注意事项:https://www.kjdsnews.com/a/1388025.html
加拿大商标登记指南如何保护你的商标:https://www.kjdsnews.com/a/1388026.html
2024.04.18亚马逊选品推荐(仅供参考):女装蛋糕连衣裙:https://www.kjdsnews.com/a/1842234.html
欧洲市场疯了,订单排到7、8月!:https://www.kjdsnews.com/a/1842235.html
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流