[英]Spring Data JPA & MyBatis
我正在嘗試將 Spring Data JPA 與 MyBatis 一起使用。 既然沒有 MyBatis 的 Vendor Adapter,那么這里的替代方案是什么?
<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
謝謝
Mybatis 沒有實現 JPA。 Mybatis 不是 ORM 框架。 JPA 是 ORM 規范,由 Hibernate、Toplink、Eclipselink 實現。 由於 Mybatis 不實現 JPA,所以它不屬於 JPA 提供者的列表。 因此,您不能將 mybatis 用作 JPA 框架。 Mybatis 是一個數據映射器框架,與 JPA 相比是完全不同的框架。 在 JPA 和 ORM 框架中,您將對象/實體映射到相應的 sql 表,並且您處理對象而不是直接處理表,除非您使用它們的本機查詢。 在 mybatis 中,你直接玩 sql 數據.. 希望這可以清除 mybatis 和 JPA 之間的區別。 因此,當您想要帶有 spring 數據的 mybatis 時,您可以獨立使用 spring 數據 mybatis 而不是 spring 數據 JPA。
如果你不希望使用JPA實現像彈簧數據的JPA模塊,但你喜歡使用Spring的數據,你可以找到彈簧數據MyBatis的一個有用的項目。
我知道這不是您問題的准確答案,但我希望這個答案很有趣。
我正在使用這個項目: https : //github.com/hatunet/spring-data-mybatis
它非常適合 spring-data-mybatis 並且它還有分頁存儲庫。
在生產項目上工作得很好。
更新 08/2020
項目轉移到另一個網絡空間並發展: https : //github.com/easybest/spring-data-mybatis
下面是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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.