1. 程式人生 > >日常Exception(五):spring與mybatis整合時報錯

日常Exception(五):spring與mybatis整合時報錯

問題:

最近在整合SSM框架,搭建一半之後(即專案建立完畢,spring與mybatis整合完畢),開始測試spring與mybatis是否整合成功。

貼出部分程式碼:

/**
 * 配置spring和junit整合,junit啟動時載入springIOC容器 spring-test,junit
 */
//@RunWith(SpringJUnit4ClassRunner.class)
////告訴junit spring配置檔案
//@ContextConfiguration({"classpath:applicationContext.xml"})
public class SpringMybatisTest {
	
//	@Autowired
//	private UserService userService;
//	
//	@Test
//	public void testMethod()
//	{
//		User user  = userService.selectByPrimaryKey(1);
//		
//		System.out.println("個人資訊:"+user.getUsername());
//	}
	
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = application.getBean(UserService.class);
		User user  = userService.selectByPrimaryKey(1);
		
		System.out.println("個人資訊:"+user.getUsername());
	}
	
}
再貼出spring-mybatis.xml檔案:
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 1.配置jdbc檔案 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath:jdbc.properties"/>
    </bean>


	<!-- 2.資料庫連線池 -->
      <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="initialSize" value="1" />
          <property name="minIdle" value="1" />
          <property name="maxActive" value="10" />

          <!-- 配置獲取連線等待超時的時間 -->
          <property name="maxWait" value="10000" />

          <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
          <property name="timeBetweenEvictionRunsMillis" value="60000" />

          <!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
          <property name="minEvictableIdleTimeMillis" value="300000" />

          <property name="testWhileIdle" value="true" />

          <!-- 這裡建議配置為TRUE,防止取到的連線不可用 -->
          <property name="testOnBorrow" value="true" />
          <property name="testOnReturn" value="false" />

      </bean>
    

     
    <!-- spring和MyBatis完美整合,不需要mybatis的配置對映檔案 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自動掃描mapping.xml檔案 -->
        <property name="mapperLocations" value="classpath:mappers/*.xml"></property>  
    </bean>   
    
    
     <!-- DAO介面所在包名,Spring會自動查詢其下的類 ,自動掃描了所有的XxxxMapper.xml對應的mapper介面檔案,只要Mapper介面類和Mapper對映檔案對應起來就可以了-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.newace.dao" />  
    </bean>  
    
    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<!-- 配置事務管理器 -->
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean> 

    <!--======= 事務配置 End =================== -->
    <!-- 配置基於註解的宣告式事務 -->
    <!-- enables scanning for @Transactional annotations -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    
</beans>
前後檢查數遍,基本沒有檢查到啥問題。而且mapper.xml檔案也有正確對映dao層。最終還是報如下錯誤:

看錯誤就是說jdbc連接出錯。後來檢查包,檢查配置檔案,都覺得沒問題。抓狂至極,最後還是耐心度娘了一番。最終,抱著試一試的心態,嘗試解決!

解決方法:

就是先確保你的jdbc.properties配置是否全部拼寫正確。最後,一定確認每個引數的後面都不能有空格(個人感覺應該是url後面不能有),一定注意這個奇葩點。我就是被這個錯誤坑了大半天!!