Spring Data JPA & MyBatis

后端 未结 5 2151
无人共我
无人共我 2021-02-10 00:02

I am trying to use Spring Data JPA with MyBatis. Since there isnt a Vendor Adapter for MyBatis, what is the alternative here?



        
5条回答
  •  说谎
    说谎 (楼主)
    2021-02-10 00:17

    Here is the configuration of mybatis and jpa in spring framework. Mybatis and jpa are different framework so you cannot use mybatis as a JPA framework. Feel free to ask any question if you cannot catch up the configuration.

    package com.mastering.springbatch.config;
    
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.JpaVendorAdapter;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    import javax.sql.DataSource;
    
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackages = {"com.mastering.springbatch.dao",
                    "com.mastering.springbatch.repository"})
    @EntityScan("com.mastering.springbatch.entity")
    public class DataConfig {
        private final String ENTITY_PACKAGE = "com.mastering.springbatch.entity";
        private DriverManagerDataSource dataSource;
    
        @Primary
        @Bean(value = "customDataSource")
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            this.dataSource = dataSource;
            return dataSource;
        }
    
        @Primary
        @Bean(value = "entityManagerFactory")
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean emf =
                    new LocalContainerEntityManagerFactoryBean();
            emf.setPackagesToScan(ENTITY_PACKAGE);
            emf.setDataSource(dataSource());
            emf.setJpaVendorAdapter(jpaVendorAdapter());
            return emf;
        }
    
        @Primary
        @Bean(value = "sqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory() throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource());
            return factoryBean.getObject();
        }
    
    
        @Primary
        @Bean(name = "transactionManager")
        public PlatformTransactionManager transactionManager() {
            JpaTransactionManager tm = new JpaTransactionManager();
            tm.setEntityManagerFactory(entityManagerFactory().getObject());
            return tm;
        }
    
        private JpaVendorAdapter jpaVendorAdapter() {
            HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
            jpaVendorAdapter.setShowSql(true);
            jpaVendorAdapter.setGenerateDdl(true);
            jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
            return jpaVendorAdapter;
        }
    }
    
    

    here is the build.gradle file

    buildscript {
        ext {
            springBootVersion = '2.1.8.RELEASE'
            springBootDepManagementVersion = '1.0.8.RELEASE'
        }
        repositories {
            mavenLocal()
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
            classpath "io.spring.gradle:dependency-management-plugin:${springBootDepManagementVersion}"
        }
    }
    
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'java'
    apply plugin: 'idea'
    
    
    group 'com.learning'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 1.8
    
    configurations {
        implementation.exclude module: "spring-boot-starter-tomcat"
    }
    
    repositories {
        mavenLocal()
        mavenCentral()
    }
    
    dependencies {
        compile 'org.mybatis:mybatis:3.5.0'
        compile 'org.mybatis:mybatis-spring:2.0.0'
    
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        implementation("org.springframework.boot:spring-boot-starter-batch")
        implementation("org.springframework.boot:spring-boot-starter-web")
        
        implementation("mysql:mysql-connector-java:8.0.14")
        
    //    implementation("io.springfox:springfox-swagger2:2.7.0")
    //    implementation("io.springfox:springfox-swagger-ui:2.7.0")
        
        implementation("org.projectlombok:lombok:1.18.10")
        annotationProcessor("org.projectlombok:lombok:1.18.10")
        compile group: 'commons-io', name: 'commons-io', version: '2.6'
    
        testAnnotationProcessor("org.projectlombok:lombok:1.18.10")
        testCompile("junit:junit:4.12")
        testCompile("org.mockito:mockito-core:2.1.0")
        testCompile("org.springframework.boot:spring-boot-starter-test")
    
    }
    
    springBoot {
        buildInfo()
    }
    
    

提交回复
热议问题