1. springMVC+spring+mybatis的整合大致结构:

(1)config/mybatis/SqlMapConfig.xml中放置mybatis的配置文件,由于这个例子很简单,所以配置得比较简单。在spring与mybatis的整合中,在这里不用配置mapper,因为在mybatis-spring整合jar包中有mapper的扫描类。

<?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></configuration>

(2)config/spring/appliacationContext.xml中配置的是mybatis和spring整合的配置。其中包括数据源(数据池)的配置、sqlSessionFactory的配置、mapper扫描器的配置还有事务的配置。目前aop还不是很会,所以事务配置并没有在程序中体现。

<?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:p="http://www.springframework.org/schema/p"        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.1.xsd    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    " default-autowire="byName">    <!-- 读取配置文件 -->    <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:db.properties</value>            </list>        </property>    </bean>    <!-- 配置数据库连接池 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="${username}"/>        <property name="password" value="${password}"/>        <property name="driverClass" value="${driverClassName}"/>        <property name="jdbcUrl" value="${jdbcUrl}"/>        <!--<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>-->        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>        <property name="maxStatements" value="${c3p0.maxStatements}"/>        <property name="numHelperThreads" value="${c3p0.numHelperThreads}"/>    </bean>    <!-- 配置sqlSessionFactory -->    <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.hggggc.ssm.mapper"/>        <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>    </bean>    <bean class="com.hggggc.ssm.service.DeptServiceImpl" id="deptService"/>    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>        <!--<tx:advice id="Txadvice" transaction-manager="transactionManager">-->        <!--<tx:attributes>-->            <!--<tx:method name="save" propagation="REQUIRED"/>-->            <!--<tx:method name="delete" propagation="REQUIRED"/>-->            <!--<tx:method name="insert" propagation="REQUIRED"/>-->            <!--<tx:method name="update" propagation="REQUIRED"/>-->            <!--<tx:method name="find" propagation="SUPPORTS" read-only="true"/>-->        <!--</tx:attributes>-->    <!--</tx:advice>-->    <!--<aop:config>-->        <!--<aop:advisor advice-ref="Txadvice" pointcut="execution(* com.hggggc.ssm.service.*.*(..))"/>-->    <!--</aop:config>--></beans> 

(3)config/spring/springMVC.xml配置的就是springMVC框架所用到的处理器映射器。 

<?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:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:task="http://www.springframework.org/schema/task"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:apop="http://www.springframework.org/schema/aop"       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-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/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/mvc/spring-aop-3.2.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/mvc/spring-tx-3.2.xsd        http://www.springframework.org/schema/task        http://www.springframework.org/schema/task/spring-task-3.2.xsd">    <!--视图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>    <!--3.1后的配置-->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>       <!--扫描组件-->    <context:component-scan base-package="com.hggggc.ssm.controller"></context:component-scan></beans>    

(4)db.properties里面配置了 数据库驱动所需的各种属性包括:驱动类名、用户名、密码等还有数据池的属性

driverClassName=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/mydb1?rewriteBatchedStatements=trueusername=rootpassword=1234c3p0.acquireIncrement=3c3p0.initialPoolSize=3c3p0.idleConnectionTestPeriod=60c3p0.minPoolSize=5c3p0.maxPoolSize=100c3p0.maxStatements=100c3p0.numHelperThreads=10c3p0.maxIdleTime=60

(5)log4j.properties是log的配置文件,不太懂。

(6)有关web.xml的配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring/*.xml</param-value>    </context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>    <servlet>        <servlet-name>springMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring/springMVC.xml</param-value>        </init-param>    </servlet>        <servlet-mapping>        <servlet-name>springMVC</servlet-name>        <url-pattern>*.action</url-pattern>    </servlet-mapping></web-app>

接下来只需写:

(1)mapper文件:mapper.java文件和mapper.xml文件同名同目录(dao)

(2)service:写service接口,并实现,最后配置。在实现类中注入mapper,此时会报错,因为还没扫描,并不影响程序运行。

<bean class="com.hggggc.ssm.service.DeptServiceImpl" id="deptService"/> 

(3)controller(handler):在其中注入service

2.spring+mybatis 报Could not load driverClass ${jdbc.driverClass}

<!-- 配置sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource"/>    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <property name="basePackage" value="com.hggggc.ssm.mapper"/>    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean>

改为

<!-- 配置sqlSessionFactory --><bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource"/>    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <property name="basePackage" value="com.hggggc.ssm.mapper"/>    <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/></bean>

在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 
所以将id改了就解决问题了