-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.hzz</groupId> | ||
<artifactId>springboot-ddd</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<spring.boot.version>2.7.12</spring.boot.version> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<!--SpringBoot的版本管理--> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jdbc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.vavr</groupId> | ||
<artifactId>vavr</artifactId> | ||
<version>0.10.4</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.hzz; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
ddd/springboot-ddd/src/main/java/org/hzz/interfaces/rest/PaymentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.hzz.interfaces.rest; | ||
|
||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.security.auth.callback.Callback; | ||
import java.util.concurrent.Callable; | ||
|
||
@RestController | ||
@RequestMapping("/payment") | ||
public class PaymentController { | ||
|
||
@GetMapping(path = "hello",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public Callable<String> getPayment() { | ||
return ()->{ | ||
System.out.println("hello"); | ||
return "hello"; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.hzz</groupId> | ||
<artifactId>spring-data-jdbc-with-h2</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<!--SpringBoot的版本管理--> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>2.7.12</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jdbc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.18</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
24 changes: 24 additions & 0 deletions
24
springboot/spring-data-jdbc-with-h2/src/main/java/org/hzz/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.hzz; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hzz.student.StudentJdbcRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
@Slf4j | ||
public class Application implements CommandLineRunner { | ||
@Autowired | ||
private StudentJdbcRepository studentJdbcRepository; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
log.info("Student id 10001 -> {}", studentJdbcRepository.findById(10001L)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
springboot/spring-data-jdbc-with-h2/src/main/java/org/hzz/project/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.hzz.project; | ||
|
||
import lombok.*; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Table("CLIENT") | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Client { | ||
@Id | ||
private int id; | ||
private String name; | ||
private Set<Project> projects = new HashSet<>(); | ||
|
||
public void addProject(Project project){ | ||
projects.add(project); | ||
} | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
springboot/spring-data-jdbc-with-h2/src/main/java/org/hzz/project/ClientRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.hzz.project; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ClientRepository extends CrudRepository<Client, Integer> { | ||
} |
17 changes: 17 additions & 0 deletions
17
springboot/spring-data-jdbc-with-h2/src/main/java/org/hzz/project/Project.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hzz.project; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
@Table("PROJECT") | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Project { | ||
@Id | ||
private int id; | ||
private String name; | ||
} |
14 changes: 14 additions & 0 deletions
14
springboot/spring-data-jdbc-with-h2/src/main/java/org/hzz/student/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.hzz.student; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Student { | ||
private Long id; | ||
private String name; | ||
private String passportNumber; | ||
} |
20 changes: 20 additions & 0 deletions
20
springboot/spring-data-jdbc-with-h2/src/main/java/org/hzz/student/StudentJdbcRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.hzz.student; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class StudentJdbcRepository { | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public StudentJdbcRepository(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public Student findById(Long id) { | ||
return jdbcTemplate.queryForObject("select * from student where id=?", | ||
new BeanPropertyRowMapper<>(Student.class), id); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
springboot/spring-data-jdbc-with-h2/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Enabling H2 Console | ||
spring.h2.console.enabled=true | ||
|
||
# Show all queries | ||
logging.level.org.springframework.jdbc.core = trace | ||
|
||
spring.datasource.url=jdbc:h2:mem:testdb | ||
spring.datasource.username=sa | ||
spring.datasource.password=sa |
2 changes: 2 additions & 0 deletions
2
springboot/spring-data-jdbc-with-h2/src/main/resources/data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
insert into student values(10001,'hzz', 'E1234567'); | ||
insert into student values(10002,'Q10Viking', 'A1234568'); |
22 changes: 22 additions & 0 deletions
22
springboot/spring-data-jdbc-with-h2/src/main/resources/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
create table student | ||
( | ||
id integer not null, | ||
name varchar(255) not null, | ||
passport_number varchar(255) not null, | ||
primary key (id) | ||
); | ||
|
||
create TABLE client | ||
( | ||
id INT auto_increment primary key, | ||
name VARCHAR(200) | ||
); | ||
|
||
create TABLE project | ||
( | ||
id INT auto_increment primary key, | ||
name VARCHAR(200), | ||
client INTEGER | ||
); | ||
ALTER TABLE project | ||
ADD FOREIGN KEY (client) REFERENCES client (id); |
26 changes: 26 additions & 0 deletions
26
springboot/spring-data-jdbc-with-h2/src/test/java/org/hzz/ClientRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.hzz; | ||
|
||
import org.hzz.project.Client; | ||
import org.hzz.project.ClientRepository; | ||
import org.hzz.project.Project; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
public class ClientRepositoryTest { | ||
|
||
@Autowired | ||
private ClientRepository clientRepository; | ||
|
||
@Test | ||
public void testAddClient() throws InterruptedException { | ||
Client client = new Client(); | ||
client.setName("DDD-learning"); | ||
client.addProject(new Project(1, "Java")); | ||
client.addProject(new Project(2, "GoLang")); | ||
clientRepository.save(client); | ||
clientRepository.findAll().forEach(System.out::println); | ||
// Client(id=1, name=DDD-learning, projects=[Project(id=1, name=Java), Project(id=2, name=GoLang)]) | ||
} | ||
} |