【日常Exception】第五回:spring与mybatis整合时报错

热门系列:


1.问题

问题:

最近在整合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连接出错。后来检查包,检查配置文件,都觉得没问题。抓狂至极,最后还是耐心度娘了一番。最终,抱着试一试的心态,尝试解决!


2.方法

解决方法:

就是先确保你的jdbc.properties配置是否全部拼写正确。最后,一定确认每个参数的后面都不能有空格(个人感觉应该是url后面不能有),一定注意这个奇葩点。我就是被这个错误坑了大半天!!

 

本博客皆为学习、分享、探讨为本,欢迎各位朋友评论、点赞、收藏、关注,一起加油!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

善良勤劳勇敢而又聪明的老杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值