zoukankan      html  css  js  c++  java
  • Spring3 +mybatis3 之 MapperScannerConfigurer

        之前一直使用"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>
  • 相关阅读:
    SparkSql初级编程实践
    云时代架构之苏宁安全架构演进及实践
    云时代架构之知乎网站架构变迁史
    质量属性的六个常见属性场景之《淘宝网》
    云时代架构之游戏服务器的架构演进
    《架构漫谈阅读心得》
    转换后缀表达式
    约瑟夫环(改进3.0)
    栈结构之后缀表达式
    约瑟夫环(改进2.0)
  • 原文地址:https://www.cnblogs.com/onmyway20xx/p/4109865.html
Copyright © 2011-2022 走看看