spring-boot配置jpa多数据源,遇到entityManagerFactory‘ that could not be found

网上有说是pom.xml里面版本问题,但无论我怎么试都没成功

org.springframework.boot
spring-boot-starter-data-jpa

运气好百度到一篇相关的
配置数据源的文件,下面是导致报错的例子

package com.Config;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.dao1",
		entityManagerFactoryRef = "entityManagerFactoryBeanOne",
		transactionManagerRef = "platformTransactionManagerOne")
public class JpaConfigOne {
    @Resource(name="dsOne")
	@Autowired
	DataSource dsOne;
	@Autowired
	JpaProperties jpaProperties;


 
	@Bean
	@Primary
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanOne(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dsOne)
				.properties(jpaProperties.getProperties())
				.packages("com.pojo")
				.persistenceUnit("pul")
				.build();
	}
	@Bean
	public PlatformTransactionManager platformTransactionManagerOne(EntityManagerFactoryBuilder builder) {
		LocalContainerEntityManagerFactoryBean factoryOne= entityManagerFactoryBeanOne(builder);
		return new JpaTransactionManager(factoryOne.getObject());
	}

}

后来我在数据源dsOne上面改了一行,改成@Qualifier(“dataSource1”)

package com.Config;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.dao1",
		entityManagerFactoryRef = "entityManagerFactoryBeanOne",
		transactionManagerRef = "platformTransactionManagerOne")
public class JpaConfigOne {
//	@Resource(name="dsOne")
	@Autowired
	@Qualifier("dataSource1")
	DataSource dsOne;
	@Autowired
	JpaProperties jpaProperties;


	@Bean
	@Primary
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanOne(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dsOne)
				.properties(jpaProperties.getProperties())
				.packages("com.pojo")
				.persistenceUnit("pul")
				.build();
	}
	@Bean
	public PlatformTransactionManager platformTransactionManagerOne(EntityManagerFactoryBuilder builder) {
		LocalContainerEntityManagerFactoryBean factoryOne= entityManagerFactoryBeanOne(builder);
		return new JpaTransactionManager(factoryOne.getObject());
	}

}

然后配置数据源的文件也加一行,里面有@Qualifier(“dataSource1”)就成功了

package com.Config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration
public class DataSourceConfig {
	@Bean
	@Qualifier("dataSource1")
	@Primary
	@ConfigurationProperties("spring.datasource.one")
	DataSource dsOne() {
		return DruidDataSourceBuilder.create().build();
	}
	@Bean
	@Qualifier("dataSource2")
	@ConfigurationProperties("spring.datasource.two")
	DataSource dsTwo() {
		return DruidDataSourceBuilder.create().build();
	}
}

下面是完整的项目代码,
spring-boot配置jpa多数据源,遇到entityManagerFactory‘ that could not be found_第1张图片
pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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>

  <groupId>com.tgb</groupId>
  <artifactId>test3</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>test3 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

 
  <parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid-spring-boot-starter</artifactId>
		<version>1.1.10</version>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</scope>
	</dependency>
	
	
	
	
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
     <dependency>
	   	<groupId>org.springframework.boot</groupId>
	   	<artifactId>spring-boot-starter-web</artifactId>
	 </dependency>
	 <dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.9</version>
	 </dependency>	
	 <dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</scope>
	</dependency>
	
	<dependency>
	   <groupId>org.hibernate</groupId>
	    <artifactId>hibernate-entitymanager</artifactId>
	    <version>5.0.3.Final</version>
	</dependency>
	


	
	<dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>

  </dependencies>




  <build>
    <finalName>test3</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

application.properties



spring.datasource.one.url=jdbc:mysql://localhost:3306/springtest?characterEncoding=UTF-8&useSSL=false
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.username=root
spring.datasource.one.password=1234567

spring.datasource.two.url=jdbc:mysql://localhost:3306/scrapy_news?characterEncoding=UTF-8&useSSL=false
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.username=root
spring.datasource.two.password=1234567


spring.jpa.hibernate.ddl-auto=update
spring.jpa.database:mysql
spring.jpa.properties.show-sql=true

User.java

package com.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "chapter05")
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + "]";
	}
}

BootApplication.java

package com.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@ComponentScan("com")
@SpringBootApplication()
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,WebMvcAutoConfiguration.class })
public class BootApplication {


    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);

      
    }
}

UserDao2.java

package com.dao2;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pojo.User;

public interface UserDao2 extends JpaRepository<User,Integer>{

}

UserDao.java

package com.dao1;

import org.springframework.data.jpa.repository.JpaRepository;

import com.pojo.User;

public interface UserDao extends JpaRepository<User,Integer>{

}

UserController.java

package com.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dao1.UserDao;
import com.dao2.UserDao2;
import com.pojo.User;

@RestController
public class UserController {
	@Autowired
	UserDao userDao;
	@Autowired
	UserDao2 userDao2;
	@GetMapping("/test")
	public void test1() {
		User u1=new User();
		u1.setName("11");
		userDao.save(u1);
		User u2=new User();
		u2.setName("22");
		userDao2.save(u2);
	}
}

JpaConfigTwo.java

package com.Config;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.dao2",
		entityManagerFactoryRef = "entityManagerFactoryBeanTwo",
		transactionManagerRef = "platformTransactionManagerTwo")

public class JpaConfigTwo {
//	@Resource(name="dsTwo")
	@Autowired
	@Qualifier("dataSource2")
	DataSource dsTwo;
	@Autowired
	JpaProperties jpaProperties;
	@Bean
	LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanTwo(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dsTwo)
				.properties(jpaProperties.getProperties())
				.packages("com.pojo")
				.persistenceUnit("pul")
				.build();
	}
	@Bean
	PlatformTransactionManager platformTransactionManagerTwo(EntityManagerFactoryBuilder builder) {
		LocalContainerEntityManagerFactoryBean factoryTwo= entityManagerFactoryBeanTwo(builder);
		return new JpaTransactionManager(factoryTwo.getObject());
	}
}

JpaConfigOne.java

package com.Config;


import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.dao1",
		entityManagerFactoryRef = "entityManagerFactoryBeanOne",
		transactionManagerRef = "platformTransactionManagerOne")
public class JpaConfigOne {
//	@Resource(name="dsOne")
	@Autowired
	@Qualifier("dataSource1")
	DataSource dsOne;
	@Autowired
	JpaProperties jpaProperties;


 
	@Bean
	@Primary
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanOne(EntityManagerFactoryBuilder builder) {
		return builder.dataSource(dsOne)
				.properties(jpaProperties.getProperties())
				.packages("com.pojo")
				.persistenceUnit("pul")
				.build();
	}
	@Bean
	public PlatformTransactionManager platformTransactionManagerOne(EntityManagerFactoryBuilder builder) {
		LocalContainerEntityManagerFactoryBean factoryOne= entityManagerFactoryBeanOne(builder);
		return new JpaTransactionManager(factoryOne.getObject());
	}

}

DataSourceConfig.java

package com.Config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration
public class DataSourceConfig {
	@Bean
	@Qualifier("dataSource1")
	@Primary
	@ConfigurationProperties("spring.datasource.one")
	DataSource dsOne() {
		return DruidDataSourceBuilder.create().build();
	}
	@Bean
	@Qualifier("dataSource2")
	@ConfigurationProperties("spring.datasource.two")
	DataSource dsTwo() {
		return DruidDataSourceBuilder.create().build();
	}
}

你可能感兴趣的:(java开发小网页,java)