Intro.Annotation

๐Ÿ“Œ Annotation ์–ด๋…ธํ…Œ์ด์…˜ : class์™€ method์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๋ถ€์—ฌํ•จ

1๏ธโƒฃ Annotation ํ™œ์šฉ

  • ๊ฐ class์˜ ์—ญํ• ์„ ์ •

  • Bean ์ž๋™์ฃผ์ž…

  • ์ž๋™์œผ๋กœ Getter, Setter ์ƒ์„ฑ

2๏ธโƒฃ Annotation ์žฅ์ 

  • ์ฝ”๋“œ๋Ÿ‰ ๊ฐ์†Œ

  • ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด

  • ์ƒ์‚ฐ์„ฑ ์ฆ๊ฐ€


๋Œ€ํ‘œ์ ์ธ Annotation๊ณผ ์—ญํ• 

@Component

๊ฐœ๋ฐœ์ž๊ฐ€ ์ƒ์„ฑํ•œ class โ†’ Spring Bean์œผ๋กœ ๋“ฑ๋กํ•  ๋•Œ ์‚ฌ์šฉ

  • ์„ค์ •์ •๋ณด ์—†์ด ์ž๋™์œผ๋กœ ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๋ชจ๋“  ํด๋ž˜์Šค โ†’ Spring Bean ๋“ฑ๋ก

  • Spring์€ โ†’ @Component ์–ด๋…ธํ…Œ์ด์…˜ ๋ณด๊ณ  โ†’ Spring์˜ Bean์œผ๋กœ ๋“ฑ๋กํ•จ

@Component(value="mallang")
public class Family {
    public Mallang() {
        System.out.println("hi");
    }
}

@ComponentScan

@Component ์Šค์บ” ์‹œ, @ComponentScan์ด ๋ถ™์€ ํด๋ž˜์Šค์˜ ํŒจํ‚ค์ง€๊ฐ€ ์‹œ์ž‘์œ„์น˜

  • @ComponentScan์ด ๋ถ™์€ class โ†’ ํ”„๋กœ์ ํŠธ ์ตœ์ƒ๋‹จ์— ์œ„์น˜ํ•˜๊ธฐ

@Controller

Spring์—๊ฒŒ ํ•ด๋‹น class๊ฐ€ Controller ์—ญํ• ์„ ํ•œ๋‹ค๊ณ  ๋ช…์‹œํ•จ

  • ํ•ด๋‹น class์—์„œ URL Mapping ๋œ method โ†’ return ๊ฐ’์œผ๋กœ viewName(String) ๋ฐ˜ํ™˜ํ•จ
@Controller // ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
@RequestMapping("/user") // ํ•ด๋‹น URL๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•จ
public class UserController {
    @RequestMapping(method = RequestMethod.GET)
    public String getUser(Model model) {
        //  GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
}

@RequestMapping

Requestํ•œ URL๊ณผ ์–ด๋…ธํ…Œ์ด์…˜ value ๊ฐ’ ์ผ์น˜ โ†’ ํ•ด๋‹น class ๋‚ด method ์‹คํ–‰

  • @RequestMapping(value = "")

  • Controller ๊ฐ์ฒด ์•ˆ์˜ method ๋ฐ class์— ์ ์šฉ๊ฐ€๋Šฅ

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(method = RequestMethod.GET)
    public String getUser(Model model) {
        //  GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String addUser(Model model) {
        //  POST method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
    
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String addUser(Model model) {
        //  GET method, /user/info ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
}

@RequestHeader

Request์˜ Header ๊ฐ’์„ ๊ฐ€์ ธ์˜ด

  • Request Header : ์ถ”๊ฐ€๋ฐ์ดํ„ฐ, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ (์˜์‚ฌํ‘œํ˜„์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ)

  • ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ์“ด, ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์‚ฌ์šฉ

  • @RequestHeader(value = "")

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(method = RequestMethod.GET)
    public String getUser(@RequestHeader(value="Accept-Language") String acceptLanguage) {
        //  GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
}

@PathVariable

URL์„ ํ†ตํ•ด ์ „๋‹ฌ๋œ ๊ฐ’ โ†’ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์˜ด

  • RESTful ๋ฐฉ์‹์œผ๋กœ REST ํ†ต์‹ ์— ์‚ฌ์šฉ

  • @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜ ๊ฐ’์œผ๋กœ {ํ…œํ”Œ๋ฆฟ๋ณ€์ˆ˜} ์‚ฌ์šฉ

  • @PathVariable ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ โ†’ {ํ…œํ”Œ๋ฆฟ๋ณ€์ˆ˜}์™€ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ–๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ถ”๊ฐ€

๐Ÿ“ localhost:8080/userId๊ฐ’/memo๊ฐ’

@RestController
public class TestController() {
	@RequestMapping("/{userId}/{memo})
	public String test(@PathVariable("userId") String userId, @PathVariable("memo") String memo)
	// ๋ณ€์ˆ˜ userId, memo์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’(=URL ์ „๋‹ฌ๊ฐ’)์ด ํ• ๋‹น๋จ
	return "Test ์™„๋ฃŒ"
	}
}

@RequestParam

URL์— ์ „๋‹ฌ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ โ†’ ๋ฉ”์„œ๋“œ ์ธ์ž์™€ ๋งค์นญํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜

  • ์ฟผ๋ฆฌ์ŠคํŠธ๋ง, GET๋ฐฉ์‹

  • JSON ํ˜•ํƒœ์˜ Body โ†’ Message Converter๋ฅผ ํ†ตํ•ด Java ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜์‹œํ‚ด

  • localhost:8080/param?userId=์ž…๋ ฅ๊ฐ’&memo=์ž…๋ ฅ๊ฐ’

  • ํŠน์ˆ˜๋ฌธ์ž ๋ฐ ๊ตฌ๋ถ„์  ํฌํ•จ ๊ฐ’์ด ์ž˜๋ฆฌ๊ฑฐ๋‚˜ ๋น„์ •์ƒ์ ์œผ๋กœ ๋“ค์–ด์˜ด

  • GET๋ฐฉ์‹ Request โ†’ URL์— ๊ฐ’ ํ‘œ์ถœ๋จ

  • POST๋ฐฉ์‹ Request โ†’ URL์— ๊ฐ’ ํ‘œ์ถœ โŒ

๐Ÿ“ https://naver.com?nickname=dog&old=10

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(method = RequestMethod.GET) // GET ๋ฐฉ์‹ ์„ ์–ธ
    public String getUser(@RequestParam String nickname, @RequestParam(name="old") String age {
        // GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
        String sub = nickname + "_" + age;
        ...
    }
}

@RequestBody

Body์— ์ „๋‹ฌ๋˜๋Š” ๋ฐ์ดํ„ฐ โ†’ ๋ฉ”์„œ๋“œ ์ธ์ž์™€ ๋งค์นญํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜

  • Client๊ฐ€ ๋ณด๋‚ด๋Š” HTTP Request ๋ณธ๋ฌธ (JSON ๋“ฑ) โ†’ Java ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜

  • โ‘  Client ์ž…๋ ฅ๊ฐ’ โ†’ HTTP Body์— JSON ํ˜•ํƒœ๋กœ ๋„˜์–ด๊ฐ

  • โ‘ก ํŒŒ๋ผ๋ฏธํ„ฐ์— @RequestBody + ๊ฐ’์„ ๋ฐ›์•„์˜ฌ ๊ฐ์ฒด ์ง€์ •ํ•ด์คŒ

  • ๊ฐ์ฒด ํ•„๋“œ๊ฐ’ - ํŒŒ๋ผ๋ฏธํ„ฐ ํ•„๋“œ๊ฐ’ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•ด์•ผํ•จ


@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(method = RequestMethod.POST)
    public String addUser(@RequestBody User user) {
        //  POST method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
        String sub_name = user.name;
        String sub_old = user.old;
    }
}

@ModelAttribute

Client๊ฐ€ ์ „์†กํ•˜๋Š” HTTP parameter, Body ๋‚ด์šฉ โ†’ Setter ํ•จ์ˆ˜ โ†’ 1 : 1๋กœ ๊ฐ์ฒด์— ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•จ

  • @RequestParam or @PathVariable์ฒ˜๋Ÿผ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์ˆ˜ ํ•˜๋‚˜๋‹น ํ•˜๋‚˜์”ฉ ์ ๊ธฐ ๋„ˆ๋ฌด ๋งŽ์„ ๋•Œ โ†’ ํ•œ๋ฒˆ์— ๊ฐ€์ ธ์˜ด

  • ๊ฐ์ฒด์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์Œ

  • ๊ฐ์ฒด์— @Setter ์ฒจ๋ถ€ ํ•„์ˆ˜

  • ํŒŒ๋ผ๋ฏธํ„ฐ์˜ @ModelAttribute๋Š” ์ƒ๋žต๊ฐ€๋Šฅ

  • @RequestBody๊ฐ€ Json์„ ๋ฐ›๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, @ModelAttribute๋Š” Json์„ ๋ฐ›์„ ์ˆ˜ ์—†์Œ


@PostMapping("/form/model")
    @ResponseBody
    public String helloRequestBodyForm(@ModelAttribute Star star) {
		// ModelAttribute ์‚ฌ์šฉ -> ํ•œ๋ฒˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ๊ฐ€์ ธ์˜ด
        // ๊ฐ์ฒดํ˜•์‹์œผ๋กœ ๋ฐ›์•„์„œ ๊ฐ€์ ธ์˜ด
        return String.format("Hello, @RequestBody.<br> (name = %s, age = %d) ", star.name, star.age);
    }

@ResponseBody

๋ฉ”์„œ๋“œ ๋ฆฌํ„ด ๊ฐ’ โ†’ View๋กœ ์ถœ๋ ฅ โŒ

  • HTTP Response Body์— ์ง์ ‘ ์“ฐ์—ฌ์ง

  • return โ†’ Json, xml ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ


@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String getUser(@RequestParam String nickname, @RequestParam(name="old") String age {
        // GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
        // https://naver.com?nickname=dog&old=10
        User user = new User();
        user.setName(nickname);
        user.setAge(age);
        return user;
    }
}

@Autowired

Spring ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ Bean ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•

Bean ๊ฐ์ฒด์ฃผ์ž… ๋ฐฉ๋ฒ•
1@Autowired ์–ด๋…ธํ…Œ์ด์…˜
2@AllArgsConstructor ์ƒ์„ฑ์ž
3setter

@GetMapping

RequestMapping(Method=RequestMethod.GET)์™€ ๊ฐ™์€ ์—ญํ• 


@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/")
    public String getUser(Model model) {
        //  GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
    
    ////////////////////////////////////
    // ์œ„์™€ ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋Š” ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. //
    ////////////////////////////////////

    @RequestMapping(method = RequestMethod.GET)
    public String getUser(Model model) {
        //  GET method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
}

@PostMapping

RequestMapping(Method=RequestMethod.POST)์™€ ๊ฐ™์€ ์—ญํ• 


@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(method = RequestMethod.POST)
    public String addUser(Model model) {
        //  POST method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }

    ////////////////////////////////////
    // ์œ„์™€ ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋Š” ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. //
    ////////////////////////////////////

    @PostMapping('/')
    public String addUser(Model model) {
        //  POST method, /user ์š”์ฒญ์„ ์ฒ˜๋ฆฌ
    }
}

@RequiredArgsConstructor

final, @Notnull์ด ๋ถ™์€ ํ•„๋“œ์˜ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์คŒ

  • ์›๋ž˜ ์˜์กด์„ฑ ์ฃผ์ž… โ†’ ์ƒ์„ฑ์ž, Setter, Field ํƒ€์ž…์˜ ๋ฐฉ์‹์‚ฌ์šฉ

  • Lombok์˜ @RequiredArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ


@Service
@RequriedArgsConstructor // final ๋ถ™์€ ํ•„๋“œ -> ์ƒ์„ฑ์ž ์ž๋™์œผ๋กœ ๋ถ™์—ฌ์คŒ
public class TestService {
	private final TestRepository1 testRepository1; // final -> ์ƒ์ˆ˜
	private final TestRepository2 testRepository2;
}

@EntityListeners

Entity ๋ณ€ํ™”๊ฐ์ง€ โ†’ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ์กฐ์ž‘

  • Column๊ฐ’์ด ์ž๋™์œผ๋กœ ์ˆ˜์ •๋จ

@Id

primary key (๊ธฐ๋ณธํ‚ค_๋ฐ์ดํ„ฐ ์ค‘๋ณต์„ ๋ง‰์Œ) ๋ณ€์ˆ˜ ์„ ์–ธ

  • JPA์—์„œ Entity ๊ฐ์ฒด ์ •์˜ ์‹œ, @Id ์†์„ฑ์„ ํ•จ๊ป˜ ์ •์˜ํ•ด์•ผํ•จ

๐Ÿ’ก ์ง์ ‘ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•

  • @Id ์–ด๋…ธํ…Œ์ด์…˜๋งŒ์œผ๋กœ Id๋ฅผ ์ง€์ •ํ•จ

๐Ÿ’ก ์ž๋™์ƒ์„ฑ

  • @Id์™€ @GeneratedValue ๊ฐ™์ด ์‚ฌ์šฉ

  • GenerationType ์˜ต์…˜์œผ๋กœ ์ „๋žต์„ ์ง€์ •ํ•จ

๐Ÿ“ TABLE
	- ํŠน์ • ๋ฒค๋”์— ์˜์กด์ ์ด์ง€ ์•Š์Œ
    - ์‹œํ€€์Šค ํ…Œ์ด๋ธ” โ†’ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œํ€€์Šค๋ฅผ ํ‰๋‚ด๋‚ผ Id ํ• ๋‹น

๐Ÿ“ IDENTITY
	- PK (๊ธฐ๋ณธํ‚ค) ์ƒ์„ฑ์„ DB์—๊ฒŒ ์œ„์ž„ (MySQL = AUTO INCREMENT)
    - ํŠน์ • DB ๋ฒค๋”์— ์˜์กด์ ์ž„

๐Ÿ“ SEQUENCE
	- DB์˜ sequence ๊ฐ์ฒด ์ด์šฉ โ†’ ์œ ์ผํ•œ ๊ฐ’์„ ์ˆœ์„œ๋Œ€๋กœ ์ƒ์„ฑ
    - ํŠน์ • DB ๋ฒค๋”์— ์˜์กด์ ์ž„

๐Ÿ“ AUTO
	- DB๋ฒค๋”์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ 3๊ฐ€์ง€ ์ „๋žต ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•จ
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

@Column

๊ฐ์ฒดํ•„๋“œ(๋ณ€์ˆ˜)์™€ DB column์„ ๋งตํ•‘ํ•จ

๐Ÿ’ก ์†์„ฑ

  • name : ๋งตํ•‘ํ•  ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์ด๋ฆ„์„ ์ง€์ •

  • insertable : ์—”ํ‹ฐํ‹ฐ ์ €์žฅ ์‹œ ์„ ์–ธ๋  ํ•„๋“œ๋„ ๊ฐ™์ด ์ €์žฅ

  • updateable : ์—”ํ‹ฐํ‹ฐ ์ˆ˜์ • ์‹œ ์ด ํ•„๋“œ๋ฅผ ํ•จ๊ป˜ ์ˆ˜์ •

  • table : ์ง€์ •ํ•œ ํ•„๋“œ๋ฅผ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ๋งตํ•‘

  • nullable : null์„ ํ—ˆ์šฉํ• ์ง€, ํ—ˆ์šฉํ•˜์ง€ ์•Š์„์ง€ ๊ฒฐ์ •

@Column(nullable = false) // null ํ—ˆ์šฉ ์•ˆํ•จ
	private String memberName;
  • unique : ์ œ์•ฝ์กฐ๊ฑด์„ ๊ฑธ ๋•Œ ์‚ฌ์šฉ

  • length : varchar์˜ ๊ธธ์ด๋ฅผ ์กฐ์ •ํ•จ (๊ธฐ๋ณธ๊ฐ’ 255)


Lombok Annotation

๐Ÿ“Œ Lombok : ์ฝ”๋“œ๋ฅผ ํฌ๊ฒŒ ์ค„์—ฌ, ๊ฐ€๋…์„ฑ์„ ํฌ๊ฒŒ ๋†’์—ฌ์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

  • @Setter : class ๋ชจ๋“  ํ•„๋“œ์˜ Setter method๋ฅผ ์ƒ์„ฑ

  • @Getter : class ๋ชจ๋“  ํ•„๋“œ์˜ Getter method๋ฅผ ์ƒ์„ฑ

  • @AllArgsConstructor : class ๋ชจ๋“  ํ•„๋“œ ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•จ

  • @NoArgsConstructor : class ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•ด์คŒ

  • @ToString : class ๋ชจ๋“  ํ•„๋“œ์˜ toString method๋ฅผ ์ƒ์„ฑํ•จ

profile
๐ŸฑSunyeon-Jeong, mallang developer๐Ÿฐ

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 18์ผ

์ •๋ง ์œ ์ตํ•œ ๊ธ€์ด์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ