easybest / spring-boot-starter-data-mybatis

Starter for using Spring Data MyBatis
Apache License 2.0
11 stars 5 forks source link

Conflict with standard mybatis starter #11

Open RushPVA opened 7 years ago

RushPVA commented 7 years ago

when i try to use both starters spring-boot-starter-data-mybatis and mybatis-spring-boot-starter, i have an exception that two SqlSessionTemplate beans were created.

easybest commented 7 years ago

here is a sample : https://github.com/hatunet/spring-data-mybatis-samples

easybest commented 7 years ago

can you show me more info about your exception?

RushPVA commented 7 years ago

i have two dependencies: 1) default spring mybatis starter

org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0

2) spring data mybatis starter

com.ifrabbit spring-boot-starter-data-mybatis 1.0.13.RELEASE

Without any additional configuration(only two mappers were created: TestMapper - standard mybatis mapper and another - spring data mybatis repository) i get exception:

TestMapper.class] required a single bean, but 2 were found:

RushPVA commented 7 years ago

Workaround: remove "spring data mybatis starter" dependency and configure Mybatis repositories manually...

easybest commented 7 years ago

don't add org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 if you are using spring boot, you just need add this dependecy:

com.ifrabbit spring-boot-starter-data-mybatis 1.0.13.RELEASE
RushPVA commented 7 years ago

hmm but org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 have additional possibilities to configure mybatis by spring boot way. why both approachs conflicts with each other? i suppose it would be better if com.ifrabbit spring-boot-starter-data-mybatis 1.0.13.RELEASE will use existing beans if they are configured...

easybest commented 7 years ago

maybe two SqlSessionFactories make this conflict.

pls show me you exception stackprint

RushPVA commented 7 years ago

I have already post usefull part of stacktrace: TestMapper.class] required a single bean, but 2 were found:

Another part of stacktrace contains only my classes info. As I can see there are two sqlSessionTemplate beans.

easybest commented 7 years ago

this reason hit the problem , when you use spring-boot-starter-data-mybatis , it will create a sqlsessiontemplate by atuoconfigration, so you should keep only one, and ignore another one.

I still think you don't need add org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0, can you explain the detail reason why you must add it to your project?

RushPVA commented 7 years ago

reasons to add org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0: 1)i can configure mybatis config without any code in application.yaml mybatis: type-aliases-package: mypackage configuration: map-underscore-to-camel-case: true default-fetch-size: 1000 default-statement-timeout: 300 ... As i know com.ifrabbit spring-boot-starter-data-mybatis doesn't have some of this configs. 2) my mappers will be picked up automatically(i need just add @Mapper annotation) ... Ofcourse i can do it manually without starter, and i can configure mybatis repositories manually without starter too. But i suppose main idea of spring boot starters is reducing java configuration. Usually i need some properties in application.yaml to configure whole library. And one more reason - as i know starters are designed to not conflict each other...(if you think problem is in org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0, can you explain me problem and i will write suggesstion in their repository)

RushPVA commented 7 years ago

My suggestion is: spring-boot-starter-data-mybatis should depend on mybatis-spring-boot-starter and use beans from there. I see same design for many other starters. For example spring-data-jpa depends on aop starter and jdbc starter. if you go by this way you will not need duplicate configuration of standard mybatis starter.

easybest commented 7 years ago

thanks for your suggestion. I will read source of mybatis-spring-boot-starter and decide next plan. This process will be as fast as possible.