본문 바로가기

서버

마이바티스 에러 해결 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

728x90

SpringBoot 4.x 와 Mybatis 4.x 버전 사용

 

Mybatis 최초 연결시 체크리스트를 만들어 보았다. 

 

 

1. application.yml 설정

mybatis:
  mapper-locations: classpath:/mapper/**/*.xml #쿼리위치
  type-aliases-package: com.example.test #xml에서의 파라미터, resultType 찾는 패키지
  configuration: #카멜케이스
    map-underscore-to-camel-case: true

  jpa:
    hibernate:
      ddl-auto: create

 

mybatis 설정은 들여쓰기 없이 제일 왼쪽으로 붙여서 추가

 


 

 

2. @MapperScan 혹은 @Mapper 어노테이션 확인

@MapperScan은 전역으로 설정(저는 이렇게 진행하였습니다.)

@Mapper는 특정 매퍼 인터페이스만 설정


@MapperScan 예시

@SpringBootApplication
@MapperScan("com.example.test.*.mapper")
public class ErpApplication {

	public static void main(String[] args) {
		SpringApplication.run(ErpApplication.class, args);
	}

}

@Mapper 예시

@Mapper
public interface TestMapper {
	List<Employee> getTest();
}

 

둘중 택일을 해서 사용해야한다. 

 

추가로 JPA와 함께 사용한다면 @MapperScan하는 패키지 대상에 JpaRepository Interface가 들어가면 안된다. 

 


3. 인터페이스 생성(@MapperScan으로 진행하는 경우)

public interface TestMapper {
	List<Employee> getTest();
}

 

 


 

4. mapper.xml 추가

 

/resources/mapper/test/TestMapper.xml

 

mapper의 파일명 규칙은 지켜주는 게 좋음. 

 

<?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.example.test.user.mapper.TestMapper">

    <select id="getTest">
        select *
        from test
    </select>


</mapper>

 

<mapper> 태그 외에도 추가해주어야함