之前一直使用"org.mybatis.spring.mapper.MapperFactoryBean"这个类在spring中配置mybatis的dao接口,后来发现如果dao太多话,配置非常繁冗,于是想改用
org.mybatis.spring.mapper.MapperScannerConfigurer来精简配置.
使用注意, 直接上代码吧:
1. dao的配置文件:
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
<!-- 一定得指定这个属性!--> default-autowire="byType" >
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
</bean>
<!-- 无需指定其他属性 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- value是dao接口类所处的package--> <property name="basePackage" value="com.jd.jd_ptesting.dao" /> </bean> </beans>
2. Service 配置
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType"
>
<bean id="settingService" class="com.jd.jd_ptesting.service.impl.SettingServiceImpl"> <!-- 注意 此处一定不要指定dao属性!!! <property name="settingDao" ref="settingDao" /> --> </bean> </beans>