<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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 ">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:/config/jdbc.properties" />
<!-- 数据库连接池 -->
<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="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="minIdle" value="${jdbc.minIdle}" />
</bean>
<!-- 配置Mybatis的SqlsessionFactory,mybatis-spring中有特定的SqlSessionFactoryBean支持 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 定义数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定***Mapper.xml文件存放的位置-->
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />
<!-- 指定mybatis的配置文件的位置,后续属性指定的内容会覆盖它的配置。 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
<!--定义指定包中的类别名为 不包括报名的简单类名 ,多个package可以用逗号分隔-->
<property name="typeAliasesPackage" value="com.nonobank.demo.pojo" />
<!--定义单个别名,别名定义同上,只不过是单个指定
<property name="typeAliases">
<array>
<value>com.tiantian.mybatis.model.Blog</value>
<value>com.tiantian.mybatis.model.Comment</value>
</array>
</property>
-->
</bean>
<!--
MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描包的路径
如果要扫描多个包,中间使用半角逗号分隔
要求mapper.xml和mapper.java同名且在同一个目录
-->
<property name="basePackage" value="mapper,com.nonobank.demo.dao"/>
<!-- 使用sqlSessionFactoryBeanName -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 定义MapperFactoryBean,我们就可以把我们对应的Mapper接口注入到由Soring管理的Bean对象中了
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.tiantian.mybatis.mapper.BlogMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
-->
</beans>
在我测试过程中,碰到另一个很蛋疼的问题,我将mapper.java与mapper.xml分开,并且配置了如上使用mapperLocations配置了xml数据源。按道理我使用MapperScannerConfigurer和MapperFactoryBean两种方式都能正常访问了。但是只有使用MapperScannerConfigurer能正常运行,而如果用
MapperFactoryBean则无法自动注入userMapper(我使用的是@AutoWired注解)。
经过一些分析我重现了两种可行的情况:
1、使用MapperFactoryBean,在application-service.xml中配置service Bean并且提供< context:annotation-config />标签(注意这个标签需要和Bean定义在一个xml文件中)用于注入userMapper(@Autowired)。
2、可以只配置service Bean,不添加context:annotation-config标签,使用MapperScannerConfigurer,UserMapper却也成功注入了。
第一种方式是正确的Spring配置方式,而第二种方式Spring没有通过ontext:annotation-config标签提供注解自动装配功能,却也实现了注解装配,这是问题的所在
参考博客:
Spring整合Mybatis