(1)config/mybatis/SqlMapConfig.xml中放置mybatis的配置文件,因爲這個例子很簡單,因此配置得比較簡單。在spring與mybatis的整合中,在這裏不用配置mapper,由於在mybatis-spring整合jar包中有mapper的掃描類。java
<?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還不是很會,因此事務配置並無在程序中體現。mysql
<?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框架所用到的處理器映射器。 web
<?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裏面配置了 數據庫驅動所需的各類屬性包括:驅動類名、用戶名、密碼等還有數據池的屬性spring
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的配置文件,不太懂。sql
(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>
接下來只需寫:spring-mvc
(1)mapper文件:mapper.java文件和mapper.xml文件同名同目錄(dao)mybatis
(2)service:寫service接口,並實現,最後配置。在實現類中注入mapper,此時會報錯,由於還沒掃描,並不影響程序運行。
mvc
<bean class="com.hggggc.ssm.service.DeptServiceImpl" id="deptService"/>
(3)controller(handler):在其中注入serviceapp
<!-- 配置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改了就解決問題了