+spring furnature example
This commit is contained in:
parent
eb0af2c831
commit
6cb4847bc8
29
Java/spring/furniture/build.gradle
Normal file
29
Java/spring/furniture/build.gradle
Normal file
@ -0,0 +1,29 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.1.5'
|
||||
id 'io.spring.dependency-management' version '1.1.3'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
|
||||
java {
|
||||
sourceCompatibility = '17'
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-groovy-templates'
|
||||
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0'
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
useJUnitPlatform()
|
||||
}
|
17
Java/spring/furniture/dump.sql
Normal file
17
Java/spring/furniture/dump.sql
Normal file
@ -0,0 +1,17 @@
|
||||
-- H2 2.1.214;
|
||||
SET DB_CLOSE_DELAY -1;
|
||||
;
|
||||
CREATE USER IF NOT EXISTS "SA" SALT 'bb38af8fed566570' HASH '21b7a0b7822e49c228946e75eda19d36c360fca82e91726ea7d82a657a249d38' ADMIN;
|
||||
CREATE MEMORY TABLE "PUBLIC"."furniture_application$furniture"(
|
||||
"PRICE" INTEGER NOT NULL,
|
||||
"ID" BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1 RESTART WITH 2) NOT NULL,
|
||||
"NAME" CHARACTER VARYING(255)
|
||||
);
|
||||
ALTER TABLE "PUBLIC"."furniture_application$furniture" ADD CONSTRAINT "PUBLIC"."CONSTRAINT_C" PRIMARY KEY("ID");
|
||||
-- 1 +/- SELECT COUNT(*) FROM PUBLIC."furniture_application$furniture";
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" VALUES
|
||||
(500, 1, 'Sofa');
|
||||
CREATE MEMORY TABLE "PUBLIC"."NIGGER"(
|
||||
"I" INTEGER
|
||||
);
|
||||
-- 0 +/- SELECT COUNT(*) FROM PUBLIC.NIGGER;
|
1
Java/spring/furniture/settings.gradle
Normal file
1
Java/spring/furniture/settings.gradle
Normal file
@ -0,0 +1 @@
|
||||
rootProject.name = 'furniture'
|
@ -0,0 +1,84 @@
|
||||
package com.example.furniture;
|
||||
|
||||
import java.util.List;
|
||||
import jakarta.persistence.*;
|
||||
import org.springframework.boot.*;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
@SpringBootApplication
|
||||
public class FurnitureApplication {
|
||||
@Entity
|
||||
public static
|
||||
class Furniture {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public
|
||||
Long id;
|
||||
|
||||
public
|
||||
String name;
|
||||
|
||||
private
|
||||
int price;
|
||||
|
||||
Furniture(String name, int price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
Furniture() { ; }
|
||||
}
|
||||
|
||||
@RestController
|
||||
static
|
||||
class Controller {
|
||||
private final
|
||||
FurnitureRepository repository;
|
||||
|
||||
static private
|
||||
Logger logger = LoggerFactory.getLogger("connectionLogger");
|
||||
|
||||
Controller(FurnitureRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
ModelAndView index(){
|
||||
logger.info("connection");
|
||||
ModelAndView mav = new ModelAndView();
|
||||
mav.addObject("title", "Furniture list:");
|
||||
mav.addObject("furnitures", this.repository);
|
||||
mav.setViewName("inde");
|
||||
return mav;
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
ModelAndView indexPost(@RequestBody MultiValueMap<String, String> data) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
logger.info("post: " + objectMapper.writeValueAsString(data));
|
||||
repository.save(new Furniture(data.getFirst("name"), 0));
|
||||
} catch(Exception e) { ; }
|
||||
return index();
|
||||
}
|
||||
|
||||
//@GetMapping("/error")
|
||||
//ModelAndView error(){
|
||||
// ModelAndView mav = new ModelAndView();
|
||||
// mav.setViewName("error");
|
||||
// return mav;
|
||||
//}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(FurnitureApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.furniture;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface FurnitureRepository extends JpaRepository<FurnitureApplication.Furniture, Long> {}
|
@ -0,0 +1,3 @@
|
||||
spring.jpa.defer-datasource-initialization=true
|
||||
spring.sql.init.mode=always
|
||||
# server.error.path=/error
|
11
Java/spring/furniture/src/main/resources/data.sql
Normal file
11
Java/spring/furniture/src/main/resources/data.sql
Normal file
@ -0,0 +1,11 @@
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Sofa', 500);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Dining Table', 350);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Bed', 700);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Wardrobe', 300);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Coffee Table', 150);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Bookshelf', 200);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Office Chair', 100);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Recliner', 450);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('Dresser', 280);
|
||||
INSERT INTO "PUBLIC"."furniture_application$furniture" (name, price) VALUES ('End Table', 80);
|
||||
-- SCRIPT TO 'dump.sql';
|
3
Java/spring/furniture/src/main/resources/templates/e
Normal file
3
Java/spring/furniture/src/main/resources/templates/e
Normal file
@ -0,0 +1,3 @@
|
||||
div (style: 'color:red') {
|
||||
h1 { yield 'ERROR' }
|
||||
}
|
23
Java/spring/furniture/src/main/resources/templates/index.tpl
Normal file
23
Java/spring/furniture/src/main/resources/templates/index.tpl
Normal file
@ -0,0 +1,23 @@
|
||||
yieldUnescaped '<!DOCTYPE html>'
|
||||
html(lang:'en') {
|
||||
body {
|
||||
div {
|
||||
h1 { yield(title) }
|
||||
}
|
||||
ul {
|
||||
furnitures.findAll().each { furniture ->
|
||||
li {
|
||||
yield "${furniture.name}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
div {
|
||||
h4 { yield 'Add new' }
|
||||
form (method: 'post', enctype: 'application/json') {
|
||||
input (name: "name", placeholder: "name") { } br {}
|
||||
button (type: 'submit') { yield 'Submit' }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.furniture;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class FurnitureApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user