Spring3 +mybatis3 之 MapperScannerConfigurer

本文介绍如何使用org.mybatis.spring.mapper.MapperScannerConfigurer简化MyBatis DAO接口配置,避免大量重复工作。通过示例展示了如何配置MapperScannerConfigurer并正确设置Service层。

    之前一直使用"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>

 

转载于:https://www.cnblogs.com/onmyway20xx/p/4109865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值