From: Kai Moritz Date: Fri, 10 Jul 2020 12:54:21 +0000 (+0200) Subject: Simple web-app example for spring-boot-data-jdbc X-Git-Tag: part-1~9 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=21ee193ca1a46c8f9b52768f4c18af7829a8c35a;p=demos%2Fspring%2Fdata-jdbc Simple web-app example for spring-boot-data-jdbc --- 21ee193ca1a46c8f9b52768f4c18af7829a8c35a diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..612c5bc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target +.idea +*.iml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..77328f4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.5.RELEASE + + + de.juplo.boot.data + jdbc + 0.0.1-SNAPSHOT + data-jdbc + Simple web-app example for spring-boot-data-jdbc + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-json + + + org.projectlombok + lombok + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/de/juplo/boot/data/jdbc/Application.java b/src/main/java/de/juplo/boot/data/jdbc/Application.java new file mode 100644 index 0000000..d7301e1 --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/Application.java @@ -0,0 +1,17 @@ +package de.juplo.boot.data.jdbc; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + private final static Logger LOG = LoggerFactory.getLogger(Application.class); + + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/main/java/de/juplo/boot/data/jdbc/User.java b/src/main/java/de/juplo/boot/data/jdbc/User.java new file mode 100644 index 0000000..16587c1 --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/User.java @@ -0,0 +1,28 @@ +package de.juplo.boot.data.jdbc; + +import lombok.*; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.core.mapping.Table; + +import java.time.LocalDateTime; + +@Table("users") +public class User { + @Id + Long id; + @Getter + @Setter + String username; + @Getter + @Setter + LocalDateTime created; + @Getter + @Setter + boolean loggedIn; + + public User(String username, LocalDateTime created, boolean loggedIn) { + this.username = username; + this.created = created; + this.loggedIn = loggedIn; + } +} diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserController.java b/src/main/java/de/juplo/boot/data/jdbc/UserController.java new file mode 100644 index 0000000..4eb1094 --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/UserController.java @@ -0,0 +1,78 @@ +package de.juplo.boot.data.jdbc; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.web.util.UriComponents; + +import java.time.LocalDateTime; + +@RestController +@Transactional +@RequestMapping("/users") +public class UserController { + + private static final Logger LOG = LoggerFactory.getLogger(UserController.class); + + + private final UserRepository repository; + + + public UserController(UserRepository repository) { + this.repository = repository; + } + + + @PostMapping + public ResponseEntity createUser( + ServletUriComponentsBuilder builder, + @RequestBody String username) { + String sanitizedUsername = UserController.sanitize(username); + User user = new User(sanitizedUsername, LocalDateTime.now(), false); + repository.save(user); + UriComponents uri = + builder + .fromCurrentRequest() + .pathSegment("{username}") + .buildAndExpand(sanitizedUsername); + return ResponseEntity.created(uri.toUri()).build(); + } + + @GetMapping("{username}") + public ResponseEntity getUser(@PathVariable String username) { + User user = repository.findByUsername(UserController.sanitize(username)); + + if (user == null) + return ResponseEntity.notFound().build(); + + return ResponseEntity.ok(user); + } + + @DeleteMapping("{username}") + public ResponseEntity removeUser(@PathVariable String username) { + User user = repository.findByUsername(UserController.sanitize(username)); + + if (user == null) + return ResponseEntity.notFound().build(); + + repository.delete(user); + + return ResponseEntity.ok(user); + } + + @GetMapping() + public ResponseEntity> getUsers() { + return ResponseEntity.ok(repository.findAll()); + } + + + private static String sanitize(String string) { + if (string == null) + return ""; + + return string.trim().toLowerCase(); + } +} diff --git a/src/main/java/de/juplo/boot/data/jdbc/UserRepository.java b/src/main/java/de/juplo/boot/data/jdbc/UserRepository.java new file mode 100644 index 0000000..3f03af1 --- /dev/null +++ b/src/main/java/de/juplo/boot/data/jdbc/UserRepository.java @@ -0,0 +1,10 @@ +package de.juplo.boot.data.jdbc; + +import org.springframework.data.jdbc.repository.query.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; + +public interface UserRepository extends CrudRepository { + @Query("select * from users u where u.username = :username") + User findByUsername(@Param("username") String username); +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..7ea719a --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,5 @@ +management: + endpoints: + web: + exposure: + include: "*" diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000..5e3590b --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1 @@ +CREATE TABLE users (id BIGINT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(255), created TIMESTAMP, logged_in BIT); diff --git a/src/test/java/de/juplo/boot/data/jdbc/ApplicationTests.java b/src/test/java/de/juplo/boot/data/jdbc/ApplicationTests.java new file mode 100644 index 0000000..c77d5b7 --- /dev/null +++ b/src/test/java/de/juplo/boot/data/jdbc/ApplicationTests.java @@ -0,0 +1,16 @@ +package de.juplo.boot.data.jdbc; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +}