Skip to content

Commit 95b227c

Browse files
committed
Added mySQL support
1 parent 60c5cd3 commit 95b227c

File tree

14 files changed

+891
-4
lines changed

14 files changed

+891
-4
lines changed

src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.slf4j.LoggerFactory;
1010
import org.springframework.beans.factory.InitializingBean;
1111
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.beans.factory.annotation.Qualifier;
1213
import org.springframework.http.HttpStatus;
1314
import org.springframework.http.ResponseEntity;
1415
import org.springframework.jdbc.core.JdbcTemplate;
@@ -37,7 +38,7 @@ public class ClinicActivityController implements InitializingBean {
3738
@Autowired
3839
public ClinicActivityController(ClinicActivityDataService dataService,
3940
ClinicActivityLogRepository repository,
40-
JdbcTemplate jdbcTemplate) {
41+
@Qualifier("postgresJdbcTemplate") JdbcTemplate jdbcTemplate) {
4142
this.dataService = dataService;
4243
this.repository = repository;
4344
this.jdbcTemplate = jdbcTemplate;

src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityDataService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Qualifier;
78
import org.springframework.samples.petclinic.model.ClinicActivityLog;
89
import org.springframework.stereotype.Service;
910
import org.springframework.transaction.PlatformTransactionManager;
@@ -57,9 +58,9 @@ public class ClinicActivityDataService {
5758

5859
@Autowired
5960
public ClinicActivityDataService(ClinicActivityLogRepository repository,
60-
JdbcTemplate jdbcTemplate,
61-
DataSource dataSource,
62-
PlatformTransactionManager transactionManager) {
61+
@Qualifier("postgresJdbcTemplate") JdbcTemplate jdbcTemplate,
62+
@Qualifier("postgresDataSource") DataSource dataSource,
63+
@Qualifier("postgresTransactionManager") PlatformTransactionManager transactionManager) {
6364
this.repository = repository;
6465
this.jdbcTemplate = jdbcTemplate;
6566
this.dataSource = dataSource;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.springframework.samples.petclinic.config;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.boot.jdbc.DataSourceBuilder;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.annotation.Primary;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
12+
import javax.sql.DataSource;
13+
14+
@Configuration
15+
public class DatabaseConfig {
16+
17+
@Primary
18+
@Bean(name = "postgresDataSource")
19+
@ConfigurationProperties("app.datasource.postgres")
20+
public DataSource postgresDataSource() {
21+
return DataSourceBuilder.create().type(HikariDataSource.class).build();
22+
}
23+
24+
@Bean(name = "mysqlDataSource")
25+
@ConfigurationProperties("app.datasource.mysql")
26+
public DataSource mysqlDataSource() {
27+
return DataSourceBuilder.create().type(HikariDataSource.class).build();
28+
}
29+
30+
@Primary
31+
@Bean(name = "postgresJdbcTemplate")
32+
public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDataSource") DataSource dataSource) {
33+
return new JdbcTemplate(dataSource);
34+
}
35+
36+
@Bean(name = "mysqlJdbcTemplate")
37+
public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
38+
return new JdbcTemplate(dataSource);
39+
}
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.springframework.samples.petclinic.config;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
import org.springframework.orm.jpa.JpaTransactionManager;
9+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
10+
import org.springframework.transaction.PlatformTransactionManager;
11+
import org.springframework.transaction.annotation.EnableTransactionManagement;
12+
13+
import jakarta.persistence.EntityManagerFactory;
14+
import javax.sql.DataSource;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
@Configuration
19+
@EnableTransactionManagement
20+
@EnableJpaRepositories(
21+
entityManagerFactoryRef = "mysqlEntityManagerFactory",
22+
transactionManagerRef = "mysqlTransactionManager",
23+
basePackages = {"org.springframework.samples.petclinic.patientrecords"}
24+
)
25+
public class MySqlConfig {
26+
27+
@Bean(name = "mysqlEntityManagerFactory")
28+
public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(
29+
EntityManagerFactoryBuilder builder,
30+
@Qualifier("mysqlDataSource") DataSource dataSource) {
31+
32+
Map<String, Object> properties = new HashMap<>();
33+
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
34+
properties.put("hibernate.hbm2ddl.auto", "none");
35+
properties.put("hibernate.show_sql", false);
36+
37+
return builder
38+
.dataSource(dataSource)
39+
.packages("org.springframework.samples.petclinic.model")
40+
.persistenceUnit("mysql")
41+
.properties(properties)
42+
.build();
43+
}
44+
45+
@Bean(name = "mysqlTransactionManager")
46+
public PlatformTransactionManager mysqlTransactionManager(
47+
@Qualifier("mysqlEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
48+
return new JpaTransactionManager(entityManagerFactory);
49+
}
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.springframework.samples.petclinic.config;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Primary;
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9+
import org.springframework.orm.jpa.JpaTransactionManager;
10+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
11+
import org.springframework.transaction.PlatformTransactionManager;
12+
import org.springframework.transaction.annotation.EnableTransactionManagement;
13+
14+
import jakarta.persistence.EntityManagerFactory;
15+
import javax.sql.DataSource;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
@Configuration
20+
@EnableTransactionManagement
21+
@EnableJpaRepositories(
22+
entityManagerFactoryRef = "postgresEntityManagerFactory",
23+
transactionManagerRef = "postgresTransactionManager",
24+
basePackages = {
25+
"org.springframework.samples.petclinic.owner",
26+
"org.springframework.samples.petclinic.vet",
27+
"org.springframework.samples.petclinic.clinicactivity"
28+
}
29+
)
30+
public class PostgresConfig {
31+
32+
@Primary
33+
@Bean(name = "postgresEntityManagerFactory")
34+
public LocalContainerEntityManagerFactoryBean postgresEntityManagerFactory(
35+
EntityManagerFactoryBuilder builder,
36+
@Qualifier("postgresDataSource") DataSource dataSource) {
37+
38+
Map<String, Object> properties = new HashMap<>();
39+
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
40+
properties.put("hibernate.hbm2ddl.auto", "none");
41+
properties.put("hibernate.show_sql", false);
42+
43+
return builder
44+
.dataSource(dataSource)
45+
.packages(
46+
"org.springframework.samples.petclinic.owner",
47+
"org.springframework.samples.petclinic.vet",
48+
"org.springframework.samples.petclinic.model"
49+
)
50+
.persistenceUnit("postgres")
51+
.properties(properties)
52+
.build();
53+
}
54+
55+
@Primary
56+
@Bean(name = "postgresTransactionManager")
57+
public PlatformTransactionManager postgresTransactionManager(
58+
@Qualifier("postgresEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
59+
return new JpaTransactionManager(entityManagerFactory);
60+
}
61+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.springframework.samples.petclinic.model;
2+
3+
import jakarta.persistence.*;
4+
import java.time.LocalDateTime;
5+
6+
/**
7+
* Patient Record entity for MySQL database
8+
* Represents veterinary patient treatment records
9+
*/
10+
@Entity
11+
@Table(name = "patient_records")
12+
public class PatientRecord extends BaseEntity {
13+
14+
@Column(name = "treatment_type", nullable = false)
15+
private String treatmentType;
16+
17+
@Column(name = "patient_weight", nullable = false)
18+
private Integer patientWeight;
19+
20+
@Column(name = "visit_date", nullable = false)
21+
private LocalDateTime visitDate;
22+
23+
@Column(name = "treatment_completed", nullable = false)
24+
private Boolean treatmentCompleted;
25+
26+
@Column(name = "medical_notes", columnDefinition = "TEXT")
27+
private String medicalNotes;
28+
29+
// Default constructor
30+
public PatientRecord() {
31+
}
32+
33+
// Constructor with all fields
34+
public PatientRecord(String treatmentType, Integer patientWeight, LocalDateTime visitDate,
35+
Boolean treatmentCompleted, String medicalNotes) {
36+
this.treatmentType = treatmentType;
37+
this.patientWeight = patientWeight;
38+
this.visitDate = visitDate;
39+
this.treatmentCompleted = treatmentCompleted;
40+
this.medicalNotes = medicalNotes;
41+
}
42+
43+
// Getters and Setters
44+
public String getTreatmentType() {
45+
return treatmentType;
46+
}
47+
48+
public void setTreatmentType(String treatmentType) {
49+
this.treatmentType = treatmentType;
50+
}
51+
52+
public Integer getPatientWeight() {
53+
return patientWeight;
54+
}
55+
56+
public void setPatientWeight(Integer patientWeight) {
57+
this.patientWeight = patientWeight;
58+
}
59+
60+
public LocalDateTime getVisitDate() {
61+
return visitDate;
62+
}
63+
64+
public void setVisitDate(LocalDateTime visitDate) {
65+
this.visitDate = visitDate;
66+
}
67+
68+
public Boolean getTreatmentCompleted() {
69+
return treatmentCompleted;
70+
}
71+
72+
public void setTreatmentCompleted(Boolean treatmentCompleted) {
73+
this.treatmentCompleted = treatmentCompleted;
74+
}
75+
76+
public String getMedicalNotes() {
77+
return medicalNotes;
78+
}
79+
80+
public void setMedicalNotes(String medicalNotes) {
81+
this.medicalNotes = medicalNotes;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return "PatientRecord{" +
87+
"id=" + getId() +
88+
", treatmentType='" + treatmentType + '\'' +
89+
", patientWeight=" + patientWeight +
90+
", visitDate=" + visitDate +
91+
", treatmentCompleted=" + treatmentCompleted +
92+
", medicalNotes='" + medicalNotes + '\'' +
93+
'}';
94+
}
95+
}

0 commit comments

Comments
 (0)