[英]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.