MyBatis๋ ํ์ฌ๊น์ง ๋ ๊ฑฐ์ ํ๋ก์ ํธ์์๋ง ์ฌ์ฉํ์๋ค.
https://mybatis.org/mybatis-3/ko/index.html
MyBatis – ๋ง์ด๋ฐํฐ์ค 3 | ์๊ฐ
๋ง์ด๋ฐํฐ์ค๋ ๋ฌด์์ธ๊ฐ? ๋ง์ด๋ฐํฐ์ค๋ ๊ฐ๋ฐ์๊ฐ ์ง์ ํ SQL, ์ ์ฅํ๋ก์์ ๊ทธ๋ฆฌ๊ณ ๋ช๊ฐ์ง ๊ณ ๊ธ ๋งคํ์ ์ง์ํ๋ ํผ์์คํด์ค ํ๋ ์์ํฌ์ด๋ค. ๋ง์ด๋ฐํฐ์ค๋ JDBC๋ก ์ฒ๋ฆฌํ๋ ์๋น๋ถ๋ถ์ ์ฝ๋์
mybatis.org
JPA๋ SQL๋ฌธ์ ๊ฑฐ์ง ์ฌ์ฉ์ํจ
MyBatis๋ SQL์์ฃผ
๋จ์ CRUD๋ JPA๊ฐ ํธํ๋ค
ํ ์ด๋ธ ๊ตฌ์ฑ์ด ๋ณต์กํ ๊ฒฝ์ฐ์๋ MyBatis ( join ์ฌ์ฉ์ด ํ์ํ ๋ )์ฌ์ฉ์ด ํธ๋ฆฌํ๋ค.
์คํ๋ง๋ถํธ์์ MyBatis๋ฅผ ์ฌ์ฉํด ๋ณด๊ฒ ๋น.
๋ ๊ฑฐ์ ํ๋ก์ ํธ์์๋ MyBatis์ฌ์ฉ์ ์ํด์ ๋ง์ ์ค์ ์ด ํ์ํ๋๋ฐ,
์คํ๋ง ๋ถํธ์์๋ ๋ง์ ์ค์ ์ด ํ์์๋ค.
๋ํ ๋ง์ฝ
๋ฐฑ์๋๋ ํ๋ก ํธ๋ ๋๋ ์ ํ๋ก์ ํธ๋ฅผ ํ๋ค๋ฉด
๋ฐฑ์๋์์๋ ๋ฐ์ดํฐ๋ง rest api๊น์ง๋ง ๋ณด๋
ํ๋ฉด์ ํ๋ก ํธ์๋์์ ํ๊ณ ํ๋ก ํธ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ํ๋ฉด์ ๋ณด์ฌ์ค


2.7.17๋ก ๋ณ๊ฒฝ!!
3.1.5 ๋ฒ์ ์ java 17๋ฒ์ ๋ถํฐ ๊ฐ๋ฅ

์นํ๋ก์ ํธ์๋ spring web ํ์
์๋ก๊ณ ์นจ spring boot devtool
get ,set ๋ฉ์๋ ์๋์์ฑ lombok
mybais framwork/ mysql driver - DB
finish
java ๋ฒ์ ์ ํ ์๋ชปํด์ ใ . 11๋ฒ์ ์ผ๋ก pom.xml ์์ ํ๊ณ ๋ฉ์ด๋ธ ์ ๋ฐ์ดํธํจ


User ํด๋์ค ์์ฑ

package com.myapp.mybatis.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor //๋ชจ๋ ํ๋ ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๋ง๋ ์์ฑ์
public class User {
private String id;
private String name;
private String phone;
private String address;
}
@AllArgsConstructor //๋ชจ๋ ํ๋ ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๋ง๋ ์์ฑ์
MySQL์์ ์คํค๋ง ์์ฑ
jpa์์๋ ์ํฐํฐ๋ฅผ ์ฌ์ฉํด์ ์๋์ผ๋ก ๋ง๋ค์๋๋ฐ
mybatis ์ฌ์ฉ์ ์ํด์๋ ํ ์ด๋ธ ๋ง๋ค์ด์ค์ผํจ


mybatis ์ค์

#MYSQL DB์ค์
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=1234
UserController ํจํค์ง์ usercontroller์์ฑ

package com.myapp.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.myapp.mybatis.mapper.UserMapper;
import com.myapp.mybatis.model.User;
@RestController //ํ๋ฉด์์ด ๋ฐ์ดํฐ๋ง ์ ๋ฌ
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/users/{id}")
public User getUser(@PathVariable("id") String id) {
User user = userMapper.getUser(id);
return user;
}
}
mapper ํจํค์ง ์์ฑ

์ธํฐํ์ด์ค๋ก UserMapper ์์ฑ

package com.myapp.mybatis.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.myapp.mybatis.model.User;
//jpa์ repository ์ญํ ์ด๋ค.
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
User getUser(String id);
}
๋ ๊ฐ์ ๋ฐฉ๋ฒ์ด ์๋๋ฐ
์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ์
๋ฐ๋ก sql๋ฌธ ์์ฑํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์์ ์ฝ๋๋ sql๋ฌธ์ ๋ฐ๋ก ์์ฑํ ๋ฐฉ๋ฒ์
๊ฒฐ๊ณผ๋ User์ผ๋ก ๋์ด
ํ ์คํธ๋ฅผ ์ํด์
mysql์ ์ ์ ํ๋ช ์ง์ ์ ๋ ฅํด์ค

๋ธ๋ผ์ฐ์ ์์ ํ์ธํ๊ธฐ
http://localhost:8080/user/1
์ ์ด์จ ํ์์ผ๋ก ๋ณด์ฌ์ค


์๋๋ mapper๋ฅผ service์์ ๊ฐ์ ธ์์ ์ฌ์ฉํ๊ณ ๊ทธ ํ์ ์ปจํธ๋กค๋ฌ๋ก ์ด๋ํ๋๋ฐ
์ผ๋จ์ ๊ฐ๋จํ๊ฒ ํด๋ณด๊ธฐ์ํด์ ์ปจํธ๋กค๋ฌ๋ก ๋ฐ๋ก ์ฌ์ฉํจ
๊ทธ๋ฌ๋ฉด ์ ์ฒด ์ ์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ๋ ค๋ฉด ์ด๋ป๊ฒ ์์ฑํ๋ฉด ๋ ๊น?
UserController
//๋ชจ๋ ์ ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
@GetMapping("/users")
public List<User> getUserList(){
List<User> userList = userMapper.getUserList();
return userList;
}
UserMapper
@Select("select * from user")
List<User> getUserList();

๋ธ๋ผ์ฐ์ ์์ ํ์ธํ๊ธฐ

์ ์ ๋ฐ์ดํฐ ์์ฑํ๊ธฐ (insert)

//์ ์ ๋ฐ์ดํฐ ์์ฑํ๊ธฐ
@PostMapping
public void createUser(@RequestParam("id") String id,
@RequestParam("name") String name,
@RequestParam("phone") String phone,
@RequestParam("address") String address) {
userMapper.insertUser(id,name,phone,address);
}
ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์ ์ ์ ์์ฑ
@Insert("insert into user values(#{id}, #{name}, #{phone}, #{address})")
void insertUser(String id, String name, String phone, String address);
ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ค
id,name, phone,address
postmapping์ ์ฃผ์์ฐฝ์ ์ ๋ ฅ์ด ๋ถ๊ฐ๋ฅํ๋ค
๊ทธ๋์ ์ด๋ฐ๊ฒ๋ค์(restapi) ํ ์คํธํ๊ธฐ์ํด ๋ง๋ค์ด์ง ํด์ด ์์.
Postman API Platform | Sign Up for Free
Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
www.postman.com
ํ์๊ฐ์ ํด์ผ ์ฌ์ฉ๊ฐ๋ฅํจ
๊ตฌ๊ธ๋ก ํจ




ADD request


get ์กฐํ
post ์ ๋ ฅ
put patch ์ ๋ฐ์ดํธ
delete ์ญ์

send ๋๋ฅด๋ฉด ์๋์ ๋ต๋ณ์ด ์ถ๋ ฅ๋จ.





send ํด๋ฆญํ๋ฉด ์๋์ ์ค๋ฅ ์๋๋์ง ํ์ธํจ
๋๋ 400 ์ค๋ฅ๊ฐ ๋ ์ ์คํ๋ง๋ถํธ ์ฝ๋ ํ์ธํด๋ณด๋๊น
id String ์ธ๋ฐ int๋ก ์ ๋ ฅํ์
500์ sql๋ฌธ ์๋ชป์ ๋ ฅํ๋๊น ๋์ค๋์ด๋ค..


DB์์๋ ํ์ธํ๊ธฐ

์ ์ ์ ๋ณด ์์ ํ๊ธฐ
UserController
// ์ ์ ์ ๋ณด ์์ ํ๊ธฐ
@PutMapping("/{id}")
public void editUser(@PathVariable("id") String id,
@RequestParam("name") String name,
@RequestParam("phone") String phone,
@RequestParam("address") String address) {
userMapper.updateUser(id, name, phone, address);
}
UserMapper
//์ ์ ์์ ํ๊ธฐ
@Update("update user set name=#{name}, phone=#{phone}, address=#{address} "
+ "where id=#{id}")
void updateUser(String id, String name, String phone, String address);
http://localhost:8080/users/3?name=ํญ์ &phone=010-1111-1113&address=๋ถ์ฐ์ง๊ตฌ 1๋ 3
์์ด๋๊ฐ 3์ธ ์ ์ ๋ฅผ ์์ ํ ๊ฑฐ๋๊น ์์ ๊ฐ์ด ์์ ํด์ค์ผํจ .
(id-3)ํญ์๋ฅผ ์๋์ ๊ฐ์ด ์์ ํ ๊ฑฐ์


200ok ๋์ค๋ฉด ์ ์์ ์ผ๋ก ์์ ๋๊ฑฐ์

์ ์ ์กฐํ


์ ์ ์ญ์ ํ๊ธฐ
UserController
// ์ ์ ์ญ์
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable("id") String id) {
userMapper.deleteUser(id);
}
UserMapper
//์ ์ ์ญ์ ํ๊ธฐ
@Delete("delete from user where id=#{id}")
void deleteUser(String id);



์๋ฐ ๋ฒ์ ์ฒดํฌ
์ฐธ๊ณ ํ๊ธฐ
'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์คํ๋ง๋ถํธ์์ JSP ์ค์ (0) | 2023.11.14 |
|---|---|
| ์คํ๋ง ๋ถํธ - MyBatis 2 / xml ๋งคํ (0) | 2023.11.14 |
| ์คํ๋ง๋ถํธ ๊ฒ์ํ ๋ง๋ค๊ธฐ ๊ฐ์ (0) | 2023.11.13 |
| h2 -> MySQL๋ก DB๋ณ๊ฒฝํ๊ธฐ (1) | 2023.11.13 |
| ์ง๋ฌธ/๋ต๋ณ ์ถ์ฒ ๊ธฐ๋ฅ (0) | 2023.11.13 |