MyBatis

7. SpringBoot로 MyBatis연동하기 - 패스트캠퍼스 백엔드 부트캠프 3기

gkss2tpt 2025. 3. 4. 17:46

1. 설정

  • pom.xml
<?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.3.9</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
       <license/>
    </licenses>
    <developers>
       <developer/>
    </developers>
    <scm>
       <connection/>
       <developerConnection/>
       <tag/>
       <url/>
    </scm>
    <properties>
       <java.version>17</java.version>
    </properties>
    <dependencies>
       <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>3.0.0</version>  <!-- 최신 버전으로 사용 -->
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jdbc</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
          <optional>true</optional>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
       </dependency>
          <!-- MySQL JDBC Driver -->
          <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>8.0.30</version>  <!-- 최신 버전을 사용하세요 -->
          </dependency>
       
       <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.36</version>
          <scope>provided</scope>
       </dependency>


    </dependencies>

    <build>
       <plugins>
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
       </plugins>
    </build>

</project>
  • lombok, mysql, mybatis, spring boot web, spring boot devtool, spring boot jdbc, thymleaf의 라이브러리를 추가
  • application.properties
spring.application.name=demo

spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=4862
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# MyBatis 설정
# Mapper XML 파일의 위치
mybatis.mapper-locations=file:src/main/resources/mappers/**/*.xml
# 도메인 클래스 위치
mybatis.type-aliases-package=com.example.demo.domain

mybatis.inject-sql-session-on-mapper-scan=true

 

  • datasource빈을 등록, mybatis의 mapper위치와 domain클래스의 위치를 적어주었다.
  • boardMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0/EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.domain.UserMapper">
    <select id="select" resultType="com.example.demo.domain.UserDto" parameterType="String">
        SELECT *
        FROM USER_INFO
        WHERE ID=#{ID}
    </select>
</mapper>
  • 간단하게 테스트용으로 select를 만들어주었다.
  • UserDto
@Setter
@Getter
@ToString
public class UserDto {
    private String id;
    private String pwd;
    private String name;
    private Date birth;
    private String email;
    private Date reg_date;

    UserDto(){}

    public UserDto(String id, String pwd, String name, Date birth, String email) {
        this.id = id;
        this.pwd = pwd;
        this.name = name;
        this.birth = birth;
        this.email = email;
    }
}

 

  • 데이터를 전달해줄 UserDto클래스를 작성
  • UserDaoImpl
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    SqlSessionTemplate session;

    String namespace = "com.example.demo.domain.UserMapper.";

    @Override
    public UserDto select(String id){
        return session.selectOne(namespace+"select", id);
    }
}
  • UserDaoImpl을 userMapper.xml과 연결시켜주었다.
  • Test
@SpringBootTest
public class UserDaoImplTest {

    @Autowired
    UserDao userDao;

    @Test
    public void select() {
        UserDto d = userDao.select("asdf");

        System.out.println("d = " + d);
    }
}
  • Test성공