Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping list of strings in mybatis

Tags:

mybatis

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.

like image 897
javaguy Avatar asked Oct 21 '14 11:10

javaguy


People also ask

What is parameterType in MyBatis?

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 .

What is Resultmap in MyBatis?

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.


1 Answers

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;
like image 102
Karthik Prasad Avatar answered Oct 07 '22 23:10

Karthik Prasad