spring boot+spring data jpa+多数据源+Jsr310JpaConverters

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
//for jsr310 java 8 java.time.*
@EntityScan(
basePackageClasses = {Application.class, Jsr310JpaConverters.class}
)
@EnableJpaAuditing
public class Application {

public static void main(String args) {
SpringApplication.run(Application.class, args);
}
}

spring:
datasource:
tms:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/aaa?useSSL=false&characterEncoding=utf-8
username: aaa
password: aaa
testWhileIdle: true
validationQuery: SELECT 1
demo:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bbb?useSSL=false&characterEncoding=utf-8
username: bbb
password: bbb
testWhileIdle: true
validationQuery: SELECT 1
jpa:
show-sql: true
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
enable_lazy_load_no_trans: true
@Configuration
public class DataSourceConfig {
@Bean(name = "tmsdataSource")
@Qualifier("tmsDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.tms")
public DataSource tmsDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "demoDataSource")
@Qualifier("demoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.demo")
public DataSource demoDataSource() {
return DataSourceBuilder.create().build();
}
}
@Configuration
@EnableTransactionManagement
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "tmsEntityManagerFactory",
transactionManagerRef = "tmsTransactionManager",
basePackages = {"com.hisun.tms.sys.repository"},//设置Repository所在位置
repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)
public class TmsConfig {

@Autowired
@Qualifier("tmsDataSource")
private DataSource tmsDataSource;
@Autowired
private JpaProperties jpaProperties;

@Primary
@Bean(name = "tmsEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return tmsEntityManagerFactory(builder).getObject().createEntityManager();
}

@Primary
@Bean(name = "tmsEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean tmsEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(tmsDataSource)
.properties(getVendorProperties(tmsDataSource))
.packages("com.xxx.tms.sys.model") //设置实体类所在位置
.persistenceUnit("tmsPersistenceUnit")
.build();
}

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

@Primary
@Bean(name = "tmsTransactionManager")
public PlatformTransactionManager tmsTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(tmsEntityManagerFactory(builder).getObject());
}

}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "demoEntityManagerFactory",
transactionManagerRef = "demoTransactionManager",
basePackages = {"com.hisun.tms.demo.repository"},//设置Repository所在位置
repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class
)
public class DemoConfig {
@Autowired
@Qualifier("demoDataSource")
private DataSource demoDataSource;
@Autowired
private JpaProperties jpaProperties;

@Bean(name = "demoEntityManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return demoEntityManagerFactory(builder).getObject().createEntityManager();
}

@Bean(name = "demoEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean demoEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(demoDataSource)
.properties(getVendorProperties(demoDataSource))
.packages("com.xxx.tms.demo.model") //设置实体类所在位置
.persistenceUnit("demoPersistenceUnit")
.build();
}

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

@Bean(name = "demoTransactionManager")
PlatformTransactionManager demoTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(demoEntityManagerFactory(builder).getObject());
}
}
@Entity
@Table(name = "sys_example")
public class Example extends AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@Column(name = "name")
@NotEmpty(message = "*Please provide a name")
private String name;

@Column(name = "position")
private String position;

@Column(name = "office")
private String office;

@Column(name = "start_date")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startDate;

@Column(name = "salary", precision = 15, scale = 2)
private BigDecimal salary;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}

public String getOffice() {
return office;
}

public void setOffice(String office) {
this.office = office;
}

public LocalDateTime getStartDate() {
return startDate;
}

public void setStartDate(LocalDateTime startDate) {
this.startDate = startDate;
}

public BigDecimal getSalary() {
return salary;
}

public void setSalary(BigDecimal salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Example{" +
"id=" + id +
", name='" + name + '\'' +
", position='" + position + '\'' +
", office='" + office + '\'' +
", startDate=" + startDate +
", salary=" + salary +
'}';
}
}
public interface ExampleRepository extends JpaRepository<Example, Integer> {
}
已邀请:

passion - 80后IT♂

赞同来自:

错误原因是我在Application中使用了
@EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class),此举是为了启用DataTablesRepository工厂,我应该想办法将其在每个数据源中分别注入。。。而不是在Application中启用

passion - 80后IT♂

赞同来自:

我想把这个问题关闭,重新提一个问题,上面的问题解决了,但是来了新的问题
单个数据源时,Jsr310JpaConverters是正常能对java 8 的LocalDataTime在做序列化前做转换的,但是配置多数据源的情况下,LocalDataTime没有使用Jsr310JpaConverters做序列化前的转换,导致LocalDataTime反序列化失败,这个问题暂时没解决

要回复问题请先登录注册