I want to retieve list of strings using mybatis.I am using following code ,but getting illegalArgument exception as follow :
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 configuration :
<select id="fetchIds" resultType="java.lang.String" >
SELECT distinct ids
FROM abc.emp
</select>
My Dao method is :
@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 anybody please suggest how we can achieve this.
parameterType. The fully qualified class name or alias for the parameter that will be passed into this statement. This attribute is optional because MyBatis can calculate the TypeHandler to use out of the actual parameter passed to the statement. Default is unset .
Result Maps finish the job by mapping the result of a database query (a set of columns) to object properties. Next to Mapped Statements, the Result Map is probably one of the most commonly used and most important features to understand.
Define the List
to List<String>
. Here your code would look like
<select id="fetchIds" resultType="string" >
SELECT distinct ids
FROM abc.emp
</select>
And Dao 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 mapper interface.java file has method signature as follows.
String[] fetchIds()throws Exception;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With