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>