Skip to content

Commit 8ded7ea

Browse files
authored
Merge pull request #317 from domaframework/refactor/examples
Refactor example code
2 parents 4cc40db + a2e385f commit 8ded7ea

File tree

171 files changed

+1775
-2149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+1775
-2149
lines changed

README.md

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,116 @@ Simple examples for [Doma](https://github.com/domaframework/doma).
55

66
This repository includes the following examples:
77

8-
* [dao-style-basic](dao-style-basic) - Shows basic DAO style.
9-
* [dao-style-file](dao-style-file) - Uses files to store SQL templates.
10-
* [dao-style-text](dao-style-text) - Uses Text Blocks to store SQL templates.
11-
* [dsl-style-java](dsl-style-java) - Uses the Criteria API.
12-
* [jpms-java](jpms-java) - Uses the Java Platform Module System (JPMS).
8+
* [example-sql-file](example-sql-file) - Uses SQL files to store SQL templates.
9+
* [example-sql-annotation](example-sql-annotation) - Uses SQL annotations to store SQL templates.
10+
* [example-criteria](example-criteria) - Uses the Criteria API.
11+
* [example-jpms](example-jpms) - Uses the Java Platform Module System (JPMS).
12+
13+
ER diagram
14+
---------------------
15+
16+
The ER diagram for the database used in the example projects is shown below.
17+
18+
```mermaid
19+
erDiagram
20+
DEPARTMENT {
21+
INTEGER id PK "not null"
22+
VARCHAR name "not null"
23+
INTEGER version "not null"
24+
}
25+
EMPLOYEE {
26+
INTEGER id PK "not null"
27+
VARCHAR name "not null"
28+
INTEGER age "not null"
29+
INTEGER salary
30+
VARCHAR job_type
31+
TIMESTAMP hiredate
32+
INTEGER department_id
33+
INTEGER version "not null"
34+
TIMESTAMP inserttimestamp
35+
TIMESTAMP updatetimestamp
36+
}
37+
USER {
38+
INT id PK "auto_increment"
39+
VARCHAR name "not null"
40+
VARCHAR email "unique not null"
41+
TIMESTAMP created_at "default current_timestamp"
42+
INT version "default 0 not null"
43+
}
44+
ROLE {
45+
INT id PK "auto_increment"
46+
VARCHAR name "unique not null"
47+
INT version "default 0 not null"
48+
}
49+
USER_ROLE {
50+
INT id PK "auto_increment"
51+
INT user_id "not null"
52+
INT role_id "not null"
53+
INT version "default 0 not null"
54+
}
55+
PRODUCT {
56+
INT id PK "auto_increment"
57+
VARCHAR name "not null"
58+
DECIMAL price "not null"
59+
INT stock_quantity "not null"
60+
TIMESTAMP created_at "default current_timestamp"
61+
INT version "default 0 not null"
62+
}
63+
CATEGORY {
64+
INT id PK "auto_increment"
65+
VARCHAR name "unique not null"
66+
INT version "default 0 not null"
67+
}
68+
PRODUCT_CATEGORY {
69+
INT id PK "auto_increment"
70+
INT product_id "not null"
71+
INT category_id "not null"
72+
INT version "default 0 not null"
73+
}
74+
ORDER {
75+
INT id PK "auto_increment"
76+
INT user_id "not null"
77+
TIMESTAMP order_date "default current_timestamp"
78+
VARCHAR status "not null"
79+
INT version "default 0 not null"
80+
}
81+
ORDER_ITEM {
82+
INT id PK "auto_increment"
83+
INT order_id "not null"
84+
INT product_id "not null"
85+
INT quantity "not null"
86+
DECIMAL price "not null"
87+
INT version "default 0 not null"
88+
}
89+
PAYMENT {
90+
INT id PK "auto_increment"
91+
INT order_id "unique not null"
92+
DECIMAL amount "not null"
93+
TIMESTAMP payment_date "default current_timestamp"
94+
INT version "default 0 not null"
95+
}
96+
REVIEW {
97+
INT id PK "auto_increment"
98+
INT user_id "not null"
99+
INT product_id "not null"
100+
INT rating "check: 1-5"
101+
TEXT comment
102+
TIMESTAMP created_at "default current_timestamp"
103+
INT version "default 0 not null"
104+
}
105+
106+
EMPLOYEE }|..|| DEPARTMENT : belongs_to
107+
USER_ROLE }|..|| USER : "user_id"
108+
USER_ROLE }|..|| ROLE : "role_id"
109+
PRODUCT_CATEGORY }|..|| PRODUCT : "product_id"
110+
PRODUCT_CATEGORY }|..|| CATEGORY : "category_id"
111+
ORDER }|..|| USER : "user_id"
112+
ORDER_ITEM }|..|| ORDER : "order_id"
113+
ORDER_ITEM }|..|| PRODUCT : "product_id"
114+
PAYMENT ||--|| ORDER : "order_id"
115+
REVIEW }|..|| USER : "user_id"
116+
REVIEW }|..|| PRODUCT : "product_id"
117+
```
13118

14119
Clone this repository
15120
---------------------
File renamed without changes.

common-test/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
dependencies {
6+
implementation(project(":common"))
7+
implementation(libs.junit.jupiter.api)
8+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package example.dao_style_basic.dao;
1+
package example.common.test;
22

3-
import example.dao_style_basic.DbConfig;
3+
import example.common.DbConfig;
4+
import example.common.dao.ScriptDao;
5+
import example.common.dao.ScriptDaoImpl;
46
import org.junit.jupiter.api.extension.AfterAllCallback;
57
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
68
import org.junit.jupiter.api.extension.BeforeAllCallback;
File renamed without changes.

dao-style-text/build.gradle.kts renamed to common/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tasks {
77
val aptOptions = extensions.getByType<com.diffplug.gradle.eclipse.apt.AptPlugin.AptOptions>()
88
aptOptions.processorArgs =
99
mapOf(
10-
"doma.domain.converters" to "example.dao_style_text.domain.DomainConverterProvider",
10+
"doma.domain.converters" to "example.common.domain.DomainConverterProvider",
1111
)
1212
}
1313
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package __.example.common.domain;
2+
3+
/** */
4+
@javax.annotation.processing.Generated(value = { "Doma", "3.4.0" }, date = "2025-02-15T08:06:59.349+0900")
5+
@org.seasar.doma.DomainTypeImplementation
6+
public final class _Age extends org.seasar.doma.jdbc.domain.AbstractDomainType<java.lang.Integer, example.common.domain.Age> {
7+
8+
static {
9+
org.seasar.doma.internal.Artifact.validateVersion("3.4.0");
10+
}
11+
12+
private static final example.common.domain.AgeConverter converter = new example.common.domain.AgeConverter();
13+
14+
private static final _Age singleton = new _Age();
15+
16+
private _Age() {
17+
super(org.seasar.doma.internal.wrapper.WrapperSuppliers.ofInteger());
18+
}
19+
20+
@Override
21+
protected example.common.domain.Age newDomain(java.lang.Integer value) {
22+
return converter.fromValueToDomain(value);
23+
}
24+
25+
@Override
26+
protected java.lang.Integer getBasicValue(example.common.domain.Age domain) {
27+
if (domain == null) {
28+
return null;
29+
}
30+
return converter.fromDomainToValue(domain);
31+
}
32+
33+
@Override
34+
public Class<?> getBasicClass() {
35+
return java.lang.Integer.class;
36+
}
37+
38+
@Override
39+
public Class<example.common.domain.Age> getDomainClass() {
40+
return example.common.domain.Age.class;
41+
}
42+
43+
/**
44+
* @return the singleton
45+
*/
46+
public static _Age getSingletonInternal() {
47+
return singleton;
48+
}
49+
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package example.common.domain;
2+
3+
/** */
4+
@javax.annotation.processing.Generated(value = { "Doma", "3.4.0" }, date = "2025-02-15T08:39:41.857+0900")
5+
@org.seasar.doma.DomainTypeImplementation
6+
public final class _Salary extends org.seasar.doma.jdbc.domain.AbstractDomainType<java.lang.Integer, example.common.domain.Salary> {
7+
8+
static {
9+
org.seasar.doma.internal.Artifact.validateVersion("3.4.0");
10+
}
11+
12+
private static final _Salary singleton = new _Salary();
13+
14+
private _Salary() {
15+
super(org.seasar.doma.internal.wrapper.WrapperSuppliers.ofInteger());
16+
}
17+
18+
@Override
19+
protected example.common.domain.Salary newDomain(java.lang.Integer value) {
20+
if (value == null) {
21+
return null;
22+
}
23+
return new example.common.domain.Salary(value);
24+
}
25+
26+
@Override
27+
protected java.lang.Integer getBasicValue(example.common.domain.Salary domain) {
28+
if (domain == null) {
29+
return null;
30+
}
31+
return domain.value();
32+
}
33+
34+
@Override
35+
public Class<?> getBasicClass() {
36+
return java.lang.Integer.class;
37+
}
38+
39+
@Override
40+
public Class<example.common.domain.Salary> getDomainClass() {
41+
return example.common.domain.Salary.class;
42+
}
43+
44+
/**
45+
* @return the singleton
46+
*/
47+
public static _Salary getSingletonInternal() {
48+
return singleton;
49+
}
50+
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package example.common.entity;
2+
3+
/** */
4+
@javax.annotation.processing.Generated(value = { "Doma", "3.4.0" }, date = "2025-02-15T08:44:03.573+0900")
5+
public final class Department_ implements org.seasar.doma.jdbc.criteria.metamodel.EntityMetamodel<example.common.entity.Department> {
6+
7+
static {
8+
org.seasar.doma.internal.Artifact.validateVersion("3.4.0");
9+
}
10+
11+
private final String __qualifiedTableName;
12+
13+
private final example.common.entity._Department __entityType = example.common.entity._Department.getSingletonInternal();
14+
15+
private final java.util.List<org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<?>> __allPropertyMetamodels;
16+
17+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.Integer> id = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.Integer>(java.lang.Integer.class, __entityType, "id");
18+
19+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.String> name = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.String>(java.lang.String.class, __entityType, "name");
20+
21+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.Integer> version = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.Integer>(java.lang.Integer.class, __entityType, "version");
22+
23+
public Department_() {
24+
this("");
25+
}
26+
27+
public Department_(String qualifiedTableName) {
28+
this.__qualifiedTableName = java.util.Objects.requireNonNull(qualifiedTableName);
29+
java.util.ArrayList<org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<?>> __list = new java.util.ArrayList<>(3);
30+
__list.add(id);
31+
__list.add(name);
32+
__list.add(version);
33+
__allPropertyMetamodels = java.util.Collections.unmodifiableList(__list);
34+
}
35+
36+
@Override
37+
public org.seasar.doma.jdbc.entity.EntityType<example.common.entity.Department> asType() {
38+
return __qualifiedTableName.isEmpty() ? __entityType : new org.seasar.doma.jdbc.criteria.metamodel.EntityTypeProxy<>(__entityType, __qualifiedTableName);
39+
}
40+
41+
@Override
42+
public java.util.List<org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<?>> allPropertyMetamodels() {
43+
return __allPropertyMetamodels;
44+
}
45+
46+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package example.common.entity;
2+
3+
/** */
4+
@javax.annotation.processing.Generated(value = { "Doma", "3.4.0" }, date = "2025-02-15T08:44:03.603+0900")
5+
public final class Employee_ implements org.seasar.doma.jdbc.criteria.metamodel.EntityMetamodel<example.common.entity.Employee> {
6+
7+
static {
8+
org.seasar.doma.internal.Artifact.validateVersion("3.4.0");
9+
}
10+
11+
private final String __qualifiedTableName;
12+
13+
private final example.common.entity._Employee __entityType = example.common.entity._Employee.getSingletonInternal();
14+
15+
private final java.util.List<org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<?>> __allPropertyMetamodels;
16+
17+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.Integer> id = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.Integer>(java.lang.Integer.class, __entityType, "id");
18+
19+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.String> name = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.String>(java.lang.String.class, __entityType, "name");
20+
21+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<example.common.domain.Age> age = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<example.common.domain.Age>(example.common.domain.Age.class, __entityType, "age");
22+
23+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<example.common.domain.Salary> salary = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<example.common.domain.Salary>(example.common.domain.Salary.class, __entityType, "salary");
24+
25+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<example.common.entity.JobType> jobType = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<example.common.entity.JobType>(example.common.entity.JobType.class, __entityType, "jobType");
26+
27+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.time.LocalDate> hiredate = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.time.LocalDate>(java.time.LocalDate.class, __entityType, "hiredate");
28+
29+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.Integer> departmentId = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.Integer>(java.lang.Integer.class, __entityType, "departmentId");
30+
31+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.lang.Integer> version = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.lang.Integer>(java.lang.Integer.class, __entityType, "version");
32+
33+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.time.LocalDateTime> insertTimestamp = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.time.LocalDateTime>(java.time.LocalDateTime.class, __entityType, "insertTimestamp");
34+
35+
public final org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<java.time.LocalDateTime> updateTimestamp = new org.seasar.doma.jdbc.criteria.metamodel.DefaultPropertyMetamodel<java.time.LocalDateTime>(java.time.LocalDateTime.class, __entityType, "updateTimestamp");
36+
37+
public Employee_() {
38+
this("");
39+
}
40+
41+
public Employee_(String qualifiedTableName) {
42+
this.__qualifiedTableName = java.util.Objects.requireNonNull(qualifiedTableName);
43+
java.util.ArrayList<org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<?>> __list = new java.util.ArrayList<>(10);
44+
__list.add(id);
45+
__list.add(name);
46+
__list.add(age);
47+
__list.add(salary);
48+
__list.add(jobType);
49+
__list.add(hiredate);
50+
__list.add(departmentId);
51+
__list.add(version);
52+
__list.add(insertTimestamp);
53+
__list.add(updateTimestamp);
54+
__allPropertyMetamodels = java.util.Collections.unmodifiableList(__list);
55+
}
56+
57+
@Override
58+
public org.seasar.doma.jdbc.entity.EntityType<example.common.entity.Employee> asType() {
59+
return __qualifiedTableName.isEmpty() ? __entityType : new org.seasar.doma.jdbc.criteria.metamodel.EntityTypeProxy<>(__entityType, __qualifiedTableName);
60+
}
61+
62+
@Override
63+
public java.util.List<org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel<?>> allPropertyMetamodels() {
64+
return __allPropertyMetamodels;
65+
}
66+
67+
}

0 commit comments

Comments
 (0)