<!-- 1. 数据源 : DriverManagerDataSource -->
<context:property-placeholder location="classpath:config/jdbc.properties" ignore-unresolvable="true" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" >
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
要使用这样的配置,
<bean id=" mySqlSessionFactory " class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
</bean>
// 这里的sessionFactory不能命名为SqlSessionFactory,换一个名称,
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tgb.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"> </property>
</bean>
引用时,<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"></property>
<context:property-placeholder location="classpath:config/jdbc.properties" ignore-unresolvable="true" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" >
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
要使用这样的配置,
<bean id=" mySqlSessionFactory " class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
</bean>
// 这里的sessionFactory不能命名为SqlSessionFactory,换一个名称,
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tgb.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"> </property>
</bean>
引用时,<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"></property>
注意用的是value而不是ref,改变了注入的顺序
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1875773