Spring Data JPA和MyBatis

6

我想使用Spring Data JPA和MyBatis。由于MyBatis没有供应商适配器,这里的替代方案是什么?

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.abc.xyz.domain"/>
</bean>

当我尝试初始化我的应用程序时,出现了下面的异常。
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either

谢谢


9
MyBatis不是JPA的实现,无法与Spring-Data-JPA一起使用。 - JB Nizet
很有趣,你提到了这个。我碰巧认为MyBatis是一个实现JPA的ORM,就像Hibernate一样。如果是这种情况,那么这个问题就无效了。谢谢。 - srini
您可以展示完整的Spring XML文件和完整的堆栈跟踪信息。这将有助于识别问题。 - Karthikeyan
1
你可以同时使用它们(我一直在这样做),但根据你上面提供的信息,这还不足以确定你想要实现什么。 - Ian Lim
6个回答

11

Mybatis不实现JPA。Mybatis不是ORM框架。JPA是由Hibernate、Toplink和Eclipselink实现的ORM规范。由于Mybatis没有实现JPA,它不在JPA提供商列表中。因此,您不能将Mybatis用作JPA框架。Mybatis是一个完全不同的数据映射器框架,与JPA相比。在JPA和ORM框架中,您将对象/实体映射到相应的SQL表,并且只有在使用其本机查询时才直接使用表格而非对象。在Mybatis中,您直接操作SQL数据。希望这清楚地解释了Mybatis和JPA之间的区别。因此,当您想要将Mybatis与Spring Data一起使用时,可以独立使用Spring Data Mybatis,而不是Spring Data JPA。


1
但是MyBatis是一个ORM框架。 - HibaHasan
MyBatis是一种持久化框架,它不是一个ORM框架。查看这个 @HibaHasan - Maksim Eliseev

4

Spring Data MyBatis

如果您不想使用像Spring-Data-JPA这样的JPA实现,但是您喜欢使用Spring-Data,那么您可以找到一个有用的项目Spring-Data-Mybatis

我知道这不是对您问题的精确回答,但我希望这个答案能够引起您的兴趣。


1
该项目已经过时,也没有可用的演示。作者没有提供任何信息。 - Dimitri Kopriwa
最后一次提交是在2016年10月6日,所以他们正在继续开发项目。我在问题选项卡中看到有些主题是在6分钟前关闭的。如果该项目现在无法正常工作,可能会出现Spring-Data版本问题。请尝试提出一个问题。 - user2053904
我是提出问题的人。该插件已经过时,无法与最新的Spring版本兼容,我已经处理了一个多星期,但作者并没有真正回答我的问题。如果您是该插件的用户,也许您无法回答当前的问题 :) - Dimitri Kopriwa

2

为什么不试试spring-data-jpa-extra呢?

它为spring-data-jpa提供了一种动态查询解决方案,就像mybatis一样,但比mybatis更容易使用。

我认为你会喜欢它的:)


1

我已经更新了存储库链接,因为新版本现在在 easyJet 命名空间中。该项目很稳定。 - Dimitri Kopriwa

1

这是Spring框架中Mybatis和JPA的配置。Mybatis和JPA是不同的框架,因此您不能将Mybatis用作JPA框架。如果您无法理解配置,请随时提出任何问题。

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;
    }
}

这是 build.gradle 文件

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()
}



0

一位著名人士也将mybatis定义为orm。
(https://martinfowler.com/bliki/OrmHate.html => 诸如iBatis、Hibernate和Active Record等广泛可用的开源ORM)
与mybatis相比,jpa在速度、调试、N+1问题等方面更糟糕。
如果只列出不好的事情,超过10个。
在许多方面,它比mybatis更糟糕。
Jpa在Java中是最差的选择。
没有jpa的优势。
YouTube上也列出了jpa的缺点。https://youtu.be/EpYBP7EZ8Y4?t=370 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>
致@Maksim Eliseev
https://en.wikipedia.org/wiki/Persistence_framework Jpa(Java持久化API)也是持久化框架


我不明白为什么版主要删除这篇帖子。 这只是一篇信息性的文章。 请在评论中留下您的理由。 - Zombi e
我应该说JPA总是好吗?如果你认为JPA好,请告诉我原因。 - Zombi e

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接