mysql-react-springboot 연결하기 1
2024. 7. 26. 18:08ㆍSpringboot-React
간단하게 테스트 해보기.
mysql
CREATE TABLE IF NOT EXISTS user (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kh.mapper.UserMapper">
<select id="findAll">
select * from user
</select>
<!--
useGeneratedKeys : DB에서 auto_increment 자동 증가 설정을 했을 때 키 값을 자동으로 넣을 수 있도록 설정
keyProperty : "id" 자동으로 증가하는, 기준이 되는 키 설정은 어떤 컬럼을 바라볼 것인지 설정
-->
<insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into user(name, email) values(#{name}, #{email})
</insert>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
<update id="updateUser" parameterType="User">
update user set where name = #{name}, email = #{email} where id = #{id}
</update>
</mapper>
service, mapper는 생략
UserController
package com.kh.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.kh.dto.User;
import com.kh.service.UserService;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> findALl(){
return userService.findAll();
}
@PostMapping
public void insertUser(@RequestBody User user) {
userService.insertUser(user);
}
//requestbody : 전체를 가져옴
//requestparam : 하나만 가져옴
/*
///await axios.delete(`/users?id=${id}`);
@DeleteMapping("/{id}") //api 삭제를 진행하기 위해 만나는 주소(api)가 users/유저번호
public void deleteUser(@PathVariable int id) {
userService.deleteUser(id);
}
*/
//await axios.delete(`/users`, {params: {id}});
@DeleteMapping //api 삭제를 진행하기 위해 만나는 주소(api)가 users
public void deleteUser(@RequestParam("id") int id) {
userService.deleteUser(id);
}
@PutMapping
public void updateUser(@RequestBody User user) {
userService.updateUser(user);
}
}
스프링 부트는 여기까지.
'Springboot-React' 카테고리의 다른 글
네이버 로그인 1 (0) | 2024.07.30 |
---|---|
mysql-react-springboot 연결하기 2 (0) | 2024.07.26 |
tosspay구현하기 3 (springboot) (0) | 2024.07.24 |
tosspay구현하기 2 (springboot) (0) | 2024.07.24 |
tosspay 구현하기 1 (react) (0) | 2024.07.24 |