how to scan multiple packages with MapperScannerConfigurer with spring3, mybatis, and mybatis-spring

980 views
Skip to first unread message

Randy P

unread,
Dec 17, 2013, 9:13:39 AM12/17/13
to mybati...@googlegroups.com
I am re factoring an existing application to spring3 and mybatis-spring.
One of my restrictions is to keep the current package layout.

it seems that when using org.mybatis.spring.mapper.MapperScannerConfigurer or  <mybatis:scan> that is does not recursively look other packages, and i am looking how best to do this. I have it working now by adding individually each DAO interface to the MapperScannerConfigurer

in my applicationContext i have ::

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="formularyDb" />
        <property name="configLocation"  value="file:/web/sites/drugformulary-spring/config/mybatis-config.xml" />
        <property name="mapperLocations" value="file:/web/sites/drugformulary-spring/mappers/*.xml" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <bean id="drugmasterScanner"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.mydm.formulary.drugmaster.dao
                                                                 ,org.mydm.formulary.pdfgroup.dao
                                                                 ,org.mydm.formulary.drugconfig.dao
                                                                "
        />
    </bean>


As mentioned i have to add each of these interfaces eg:org.mydm.formulary.pdfgroup.dao manually.
I have tried <property name="basePackage" value="org.mydm.formulary "/>  but none of the dao's where found and I also tried
<mybatis:scan base-package="org.myd.formulary.drugmaster" /> which also did not work, to work the package where the doa is must be specified.

my package structure is like below and i am required to keep it like this
org.mydm.formulary.drugmaster.controller
                                              .service
                                              .dao
org.mydm.formulary.pdfgroup.controller
                                              .service
                                              .dao
org.mydm.formulary.drugconfig.controller
                                              .service
                                              .dao








Eduardo Macarron

unread,
Dec 18, 2013, 1:12:29 AM12/18/13
to mybati...@googlegroups.com
Randy, the scanner does search recursively you can even point to your root classpath and mark the interfaces with a marker interface or a custom annotation.


2013/12/17 Randy P <randy...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Randy P

unread,
Dec 18, 2013, 8:53:14 AM12/18/13
to mybati...@googlegroups.com


Eduardo, thanks for responding. That is what I thought and maybe i am misunderstanding how to do it

for example 

this works::
    <bean id="drugmasterScanner"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.mydm.formulary.drugmaster.dao
                                                                 ,org.mydm.formulary.pdfgroup.dao
                                                                 ,org.mydm.formulary.drugconfig.dao
                                                                   "
        />
    </bean>

this does not work should it?

    <bean id="drugmasterScanner"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.mydm.formulary"/>
    </bean>
 
I get the exception when i try to access one of them

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): org.mydm.formulary.drugmaster.service.DrugMasterService.getDrugsWithAlert

Eduardo Macarron

unread,
Dec 18, 2013, 3:02:45 PM12/18/13
to mybati...@googlegroups.com
It should indeed. 

And also an expression like **.dao


2013/12/18 Randy P <randy...@gmail.com>

Randy P

unread,
Dec 18, 2013, 3:14:56 PM12/18/13
to mybati...@googlegroups.com
This worked!!!!

Thankyou SO MUCH!

<bean id="drugmasterScanner"  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.mydm.formulary.**.dao"/>
    </bean>
Reply all
Reply to author
Forward
0 new messages