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주차 과제]박해원 과제 제출합니다. #4

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
16 changes: 16 additions & 0 deletions sql/ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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)
);
drop table if exists member CASCADE;
create table member
(
id bigint generated by default as identity,
name varchar(255),
primary key (id)
);
41 changes: 41 additions & 0 deletions src/main/java/landvibe/springintro/Controller/HelloController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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","hello!");
return "hello";
}
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam(value = "name") 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;
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/landvibe/springintro/Controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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";
}
}
21 changes: 21 additions & 0 deletions src/main/java/landvibe/springintro/aop/TimeTraceAop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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(* hello.hellospring..*(..))")
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");
}
}
}
44 changes: 44 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,44 @@
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,40 @@
package landvibe.springintro.item.controller;

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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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;
}
}
42 changes: 42 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,42 @@
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,11 @@
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