我正在尝试为spring boot jpa应用程序创建自定义事务管理器。 我的配置类是
`package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
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.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "omniEntityManagerFactory",
transactionManagerRef = "omniTransactionManager",
basePackages = "com.example.demo.repository"
)
public class OmniDataSourceConfig {
@Value("${omni.datasource.driver-class-name}")
private String dbDriver;
@Value("${omni.datasource.url}")
private String dbConnUrl;
@Value("${omni.datasource.username}")
private String dbUsername;
@Value("${omni.datasource.password}")
private String dbPassword;
@Value("${spring.jpa.hibernate.ddl-auto}")
private String hibernateDDL;
@Value("${omni.jpa.properties.hibernate.dialect}")
private String hibernateDialect;
@Bean
@Primary
public DataSource omniDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(dbDriver);
dataSource.setUrl(dbConnUrl);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean omniEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(omniDataSource());
factory.setPackagesToScan("com.example.demo.model");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factory.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", hibernateDDL);
properties.put("hibernate.dialect", hibernateDialect);
factory.setJpaPropertyMap(properties);
return factory;
}
@Bean
@Primary
public PlatformTransactionManager omniTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(omniEntityManagerFactory().getObject());
return transactionManager;
}
}
`
存储库为
package com.example.demo.repository;
import com.example.demo.model.Child;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ChildRepository extends JpaRepository<Child, Integer> {
}
Application.properties
data.spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
omni.datasource.driver-class-name=com.mysql.jdbc.Driver
omni.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true
omni.datasource.username=root
omni.datasource.password=root
omni.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
实体类为
package com.example.demo.model;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "Child")
public class Child {
@Id
@GeneratedValue(generator = "sequence", strategy = GenerationType.IDENTITY)
@SequenceGenerator(name = "sequence", allocationSize = 1)
int i;
@Column(name = "firstName")
String firstName;
@Column(name = "lastName")
String lastName;
@Temporal(TemporalType.TIMESTAMP)
Date date;
public Child(String firstName, String lastName, Date date) {
this.firstName = firstName;
this.lastName = lastName;
this.date = date;
}
}
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>demoCore</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
错误
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
奇怪的是,如果我尝试通过Spring Boot创建配置类,即不使用appliaction .properties中的相同键和值手动创建事务管理器,则它可以正常工作。 我想念什么?