Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAJDBC-373 - Integration Mybatis with spring-data-jdbc,make mybatis can mixed with… #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

neil4dong
Copy link

Integration Mybatis with spring-data-jdbc,make mybatis can mixed with spring-data-jdbc in ONE repository by annotation with @MybatisQuery

And also Integration tests provided.

Please check out if this way is feasible ?
In my case we currently use mybatis for data access, and we are considering to migrate to spring-data-jdbc, but we have a lot of complex SQL which organized by Mybatis.

Considering the migration cost, how about use mybaitis and spring-data-jdbc together, so we can benefit form

  1. the simplicity provided form spring-data-jdbc
  2. another complex SQL solution provided by mybatis (which now only provided by QueryDsl)

… spring-data-jdbc in ONE repository by annotation with @MybatisQuery

And also IntegrationTests provided.
@pivotal-issuemaster
Copy link

@neil4dong Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@neil4dong Thank you for signing the Contributor License Agreement!

@schauder
Copy link
Contributor

Please create an issue at https://jira.spring.io/browse/DATAJDBC so we can discuss the goal of the PR first.

@jmsanzg
Copy link

jmsanzg commented Jul 15, 2019

Issue was created. Adding here the link to ease tracking https://jira.spring.io/projects/DATAJDBC/issues/DATAJDBC-373

@schauder schauder changed the title Integration Mybatis with spring-data-jdbc,make mybatis can mixed with… DATAJDBC-373 - Integration Mybatis with spring-data-jdbc,make mybatis can mixed with… Nov 19, 2019
@schauder
Copy link
Contributor

At a first very cursory glance this looks reasonable.
But I think we should treet MyBatis as a source for named queries.
I.e. by default we should try to find a MyBatis query based on the name of the method and alternatively offer the option to supply a name in the @Query annotation.

There is already work in progress for something similar based on property files (#180).
I'll will first try to get that one onto the main branch before giving a proper review to this issue with proper instructions how to proceed.

One minor thing until then: It seems like formatting got changed for JdbcQueryLookupStrategy.java. Please make sure you use the proper formatter. See https://github.com/spring-projects/spring-data-build/blob/master/CONTRIBUTING.adoc#quickstart

@neil4dong
Copy link
Author

Hi, schauder , a year have passed. I check the feture "propety file based NamedQuery", If i'm correct ,it just move sql to a property file.

If I need to build dynamic sql, is there a way to do so without using JPA (only use spring-data-jdbc) In one repository ?

I also checked document https://docs.spring.io/spring-data/data-jdbc/docs/2.0.0.RELEASE/reference/html/#repositories.custom-implementations
It says I can mix springdatajdbc repository with customized repository,I tried to make a demo , but it doesn't work . Is this feature implemented?

looking forward your apply ,thanks.

@schauder
Copy link
Contributor

schauder commented Jun 4, 2020

Custom methods should work, yes. If they don't please create a ticket.

@schauder
Copy link
Contributor

schauder commented Jun 8, 2020

@neil4dong Could you rebase this PR to resolve the merge conflicts?

@neil4dong
Copy link
Author

@schauder yes. the Custom methods is working in newer version.
sorry, I have not check the github notifications due to I'm very busy recently , I will rebase the PR when I'm free.

@neil4dong
Copy link
Author

And when I use Customized Method , I feel that is also a good way to integrate Mybatis Into springdata. event better.
we can discuss here.

`

import xxxxxx.xxxxx.xxxxx.MybatisConfig;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryComposition;
import org.springframework.lang.NonNull;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

//Mybatis load before this
@Import(MybatisConfig.class)
@Configuration
@EnableJdbcRepositories(basePackages = "com.dmall.dfp.invoicing.dao.springdatajdbc", repositoryFactoryBeanClass = SpringDataJdbcConfig.MybatisWithJdbcRepository.class)
public class SpringDataJdbcConfig extends AbstractJdbcConfiguration {

    /**
     * 集成mybatis 和 spring-data-jdbc.    混合在一个类中使用.     
     * 只需要SpringDataJdbc的Repository上implement Mybatis的接口就可以路由到mybatis
     *
     * 使用此类时,需要手动Import mybatis 的配置,使mybatis先于此类加载
     */
    public static class MybatisWithJdbcRepository<T extends Repository<S, ID>, S, ID extends Serializable> extends JdbcRepositoryFactoryBean<T, S, ID> implements ApplicationContextAware {
        private ApplicationContext applicationContext;

        public static Class<? extends Annotation> annotationClass = Mapper.class;

        /**
         * Creates a new {@link MybatisWithJdbcRepository} for the given repository interface.
         *
         * @param repositoryInterface must not be {@literal null}.
         */
        protected MybatisWithJdbcRepository(Class<? extends T> repositoryInterface) {
            super(repositoryInterface);
        }

        @Override
        public void afterPropertiesSet() {
            Class<? extends T> repositoryInterface = getObjectType();
            for (Type genericInterface : repositoryInterface.getGenericInterfaces()) {
                Class<?> clazz = genericInterface instanceof Class ? ((Class) genericInterface) : null;
                if (clazz != null && clazz.isAnnotationPresent(annotationClass)) {
                    Object bean = applicationContext.getBean(clazz);
                    setRepositoryFragments(RepositoryComposition.RepositoryFragments.just(bean));
                }
            }
            super.afterPropertiesSet();
        }

        @Override
        public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }
}

@neil4dong
Copy link
Author

When we use the configuration above , We only need to make a mybatis interface and let mybatis mechanism work, then extends the interface on our Spring Data Jdbc Repository.

@schauder
Copy link
Contributor

Let's move the discussion to https://jira.spring.io/projects/DATAJDBC/issues/DATAJDBC-373 since it seems we are discussing the merit of the feature itself and not just that of the current PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants