Skip to content

Commit

Permalink
inished all
Browse files Browse the repository at this point in the history
  • Loading branch information
upangka committed May 27, 2023
1 parent df666b2 commit 96dc435
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ddd/springboot-ddd/pom.xml
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>
11 changes: 11 additions & 0 deletions ddd/springboot-ddd/src/main/java/org/hzz/Application.java
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);
}
}
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";
};
}
}
54 changes: 54 additions & 0 deletions springboot/spring-data-jdbc-with-h2/pom.xml
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>
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));
}
}
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);
}


}
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> {
}
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;
}
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;
}
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);
}
}
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
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 springboot/spring-data-jdbc-with-h2/src/main/resources/schema.sql
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);
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)])
}
}

0 comments on commit 96dc435

Please sign in to comment.