Displaying a list of strings in mybatis
I want to delete a list of strings using mybatis. I am using the following code, but I am getting an illegalArgument exception like this:
javax.servlet.ServletException: org.mybatis.spring.MyBatisSystemException: SqlSession operation; nested exception is java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for java.lang.Object.toString
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
Mybatis config:
<select id="fetchIds" resultType="java.lang.String" >
SELECT distinct ids
FROM abc.emp
</select>
My Dao method:
@Override
public List<String> fetchIds() {
System.out.println("Inside fetchIds");
SqlSession session = null;
List retrieveList = null;
try{
session = sqlSessionFactory.openSession();
System.out.println("After session creation:"+session);
retrieveList = session.selectList("fetchIds");
}finally{
session.close();
}
return retrieveList;
}
Can anyone suggest how we can achieve this.
+3
source to share
1 answer
Define List
- List<String>
. Here your code will look like
<select id="fetchIds" resultType="string" >
SELECT distinct ids
FROM abc.emp
</select>
And the Tao method
@Override
public List<String> fetchIds() {
System.out.println("Inside fetchIds");
SqlSession session = null;
List<String> retrieveList = null;
try{
session = sqlSessionFactory.openSession();
System.out.println("After session creation:"+session);
retrieveList = session.selectList("fetchIds");
}finally{
session.close();
}
return retrieveList;
}
The interface file interface.java has a method signature as follows.
String[] fetchIds()throws Exception;
+9
source to share