Skip to content

Commit 6d4682f

Browse files
lor6Eugen
authored andcommitted
boot example app (eugenp#1808)
* boot example app * move project and rename packages * update constructor injection
1 parent 88e27db commit 6d4682f

12 files changed

Lines changed: 251 additions & 0 deletions

File tree

guest/spring-boot-app/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target/
2+
.settings/
3+
.classpath
4+
.project
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+

guest/spring-boot-app/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>spring-boot-app</groupId>
5+
<artifactId>spring-boot-app</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>1.5.3.RELEASE</version>
13+
<relativePath />
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-jpa</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.h2database</groupId>
27+
<artifactId>h2</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-actuator</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<sourceDirectory>src</sourceDirectory>
41+
<plugins>
42+
<plugin>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<version>3.5.1</version>
45+
<configuration>
46+
<source>1.8</source>
47+
<target>1.8</target>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<artifactId>maven-war-plugin</artifactId>
52+
<version>3.0.0</version>
53+
<configuration>
54+
<warSourceDirectory>WebContent</warSourceDirectory>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
<properties>
61+
<tomcat.version>8.0.43</tomcat.version>
62+
</properties>
63+
64+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.stackify;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args){
9+
SpringApplication.run(Application.class, args);
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.stackify.config;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
8+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
9+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
10+
11+
@Configuration
12+
public class PersistenceConfig {
13+
14+
@Bean
15+
public DataSource dataSource() {
16+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
17+
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2)
18+
.addScript("mySchema.sql")
19+
.addScript("myData.sql")
20+
.build();
21+
return db;
22+
}
23+
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.stackify.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.ResponseStatus;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.http.HttpStatus;
11+
12+
import com.stackify.model.Employee;
13+
import com.stackify.repository.EmployeeRepository;
14+
15+
@RestController
16+
public class EmployeeController {
17+
18+
private EmployeeRepository employeeRepository;
19+
20+
public EmployeeController(EmployeeRepository employeeRepository){
21+
this.employeeRepository = employeeRepository;
22+
}
23+
24+
@PostMapping("/employees")
25+
@ResponseStatus(HttpStatus.CREATED)
26+
public void addEmployee(@RequestBody Employee employee) {
27+
employeeRepository.save(employee);
28+
}
29+
30+
@GetMapping("/employees")
31+
public List<Employee> getEmployees() {
32+
return employeeRepository.findAll();
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.stackify.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Employee {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.IDENTITY)
12+
private long id;
13+
14+
private String name;
15+
16+
public Employee() {
17+
}
18+
19+
public Employee(long id, String name) {
20+
super();
21+
this.id = id;
22+
this.name = name;
23+
}
24+
25+
public long getId() {
26+
return id;
27+
}
28+
29+
public void setId(long id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.stackify.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import com.stackify.model.Employee;
6+
7+
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
8+
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
server.port=8081
2+
server.contextPath=/springbootapp
3+
logging.level.org.springframework.web: DEBUG
4+
5+
spring.jpa.hibernate.ddl-auto=update
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
insert into employee(name) values ('ana');

0 commit comments

Comments
 (0)