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주차 과제 제출] 박지현 과제 제출 합니다. #3

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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-jdbc'
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
18 changes: 18 additions & 0 deletions sql/ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
drop table if exists member CASCADE;
create table member
(
id bigint generated by default as identity,
name varchar(255),
primary key (id)
);


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)
);
25 changes: 25 additions & 0 deletions src/main/java/landvibe/springintro/aop/TimeTraceAop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package landvibe.springintro.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.springframework.stereotype.Component;

@Aspect
@Component //스프링 빈에 등록
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");
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/landvibe/springintro/controller/HelloController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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;
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/landvibe/springintro/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package landvibe.springintro.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/") //localhost:8080으로 들어오면 아래 호출
public String home() {
return "home"; //home.html이 호출됨
}
}

/*
기존에 있던 index.html 말고 home.html이 보여지는 이유는
톰캣 서버에 요청이 오면 먼저 스프링 컨테이너에서 해당되는 컨트롤러가 있는지를 우선적으로 확인.
따라서, 컨트롤러가 있으므로 index.html은 호출되지 않고 종료.
*/
45 changes: 45 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,45 @@
package landvibe.springintro.item.config;

import landvibe.springintro.item.repository.ItemRepository;
import landvibe.springintro.item.repository.JpaItemRepository;
import landvibe.springintro.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import jakarta.persistence.EntityManager;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ItemConfig {
/*
private final DataSource dataSource;
private final EntityManager em;

@Autowired
public ItemConfig(DataSource dataSource, EntityManager em) {
this.dataSource = dataSource;
this.em = em;
}
*/
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);
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package landvibe.springintro.item.controller;

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

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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package landvibe.springintro.item.controller;

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

public String getName() {
return name;
}

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

public Integer getPrice() {
return price;
}

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

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}
}
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 Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Integer getPrice() {
return price;
}

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

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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