一、Spring整合配置Mybatis
spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通過spring配置文件一步到位。一般需要具備如下幾個基本配置。
1.配置數據源(連接數據庫最基本的屬性配置,如數據庫url,賬號,密碼,和數據庫驅動等最基本參數配置)
1 <!-- 導入properties配置文件 --> 2 <context:property-placeholder location="classpath*:/jdbc.properties"/> 3 4 <!-- 數據源基本配置 --> 5 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 6 <property name="username" value="${jdbc.username}"/> 7 <property name="password" value="${jdbc.password}"/> 8 <property name="url" value="${jdbc.url}"/> 9 <property name="driverClassName" value="${jdbc.driverClassName}"/> 10 </bean>
我們將參數配置統一寫入jdbc.properties文件中:
1 jdbc.url=jdbc:mysql://localhost:3306/mydb 2 jdbc.driverClassName=com.mysql.jdbc.Driver 3 jdbc.username=root 4 jdbc.password=root
2.配置SessionFactory(用於將數據源和mybatis的mapper映射文件進行管理和初始化)
1 <!-- 創建sessionFactory --> 2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 3 <property name="dataSource" ref="dataSource"/> 4 <!-- 掃描mapper映射文件 --> 5 <property name="mapperLocations" value="classpath*:dao/mapping/*.xml" /> 6 </bean>
3.掃描mapper映射文件所對應的dao接口類(其實dao接口的是實現類就是mapper.xml映射文件,此配置是為了將接口和映射文件進行初始化)
1 <!-- 掃描與mapper映射文件對應的dao接口類 --> 2 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 3 <property name="basePackage" value="dao.daoInterface"/> 4 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 5 </bean>
4.創建事務(事務有兩種配置方式:注解方式和aop切入方式)
1 <!-- 創建事務管理 --> 2 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 3 <property name="dataSource" ref="dataSource"/> 4 </bean>
創建好事務管理后,我們可以選擇使用注解方式實現管理,或者aop織入管理
4.1注解方式
1 <!-- 注解式事務配置,啟動事務注解驅動 --> 2 <tx:annotation-driven/>
注解配置方式要先通過配置文件啟動事務注解驅動,然后在要加事務的方法上面加上事務注解:@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED) 事務相關知識可參考:http://www.cnblogs.com/caijh/p/7724964.html
1 @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED) 2 @Override 3 public void insertUser(UserEntity userEntity) { 4 for(int i=0;i<10;i++){ 5 userEntity.setId(111+i); 6 userEntity.setUsername("mybatis "+i); 7 userDao.insertUser(userEntity); 8 } 9 }
4.2 AOP織入方式
1 <!-- aop切入式事務配置 --> 2 <tx:advice id="trAdvice" transaction-manager="transactionManager"> 3 <tx:attributes> 4 <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/> 5 </tx:attributes> 6 </tx:advice> 7 8 <aop:config> 9 <aop:pointcut id="serviceAdvice" expression="execution(* service.serviceImpl.*.*(..))"/> 10 <aop:advisor advice-ref="trAdvice" pointcut-ref="serviceAdvice"/> 11 </aop:config>
AOP相關知識可參考:http://www.cnblogs.com/caijh/p/7710725.html
最終配置如下:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 ~ @(#) applicationContext.xml 4 ~ <br> Copyright: Copyright (c) 2017 5 ~ <br> @author cjh 6 ~ <br> 2017-10-29 15:45:16 7 --> 8 <beans xmlns="http://www.springframework.org/schema/beans" 9 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 10 xmlns:tx="http://www.springframework.org/schema/tx" 11 xmlns:aop="http://www.springframework.org/schema/aop" 12 xmlns:context="http://www.springframework.org/schema/context" 13 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 15 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 16 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> 17 18 <!-- 導入properties配置文件 --> 19 <context:property-placeholder location="classpath*:/jdbc.properties"/> 20 21 <!-- 掃描注解包 --> 22 <context:component-scan base-package="dao.daoInterface"/> 23 <context:component-scan base-package="service.serviceImpl" /> 24 25 <!-- 數據源基本配置 --> 26 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 27 <property name="username" value="${jdbc.username}"/> 28 <property name="password" value="${jdbc.password}"/> 29 <property name="url" value="${jdbc.url}"/> 30 <property name="driverClassName" value="${jdbc.driverClassName}"/> 31 </bean> 32 33 <!-- 創建sessionFactory --> 34 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 35 <property name="dataSource" ref="dataSource"/> 36 <!-- 掃描mapper映射文件 --> 37 <property name="mapperLocations" value="classpath*:dao/mapping/*.xml" /> 38 </bean> 39 40 <!-- 掃描與mapper映射文件對應的dao接口類 --> 41 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 42 <property name="basePackage" value="dao.daoInterface"/> 43 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 44 </bean> 45 46 47 <!-- 創建事務管理 --> 48 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 49 <property name="dataSource" ref="dataSource"/> 50 </bean> 51 52 <!-- 注解式事務配置,啟動事務注解驅動 --> 53 <!--<tx:annotation-driven/>--> 54 55 <!-- aop切入式事務配置 --> 56 <tx:advice id="trAdvice" transaction-manager="transactionManager"> 57 <tx:attributes> 58 <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/> 59 </tx:attributes> 60 </tx:advice> 61 62 <aop:config> 63 <aop:pointcut id="serviceAdvice" expression="execution(* service.serviceImpl.*.*(..))"/> 64 <aop:advisor advice-ref="trAdvice" pointcut-ref="serviceAdvice"/> 65 </aop:config> 66 67 </beans>
SSMDemo整合配置源碼位置:https://gitee.com/codecaijh/SSMDemo
二、Spring整合配置Hibernate
Spring整合配置hibernate和Mybatis的配置大同小異,主要區別在與SessionFactory和映射文件的管理配置,但目的都是一樣的。
1.配置數據源(連接數據庫最基本的屬性配置,如數據庫url,賬號,密碼,和數據庫驅動等最基本參數配置)【同Mybatis配置】
2.配置SessionFactory(因為Hibernate對數據庫操作做了封裝,所以需要一些額外的屬性配置)
1 <!-- 創建sessionFactory --> 2 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 3 <property name="dataSource" ref="dataSource"/> 4 <property name="hibernateProperties"> 5 <props> 6 <prop key="hibernate.show_sql">true</prop> 7 <prop key="hibernate.format_sql">true</prop> 8 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 9 <!--<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>--> 10 </props> 11 </property> 12 <!-- 實體類映射文件 --> 13 <property name="mappingLocations"> 14 <list> 15 <value>classpath*:/domain/*.hbm.xml</value> 16 </list> 17 </property> 18 <!-- 掃描實體類包 --> 19 <property name="packagesToScan"> 20 <value>domain</value> 21 </property> 22 <!-- 實體類 --> 23 <property name="annotatedClasses"> 24 <list> 25 <value>domain.UserEntity</value> 26 </list> 27 </property> 28 </bean>
Hibernate的配置中,把映射文件和是實體類映射全部配置在SessionFactory中,也就是和Mybatis第2步和第3步類似,
3.創建事務(事務有兩種配置方式:注解方式和aop切入方式)【同Mybatis配置】
最終配置如下:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 ~ @(#) applicationContext.xml 4 ~ <br> Copyright: Copyright (c) 2017 5 ~ <br> @author cjh 6 ~ <br> 2017-10-29 15:45:16 7 --> 8 <beans xmlns="http://www.springframework.org/schema/beans" 9 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 10 xmlns:tx="http://www.springframework.org/schema/tx" 11 xmlns:aop="http://www.springframework.org/schema/aop" 12 xmlns:context="http://www.springframework.org/schema/context" 13 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 16 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 17 18 <!-- 導入properties配置文件 --> 19 <context:property-placeholder location="classpath*:/jdbc.properties"/> 20 21 <!-- 掃描注解包 --> 22 <context:component-scan base-package="dao.daoImpl"/> 23 <context:component-scan base-package="service.serviceImpl" /> 24 25 <!-- 數據源基本配置 --> 26 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 27 <property name="username" value="${jdbc.username}"/> 28 <property name="password" value="${jdbc.password}"/> 29 <property name="url" value="${jdbc.url}"/> 30 <property name="driverClassName" value="${jdbc.driverClassName}"/> 31 </bean> 32 33 <!-- 創建sessionFactory --> 34 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 35 <property name="dataSource" ref="dataSource"/> 36 <property name="hibernateProperties"> 37 <props> 38 <prop key="hibernate.show_sql">true</prop> 39 <prop key="hibernate.format_sql">true</prop> 40 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 41 <!--<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>--> 42 </props> 43 </property> 44 <!-- 實體類映射文件 --> 45 <property name="mappingLocations"> 46 <list> 47 <value>classpath*:/domain/*.hbm.xml</value> 48 </list> 49 </property> 50 <!-- 掃描實體類包 --> 51 <property name="packagesToScan"> 52 <value>domain</value> 53 </property> 54 <!-- 實體類 --> 55 <property name="annotatedClasses"> 56 <list> 57 <value>domain.UserEntity</value> 58 </list> 59 </property> 60 </bean> 61 62 <!-- 創建聲明式事務管理 --> 63 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 64 <property name="sessionFactory" ref="sessionFactory"/> 65 </bean> 66 <!-- 事務通知(注解方式) --> 67 <tx:annotation-driven transaction-manager="transactionManager"/> 68 69 <!-- 事務通知(aop方式) --> 70 <!--<tx:advice id="txAdvice" transaction-manager="transactionManager"> 71 <tx:attributes> 72 <!– propagation配置傳播行為,isolation配置隔離方式 –> 73 <tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" /> 74 </tx:attributes> 75 </tx:advice> 76 77 <!– aop織入通知 –> 78 <aop:config> 79 <aop:pointcut id="serviceOption" expression="(execution(* service.serviceImpl.*.*(..)) and (execution(* dao.daoImpl.*.*(..))))"/> 80 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOption"/> 81 </aop:config>--> 82 83 84 </beans>
SSHDemo整合配置源碼位置:https://gitee.com/codecaijh/SSHDemo
三、可能遇到的問題
在整合mybatis的時候可能會遇到 BindingException: Invalid bound statement (not found): dao.daoInterface.UserDao.getUserInfo dao接口類和mapper文件綁定失敗而找不到實現方法的異常。
查看target目錄下的classes文件時發現沒有任何xml文件,推斷項目編譯的時候可能沒把它包含進去。
解決辦法:
在pom.xml文件中添加如下內容:(表示讓maven將以xml和properties等為后綴的文件在構建的時候從資源路徑加載到目標路徑)
1 <build> 2 3 <resources> 4 <resource> 5 <directory>src/main/java</directory> 6 <includes> 7 <include>**/*.properties</include> 8 <include>**/*.xml</include> 9 </includes> 10 <filtering>false</filtering> 11 </resource> 12 </resources> 13 </build>
資源往往不是代碼,無需編譯,而是一些properties或XML配置文件,構建過程中會往往會將資源文件從源路徑復制到指定的目標路徑。
配置說明:
- resources,build過程中涉及的資源文件
- targetPath,資源文件的目標路徑
- filtering,構建過程中是否對資源進行過濾,默認false
- directory,資源文件的路徑,默認位於${basedir}/src/main/resources/目錄下
- includes,一組文件名的匹配模式,被匹配的資源文件將被構建過程處理
- excludes,一組文件名的匹配模式,被匹配的資源文件將被構建過程忽略。同時被includes和excludes匹配的資源文件,將被忽略。
- filters,給出對資源文件進行過濾的屬性文件的路徑,默認位於${basedir}/src/main/filters/目錄下。屬性文件中定義若干鍵值對。在構建過程中,對於資源文件中出現的變量(鍵),將使用屬性文件中該鍵對應的值替換。
- testResources,test過程中涉及的資源文件,默認位於${basedir}/src/test/resources/目錄下。這里的資源文件不會被構建到目標構件中