Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3주차 과제] 이태균 과제 제출합니다. #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
9 changes: 9 additions & 0 deletions sql/ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
drop table if exists item CASCADE;
create table item
(
id bigint generated by default as identity,
name varchar(255),
price integer not null,
count integer not null,
primary key (id)
);
22 changes: 22 additions & 0 deletions src/main/java/landvibe/springintro/aop/TimeTraceAop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package landvibe.springintro.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TimeTraceAop {
@Around("execution(* landvibe.springintro..*(..))")
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("START : " + joinPoint.toString());
try {
return joinPoint.proceed(); // 다음 메소드가 진행
} finally {
long finish = System.currentTimeMillis();
long timeMs = finish - start;
System.out.println("END : " + joinPoint.toString() + " " + timeMs +
"ms"); // 어떤 메소드를 call 했는지
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/landvibe/springintro/controller/HelloController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package landvibe.springintro.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model){
model.addAttribute("data","LandVibe");
return "hello";
}

@GetMapping("hello-mvc")
public String helloMvc(
@RequestParam(value="name", required=false, defaultValue=
"landVibe") String name,
Model model){
model.addAttribute("name",name);
return "hello-template";
}

@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name){
return "hello " + name;
}

@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name){
Hello hello = new Hello();
hello.setName(name);
return hello;
}

static class Hello{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/landvibe/springintro/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package landvibe.springintro.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
@GetMapping("/")
public String home(){
return "home";
}
}
47 changes: 47 additions & 0 deletions src/main/java/landvibe/springintro/controller/ItemController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package landvibe.springintro.controller;

import landvibe.springintro.item.service.ItemService;
import landvibe.springintro.item.domain.Item;
import landvibe.springintro.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class ItemController {
private final ItemService itemService;

@Autowired
public ItemController(ItemService itemService) {
this.itemService = itemService;
}

@GetMapping("/items/new")
public String createForm(){
return "items/createForm";
}

@PostMapping("/items/new")
public String create(@ModelAttribute ItemCreateForm form){
Item item = new Item();
item.setName(form.getName());
item.setPrice(form.getPrice());
item.setCount(form.getCount());
itemService.create(item);

return "redirect:/";
}

@GetMapping("/items")
public String list(Model model){
List<Item> items = itemService.findItems();
model.addAttribute("items", items);
return "items/itemList";
}

}
31 changes: 31 additions & 0 deletions src/main/java/landvibe/springintro/controller/ItemCreateForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package landvibe.springintro.controller;

public class ItemCreateForm {
private String name;
private Integer price;
private Integer count;

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

public Integer getPrice() {
return price;
}

public void setPrice(Integer price) {
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
42 changes: 42 additions & 0 deletions src/main/java/landvibe/springintro/item/config/ItemConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package landvibe.springintro.item.config;

import jakarta.persistence.EntityManager;
import landvibe.springintro.item.repository.*;
import landvibe.springintro.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class ItemConfig {
private final DataSource dataSource;
//
// @Autowired
// public ItemConfig(DataSource dataSource, EntityManager em) {
// this.dataSource = dataSource;
// this.em = em;
// }

public ItemConfig(DataSource dataSource) {
this.dataSource = dataSource;
}

/* private final ItemRepository itemRepository;
public ItemConfig(ItemRepository itemRepository){
this.itemRepository = itemRepository;
}*/
@Bean
public ItemService itemService(){
return new ItemService(itemRepository());
}

@Bean
public ItemRepository itemRepository(){
//return new MemoryItemRepository();
//return new JdbcItemRepository(dataSource);
return new JdbcTemplateItemRepository(dataSource);
//return new JpaItemRepository(em);
}
}
48 changes: 48 additions & 0 deletions src/main/java/landvibe/springintro/item/domain/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package landvibe.springintro.item.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer price;
private Integer count;

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

public Integer getPrice() {
return price;
}

public void setPrice(Integer price) {
this.price = price;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package landvibe.springintro.item.repository;

import landvibe.springintro.item.domain.Item;

import java.util.List;
import java.util.Optional;

public interface ItemRepository {
Item save(Item item);
Optional<Item> findById(Long id);
Optional<Item> findByName(String name);
List<Item> findAll();
}
Loading