개발/JAVA

myBatis selectone null 처리

료팡 2017. 11. 21. 16:08
myBatis에서 selectone으로 검색시 결과값이 없는데 int절에 넣고 싶을때가 있을것이다. int절에 넣을때 nullpointerException 오류가 발생하게 된다.
 
//service
int testCnt = testDAO.selectTestCnt(testVO);
//mapper

//dao
public int selectTestCnt(TestVO testVO) {
	return ((Integer) sqlSession.selectOne(NAMESPACE + "selectTestCnt", testVO)).intValue();
}
service 단에서 nullpointerException 오류 발생 이럴때는..........이렇게 해보세요~~!!!
//int testCnt = testDAO.selectTestCnt(testVO);
//dao
public int selectTestCnt(TestVO testVO) {
	return sqlSession.selectOne(NAMESPACE + "selectTestCnt", testVO;
}
//service
Integer testCnt = testDAO.selectTestCnt(testVO);
testCnt = testCnt == null ? 0 : testCnt;