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文件中:

<?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"> 
    
 <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 -->   
    <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="10">
    </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 name="dataSource" ref="dataSource"/> 
     <!--configLocation属性指定mybatis的核心配置文件--> 
     <property name="configLocation" value="classpath:config/Configuration.xml" /> 
     <!-- 所有配置的mapper文件 -->
     <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> 

三、集成springMVC:

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

mvc-dispatcher-servlet.xml:

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



web.xml:
<?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配置 -->
  <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>

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


控制层:

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

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

展示层:

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

<body>
	<c:forEach items="${articles}" var="item">  
        ${item.id }--${item.title }--${item.content }<br />
	</c:forEach>
</body>


四、测试:

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


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


五、总结:

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

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


评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值