Spring整合MyBatis!
main方法测试,出现异常:
一堆错,很懵逼!
别慌,慢慢分析,也许错误很简单;
先分析一下applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<bean id="studentService" class="com.guor.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
<bean id="studentMapper" class="com.guor.dao.impl.StudentDaoImpl">
<!-- 将spring配置的 sqlSessionFactory 交给mapper(dao层) -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 配置数据库信息(替代mybatis的配置文件confx.ml) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 在springIOC中创建mybatis的核心类SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="com/guor/mapper/*.xml"></property>
</bean>
</beans>
分析思路:
1、全部注掉;
2、放开对外层的那一部分;
dataSource
dataSource是org.apache.commons.dbcp.BasicDataSource中的
不难发现BasicDataSource中的驱动项不是普通的driver,而是driverClassName;
运行:
输出发现是用set方法注入的,并且数据库中数据增加成功,success!
来源:oschina
链接:https://my.oschina.net/u/4006148/blog/3238382