Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class ClinicActivityController implements InitializingBean {
@Autowired
public ClinicActivityController(ClinicActivityDataService dataService,
ClinicActivityLogRepository repository,
JdbcTemplate jdbcTemplate) {
@Qualifier("postgresJdbcTemplate") JdbcTemplate jdbcTemplate) {
this.dataService = dataService;
this.repository = repository;
this.jdbcTemplate = jdbcTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.samples.petclinic.model.ClinicActivityLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
Expand Down Expand Up @@ -57,9 +58,9 @@ public class ClinicActivityDataService {

@Autowired
public ClinicActivityDataService(ClinicActivityLogRepository repository,
JdbcTemplate jdbcTemplate,
DataSource dataSource,
PlatformTransactionManager transactionManager) {
@Qualifier("postgresJdbcTemplate") JdbcTemplate jdbcTemplate,
@Qualifier("postgresDataSource") DataSource dataSource,
@Qualifier("postgresTransactionManager") PlatformTransactionManager transactionManager) {
this.repository = repository;
this.jdbcTemplate = jdbcTemplate;
this.dataSource = dataSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.springframework.samples.petclinic.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {

@Primary
@Bean(name = "postgresDataSource")
@ConfigurationProperties("app.datasource.postgres")
public DataSource postgresDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

@Bean(name = "mysqlDataSource")
@ConfigurationProperties("app.datasource.mysql")
public DataSource mysqlDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

@Primary
@Bean(name = "postgresJdbcTemplate")
public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

@Bean(name = "mysqlJdbcTemplate")
public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.springframework.samples.petclinic.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "mysqlEntityManagerFactory",
transactionManagerRef = "mysqlTransactionManager",
basePackages = {"org.springframework.samples.petclinic.patientrecords"}
)
public class MySqlConfig {

@Bean(name = "mysqlEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("mysqlDataSource") DataSource dataSource) {

Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("hibernate.show_sql", false);

return builder
.dataSource(dataSource)
.packages("org.springframework.samples.petclinic.model")
.persistenceUnit("mysql")
.properties(properties)
.build();
}

@Bean(name = "mysqlTransactionManager")
public PlatformTransactionManager mysqlTransactionManager(
@Qualifier("mysqlEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.springframework.samples.petclinic.config;

import org.springframework.beans.factory.annotation.Qualifier;
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;

import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "postgresEntityManagerFactory",
transactionManagerRef = "postgresTransactionManager",
basePackages = {
"org.springframework.samples.petclinic.owner",
"org.springframework.samples.petclinic.vet",
"org.springframework.samples.petclinic.clinicactivity"
}
)
public class PostgresConfig {

@Primary
@Bean(name = "postgresEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean postgresEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("postgresDataSource") DataSource dataSource) {

Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("hibernate.show_sql", false);

return builder
.dataSource(dataSource)
.packages(
"org.springframework.samples.petclinic.owner",
"org.springframework.samples.petclinic.vet",
"org.springframework.samples.petclinic.model"
)
.persistenceUnit("postgres")
.properties(properties)
.build();
}

@Primary
@Bean(name = "postgresTransactionManager")
public PlatformTransactionManager postgresTransactionManager(
@Qualifier("postgresEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.springframework.samples.petclinic.model;

import jakarta.persistence.*;
import java.time.LocalDateTime;

/**
* Patient Record entity for MySQL database
* Represents veterinary patient treatment records
*/
@Entity
@Table(name = "patient_records")
public class PatientRecord extends BaseEntity {

@Column(name = "treatment_type", nullable = false)
private String treatmentType;

@Column(name = "patient_weight", nullable = false)
private Integer patientWeight;

@Column(name = "visit_date", nullable = false)
private LocalDateTime visitDate;

@Column(name = "treatment_completed", nullable = false)
private Boolean treatmentCompleted;

@Column(name = "medical_notes", columnDefinition = "TEXT")
private String medicalNotes;

// Default constructor
public PatientRecord() {
}

// Constructor with all fields
public PatientRecord(String treatmentType, Integer patientWeight, LocalDateTime visitDate,
Boolean treatmentCompleted, String medicalNotes) {
this.treatmentType = treatmentType;
this.patientWeight = patientWeight;
this.visitDate = visitDate;
this.treatmentCompleted = treatmentCompleted;
this.medicalNotes = medicalNotes;
}

// Getters and Setters
public String getTreatmentType() {
return treatmentType;
}

public void setTreatmentType(String treatmentType) {
this.treatmentType = treatmentType;
}

public Integer getPatientWeight() {
return patientWeight;
}

public void setPatientWeight(Integer patientWeight) {
this.patientWeight = patientWeight;
}

public LocalDateTime getVisitDate() {
return visitDate;
}

public void setVisitDate(LocalDateTime visitDate) {
this.visitDate = visitDate;
}

public Boolean getTreatmentCompleted() {
return treatmentCompleted;
}

public void setTreatmentCompleted(Boolean treatmentCompleted) {
this.treatmentCompleted = treatmentCompleted;
}

public String getMedicalNotes() {
return medicalNotes;
}

public void setMedicalNotes(String medicalNotes) {
this.medicalNotes = medicalNotes;
}

@Override
public String toString() {
return "PatientRecord{" +
"id=" + getId() +
", treatmentType='" + treatmentType + '\'' +
", patientWeight=" + patientWeight +
", visitDate=" + visitDate +
", treatmentCompleted=" + treatmentCompleted +
", medicalNotes='" + medicalNotes + '\'' +
'}';
}
}
Loading
Loading