기존에 있던 SpringBoot 프로젝트에 MyBatis와 MySQL을 연동하고 DB에 데이터를 insert 해주는 방법까지 알아보겠다!
Maven Repository 사이트 (https://mvnrepository.com/)에서 필요한 라이브러리를 검색 후 pom.xml에 코드를 추가해준다.
❗MySQL의 경우 version 태그에 오류가 발생한다. version 태그를 지우면 된다!
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties 파일에 아래 코드를 추가해준다.
#MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/DB스키마명?serverTimezone=UTC
spring.datasource.username=DB 아이디
spring.datasource.password=DB 비밀번호#mybatis
mybatis.mapper-locations=classpath:/mapper/*.xml
** classpath = src/main/resource
public class MemberVO {
private String id;
private String password;
public MemberVO(String id, String password) {
this.id = id;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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.springboot.test.mapper.MemberMapper">
<insert id="save">
INSERT INTO member(id, password)
VALUES (#{mem.id}, #{mem.password});
</insert>
</mapper>
package com.springboot.test.mapper;
import com.springboot.test.vo.MemberVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface MemberMapper {
void save(@Param("mem") MemberVO memberVO);
}
- mapper.xml의 mapper namespace="com.springboot.test.mapper.MemberMapper" 부분은 mapper 인터페이스의 경로이다.
- mapper.xml의 insert id="save" 의 id와 mapper 인터페이스의 메소드명과 같다.
- mapper.xml의 #{mem.id}, #{mem.password}은 mapper 인터페이스의 @Param("mem") 와 같다.
import com.springboot.test.mapper.MemberMapper;
import com.springboot.test.vo.MemberVO;
import org.springframework.stereotype.Service;
@Service
public class MemberService {
private MemberMapper memberMapper;
public MemberService(MemberMapper memberMapper) {
this.memberMapper = memberMapper;
}
public void addMember(MemberVO memberVO) {
memberMapper.save(memberVO);
}
}
import com.springboot.test.service.MemberService;
import com.springboot.test.vo.MemberVO;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class MemberController {
private MemberService memberService;
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
@PostMapping("/signUp/do")
public void signup(@RequestParam Map<String, String> signupForm) {
MemberVO memberVO = new MemberVO(signupForm.get("id"), signupForm.get("password"));
memberService.addMember(memberVO);
}
}
postman으로 localhost:8080/signup 으로 id와 password의 데이터를 보내주고 DB를 확인해보면 데이터가 잘 들어와있다!!
[출처]