Nihar Patil
Nihar Patil

Reputation: 3

foreach for mybatis returns Null results

My mapper xml is like this.

<mapper namespace="EmployeeDaoMapper">
<resultMap id="employeeResultMap" type="Employee">
    <result property="employeeName" column="name"/>
    <result property="employeeId" column="employee_id"/>
</resultMap>


<select id="getEmployees" parameterType="list" resultMap="employeeResultMap">
<!--<select id="getEmployees" resultType="Employee">-->
    SELECT
    <foreach item="item" index="index" collection="list" separator="," >
        #{item}
    </foreach>
    FROM employees
    WHERE LOWER (name) LIKE LOWER(#{searchQuery} +'%') OR
    LOWER(login) LIKE LOWER(#{searchQuery} +'%') OR
    LOWER( employee_id ) LIKE LOWER(#{searchQuery} + '%')
    LIMIT #{resultsLimit}

</select>

EmployeeMapper Interface

List<Employee> getEmployees(@Param("searchQuery") String searchQuery, @Param("resultsLimit") int
        resultsLimit, @Param("list") List<String> attributes);

query returns list of null.

Instead of foreach, if i directly add column names then it works fine.

I am not sure what mistake i am making. I checked query, it is building query correctly, still unable to fetch results.

Upvotes: 0

Views: 1760

Answers (2)

blackwizard
blackwizard

Reputation: 2044

I agree with Jeff Butler.

I would add change parameterType="list" to parameterType="map" as named parameters are provided through a Map

Upvotes: 0

Jeff Butler
Jeff Butler

Reputation: 991

Try changing #{item} to ${item}. With #{item} you are telling MyBatis that there should be a prepared statement parameter marker and that is invalid for a salect column list. If you use ${item}, then MyBatis will write the string directly into the SQL.

Upvotes: 2

Related Questions