Coder Social home page Coder Social logo

Comments (18)

jeffgbutler avatar jeffgbutler commented on May 18, 2024 1

I think that's a good idea.

from spring-boot-starter.

mallim avatar mallim commented on May 18, 2024

While waiting for this fix, is there anyway to disable the MapperScan from happening? Any example?

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

Yes. Add a @MapperScan to your main application class pointing to a dummy interface. That will register a MapperFactoryBean:

@SpringBootApplication
@MapperScan("dummy.package")
public class SampleMybatisApplication implements CommandLineRunner {

or create a MapperFactoryBean explicitly:

@Bean
MapperFactoryBean<Object> createMapper() {
  return new MapperFactoryBean<Object>();
}

If a MapperFactoryBean is detected autoscan is not done.

from spring-boot-starter.

mallim avatar mallim commented on May 18, 2024

Thanks for the example, I think this is a good material to put in the docs. If not, people will have to come to this issue and read about this...

Anyway, this is what is working for my case:

@Configuration
@MapperScan("dummy")
public class MybatisConfig {

   @Bean
   public ActualMapper mapper(SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sessionTemplate = new SqlSessionTemplate( sqlSessionFactory );
        return sessionTemplate.getMapper(ActualMapper.class);
   }
}

dummy package contains an interface:

public interface DummyMapper {
}

whereby my mybatis-config.xml has some mappers

hope this will help someone ...

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

Good to know! BTW, I am curious about this. Do you want to turn off scanning? if so, why?

from spring-boot-starter.

mallim avatar mallim commented on May 18, 2024

I am using this way to avoid all interfaces to be registered as mybatis mappers even if they are not mappers. Actually, can we have a setting to turn off the @MapperScan ?

For example:

mybatis:
   scanning: true 

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

@mallim But... note that a mapper must be a bean in spring so it can be injected into any other bean. How are you registering your mapper? (or are you using the sqlSessionTemplate?)

from spring-boot-starter.

mallim avatar mallim commented on May 18, 2024

Hi @emacarron

I registered all the mappers by using this way:

@Configuration
@MapperScan("dummy") // required since version 1.0.2
public class MybatisConfig {

   @Bean
   public ActualMapper1 mapper(SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sessionTemplate = new SqlSessionTemplate( sqlSessionFactory );
        return sessionTemplate.getMapper(ActualMapper1.class);
   }

   @Bean
   public ActualMapper2 mapper(SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sessionTemplate = new SqlSessionTemplate( sqlSessionFactory );
        return sessionTemplate.getMapper(ActualMapper2.class);
   }
}

whereby mybatis-config.xml has some mappers

spring-boot-starter works with this pattern since 1.0 πŸ‘

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

I see, but I would say you have too much boilerplate.

If you set the @MapperScan() with the package name (or pattern) where your mappers are this will work fine and you can remove that two @bean methods.

Can you check that?

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

BTW I just have updated master so autoscanning will only pick interfaces inside **/mapper or **/mappers package as a convention (over configration).

Details here: http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

@kazuki43zoo I finally decided not to include "repository" to let people mix JPA and MyBatis in the same project.

Can you plese check the snapshot and let us know how it works?
repo: https://oss.sonatype.org/content/repositories/snapshots/

from spring-boot-starter.

kazuki43zoo avatar kazuki43zoo commented on May 18, 2024

@emacarron

OK, i agree with your decision !!
I think this change should will be announced expressly on release note.

Thanks!

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

Sure. And thanks for your opinion!

from spring-boot-starter.

mallim avatar mallim commented on May 18, 2024

@emacarron

My boilerplate way is still better than having this log appearing

2016-04-08 14:20:33.381  WARN 4828 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'XXXRepository' and 'XXXRepository' mapperInterface. Bean already defined with the same name!

And for trying to do this :-)

@MapperScan("pkg1,pkg2,pkg3,pkg4")

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

You can specify a pattern in basepackage. You may want to try that.
El 8 abr. 2016 8:41 a. m., "Ian Lim" [email protected] escribiΓ³:

@emacarron https://github.com/emacarron

My boilerplate way is still better than having this log appearing

2016-04-08 14:20:33.381 WARN 4828 --- [ main] o.m.s.mapper.ClassPathMapperScanner : Skipping MapperFactoryBean with name 'XXXRepository' and 'XXXRepository' mapperInterface. Bean already defined with the same name!

And for trying to do this :-)

@MapperScan("pkg1,pkg2,pkg3,pkg4")

β€”
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#46 (comment)

from spring-boot-starter.

emacarron avatar emacarron commented on May 18, 2024

I have recoded this to be more aligned with Spring @configuration that is based on annotations. So I have added a @Mapper marker annotation to the core so other DI frameworks can use it (spring, boot, cdi)

Thoughts?

from spring-boot-starter.

mallim avatar mallim commented on May 18, 2024

@emacarron

This is a very good idea πŸ‘

from spring-boot-starter.

mnesarco avatar mnesarco commented on May 18, 2024

Hi @emacarron I didn't know about this Mapper annotation in advance so I have added one in CDI some time ago just before 1.0 release. Do you think we have to remove the cdi specific annotation and use just this?

from spring-boot-starter.

marwin1991 avatar marwin1991 commented on May 18, 2024

I am using 3.5.6 And this still happens. I think MapperScan should have default value for annotationClass equal to org.apache.ibatis.annotations.Mapper.class

from spring-boot-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.