Spring Data JPA和MyBatis

ars1skjm  于 2023-11-19  发布在  Spring
关注(0)|答案(5)|浏览(137)

我尝试在MyBatis上使用Spring Data JPA。由于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


谢谢

izkcnapc

izkcnapc1#

为什么不试试spring-data-jpa-extra
它为spring-data-jpa提供了一个类似mybatis的动态查询解决方案,但比mybatis简单得多。
我想你会喜欢的:)

mlmc2os5

mlmc2os52#

这里是mybatis和jpa在spring框架中的配置。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()
}

stszievb

stszievb3#

Spring-Data-Mybatis Hatunet版本

我正在使用这个项目:https://github.com/hatunet/spring-data-mybatis
它非常适合spring-data-mybatis,并且它也有分页存储库。
能很好地完成生产项目。

更新08/2020

该项目转移到另一个网络空间并发展:https://github.com/easybest/spring-data-mybatis

zengzsys

zengzsys4#

Mybatis不实现JPA。Mybatis不是ORM Framework。JPA是ORM Specification,由Hibernate,Toplink,Replink实现。由于Mybatis不实现JPA,因此它不属于JPA提供者列表。因此您不能将Mybatis用作JPA框架。Mybatis是一个数据Map器框架,与JPA相比是完全不同的框架。在JPA和ORM框架中,你将对象/实体Map到相应的sql表,你直接处理对象而不是表,除非你使用它们的原生查询。在mybatis中,你直接处理sql数据。。希望这消除了mybatis和JPA之间的差异。因此,当你想要mybatis与spring数据时,你可以独立使用spring数据mybatis而不是spring数据JPA。

aiqt4smr

aiqt4smr5#

Spring Data MyBatis

如果你不想使用像Spring-Data-JPA模块这样的JPA实现,但你喜欢使用Spring-Data,你可以找到Spring-Data-Mybatis一个有用的项目。
我知道这不是对你的问题的准确回答,但我希望这个答案可以是有趣的。

相关问题