Spring详解之四:SpringMVC整合Mybatis相关配置文件信息

#SpringMVC整合Mybatis相关配置文件信息 ##Mybatis配置文件sqlMapConfig.xml中仅保留mappers子节点,将数据源等全部交给Spring容器管理

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers -->
  <mappers>
     <package name="cn.itcast.ssm.mapper" />
  </mappers>
</configuration>

##在SpringMVC的配置文件springmvc.xml中添加数据库连接dataSource、sqlsessionfactory和mapper扫描器配置子节点

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 配置数据源dataSource -->
	     <context:property-placeholder location="classpath:db.properties"/>
	   <!-- 数据库连接池 -->
	   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	       <property name="driverClassName" value="${jdbc.driver}"/>
	       <property name="url" value="${jdbc.url}"/>
	       <property name="username" value="${jdbc.username}"/>
	       <property name="password" value="${jdbc.password}"/>
	       <property name="maxActive" value="30"/>
	       <property name="maxIdle" value="5"/>
	   </bean>

	<!-- 配置sqlsessionfactory --> 
	   <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	       <!-- 数据库连接池 -->
	        <property name="dataSource" ref="dataSource" />
	       <!-- 加载mybatis的全局配置文件 -->
	        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	   </bean>
	
	<!-- 配置mapper扫描器 --> 
	   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	      <property name="basePackage" value="cn.itcast.springmvc.mapper"></property>
	      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	   </bean>
	<!-- (一)HandlerAdapter处理器适配和HandlerMapping处理器映射器配置 -->
	  <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
	        mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,
	        如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
	        实际开发时使用mvc:annotation-driven-->
	  <mvc:annotation-driven/>
	<!-- (二)Handler处理器(即Controller类)配置 --> 
	  <!-- (注解)通过Spring组件扫描器批量扫描自动添加Java类到Spring容器中 -->
	   <!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->
	  <context:component-scan base-package="cn.itcast.springmvc.controller.first"/>
	<!-- (三)ViewResolver视图解析器配置 -->
	   <!-- InternalResourceViewResolver:支持JSP视图解析;
	        viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar包;
	        prefix 和suffix:查找视图页面的前缀和后缀,最终视图地址为:前缀+逻辑视图名+后缀;
	        逻辑视图名:需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址 “WEB-INF/jsp/hello.jsp”.-->
	  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
	    <property name="prefix" value="/WEB-INF/jsp/"/>
	    <property name="suffix" value=".jsp"/>
	  </bean>
</beans>

转载于:https://my.oschina.net/abinge/blog/683598

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值