乐趣区

mybatis和springMVC整合及其中的问题

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.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb1?rewriteBatchedStatements=true
username=root
password=1234

c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=60
c3p0.minPoolSize=5
c3p0.maxPoolSize=100
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.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 改了就解决问题了

退出移动版