SPRING

[SPRING BOOT/스프링 부트] MyBatis 파라미터 사용할 때 Dao

merryna 2021. 5. 30. 14:35
반응형

 

id 값을 받아와서 id 별 상세보기 페이지를 만드려고 했는데 계속 SQL 문법 오류가 났다..

다시 이 실수를 하지 않기 위해서 정리

Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3 ### The error may exist in com/example/demo/dao/ArticleDao.xml ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: SELECT * FROM article WHERE id = {#id }; ### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3 ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3

org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3 ### The error may exist in com/example/demo/dao/ArticleDao.xml ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: SELECT * FROM article WHERE id = {#id };

 

Dao.Java

파라미터를 쓰기 위해서 @Param 어노테이션이 필요함

Article getArticleById(@Param("id") int id);

 

Dao.xml

맵퍼에서는 속성 명 = #{파라미터 명} 이렇게 사용한다.

<select id="getArticleById" resultType="Article">
    SELECT * 
    FROM article 
    WHERE id = #{id }
</select>


어이없게도 나는 id = {# id } 이런 식으로 작성해서 틀렸던 것..!

반응형