Spring
Spring mybatis 단일파라미터 전송시 no getter 에러
pooney
2020. 5. 9. 17:00
흔히 조건절은 <choose> , <if> 태그가 대표적으로 존재한다. 단일 파라미터를 던져서 값에 따라 다르게 실행 시키고 싶은데 이때 when절에서 변수명을 잘못하여 getter 에러를 발생시키는 실수가 발생한다.
getter 에러 발생
controller
@Controller
public class HomeController {
@Inject
SqlSession sqlSession;
@RequestMapping(value = "pooney", method = RequestMethod.GET)
public String pooney(@RequestParam String id) {
System.out.println("id : "+ id);
List<String> list = sqlSession.selectList("list.selectlist" ,id);
System.out.println(list);
return "index";
}
}
xml (변경전)
<select id="selectlist" parameterType="string" resultType="String">
<choose>
<when test='id =="h11" '>
select pwd from test1
</when>
<otherwise>
select name from test2
</otherwise>
</choose>
</select>
실행화면
아래와 같이 getter 에러가 발생한다. 흔히 mybatis에서는 클래스의 getter , setter 메소드를 통해서 값을 넣거나 가져온다. 이때 단일 파라미터의 경우는 해당 메소드가 존재 하지 않아 mybatis에서 값을 가져 올 수 가 없어 에러가 발생한다.
Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'
해결방법
<when> 절에 변수명을 id -> _parameter로 변경하는 것이다. 나머지는 일반적인 mybatis의 문법과 같다
xml (변수명을 id -> _parameter변경 후 )
<select id="selectlist" parameterType="string" resultType="String">
<choose>
<when test="_parameter.equals('h11') ">
select userpwd from test1
</when>
<otherwise>
select name from test2
</otherwise>
</choose>
</select>
실행화면
정상적으로 값을 가져와서 쿼리문을 실행하는 것을 확인 할 수 있다.