Skip to content

Commit dad3dd0

Browse files
authored
Merge pull request #9 from Rurutia1027/b_support-mysql
let persistent common support mysql datasource
2 parents e471537 + 639e196 commit dad3dd0

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

persistence-common/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ public class PersistenceConfig {
8888
}
8989
```
9090

91+
#### Using MySQL
92+
93+
To use MySQL as the data source, add the MySQL JDBC driver and configure the dialect:
94+
95+
**Maven dependency (in your application):**
96+
```xml
97+
<dependency>
98+
<groupId>com.mysql</groupId>
99+
<artifactId>mysql-connector-j</artifactId>
100+
<scope>runtime</scope>
101+
</dependency>
102+
```
103+
104+
**Configuration:**
105+
```java
106+
@Bean
107+
@Primary
108+
public DataSource dataSource() {
109+
DriverManagerDataSource ds = new DriverManagerDataSource();
110+
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
111+
ds.setUrl("jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8");
112+
ds.setUsername("user");
113+
ds.setPassword("password");
114+
return ds;
115+
}
116+
117+
// In sessionFactory hibernate properties:
118+
props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); // or MySQL8Dialect for MySQL 8+
119+
```
120+
121+
To run integration tests against MySQL (Testcontainers), use:
122+
`mvn test -DexcludedGroups=` (runs all tests including MySQL). By default, MySQL container tests are excluded for faster CI.
123+
91124
### 3. Define Entities
92125

93126
```java

persistence-common/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<!-- Database -->
2525
<postgresql.version>42.7.2</postgresql.version>
26+
<mysql.version>8.4.0</mysql.version>
2627

2728
<!-- Utils -->
2829
<commons-codec.version>1.16.0</commons-codec.version>
@@ -31,6 +32,7 @@
3132
<!-- Test -->
3233
<junit.version>5.10.1</junit.version>
3334
<testcontainers.version>1.20.3</testcontainers.version>
35+
<test.excludedGroups>mysql</test.excludedGroups>
3436
</properties>
3537

3638
<dependencies>
@@ -124,6 +126,13 @@
124126
<scope>test</scope>
125127
</dependency>
126128

129+
<dependency>
130+
<groupId>org.testcontainers</groupId>
131+
<artifactId>mysql</artifactId>
132+
<version>${testcontainers.version}</version>
133+
<scope>test</scope>
134+
</dependency>
135+
127136
<dependency>
128137
<groupId>org.testcontainers</groupId>
129138
<artifactId>junit-jupiter</artifactId>
@@ -145,6 +154,13 @@
145154
<scope>test</scope>
146155
</dependency>
147156

157+
<dependency>
158+
<groupId>com.mysql</groupId>
159+
<artifactId>mysql-connector-j</artifactId>
160+
<version>${mysql.version}</version>
161+
<scope>test</scope>
162+
</dependency>
163+
148164
<dependency>
149165
<groupId>org.springframework.boot</groupId>
150166
<artifactId>spring-boot-starter-test</artifactId>
@@ -169,6 +185,10 @@
169185
<groupId>org.apache.maven.plugins</groupId>
170186
<artifactId>maven-surefire-plugin</artifactId>
171187
<version>3.1.2</version>
188+
<configuration>
189+
<!-- Exclude MySQL Testcontainers tests by default; run with -DexcludedGroups= to include -->
190+
<excludedGroups>${test.excludedGroups}</excludedGroups>
191+
</configuration>
172192
</plugin>
173193
</plugins>
174194
</build>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.tus.common.domain.persistence.integration;
2+
3+
import org.junit.jupiter.api.Tag;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
7+
import org.tus.common.domain.persistence.QueryService;
8+
import org.tus.common.domain.persistence.integration.config.PersistenceTestContainerMySQLConfig;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
/**
16+
* Integration tests that run against MySQL (Testcontainers).
17+
* Run with: mvn test -Dgroups=mysql
18+
*/
19+
@Tag("mysql")
20+
@SpringJUnitConfig(classes = PersistenceTestContainerMySQLConfig.class)
21+
public class QueryServiceMySQLIT {
22+
23+
@Autowired
24+
private QueryService queryService;
25+
26+
@Test
27+
void queryOverMySQL() {
28+
String hql = "FROM TestPersistedEntity";
29+
List<?> results = queryService.query(hql, Map.of());
30+
assertNotNull(results);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.tus.common.domain.persistence.integration.config;
2+
3+
import org.hibernate.SessionFactory;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
7+
import org.springframework.orm.hibernate5.HibernateTransactionManager;
8+
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
9+
import org.springframework.transaction.PlatformTransactionManager;
10+
import org.springframework.transaction.annotation.EnableTransactionManagement;
11+
import org.testcontainers.containers.MySQLContainer;
12+
import org.tus.common.domain.persistence.PersistenceService;
13+
14+
import javax.sql.DataSource;
15+
import java.util.Properties;
16+
17+
/**
18+
* Test configuration for integration tests using MySQL via Testcontainers.
19+
* Use this when testing against MySQL-specific behaviour.
20+
*/
21+
@Configuration
22+
@EnableTransactionManagement
23+
public class PersistenceTestContainerMySQLConfig {
24+
25+
@Bean(initMethod = "start", destroyMethod = "stop")
26+
@SuppressWarnings("resource") // Spring manages container lifecycle via destroyMethod
27+
public MySQLContainer<?> mySQLContainer() {
28+
return new MySQLContainer<>("mysql:8.0")
29+
.withDatabaseName("testdb")
30+
.withUsername("test")
31+
.withPassword("test");
32+
}
33+
34+
@Bean
35+
public DataSource dataSource(MySQLContainer<?> container) {
36+
DriverManagerDataSource ds = new DriverManagerDataSource();
37+
ds.setDriverClassName(container.getDriverClassName());
38+
ds.setUrl(container.getJdbcUrl());
39+
ds.setUsername(container.getUsername());
40+
ds.setPassword(container.getPassword());
41+
return ds;
42+
}
43+
44+
@Bean
45+
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
46+
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
47+
sessionFactory.setDataSource(dataSource);
48+
sessionFactory.setPackagesToScan("org.tus.common.domain.persistence.integration.entity");
49+
Properties props = new Properties();
50+
props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
51+
props.put("hibernate.hbm2ddl.auto", "update");
52+
sessionFactory.setHibernateProperties(props);
53+
return sessionFactory;
54+
}
55+
56+
@Bean
57+
public PersistenceService persistenceService(SessionFactory sessionFactory) {
58+
return new PersistenceService(sessionFactory);
59+
}
60+
61+
@Bean
62+
public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
63+
return new HibernateTransactionManager(sessionFactory);
64+
}
65+
}

0 commit comments

Comments
 (0)