Search code examples
javamysqljakarta-eemybatiswso2-as

Parameters not parsing to my batis mapper via a java.util.Map


I have following method,

public Project getProjectByUser(long userId)
            throws IOException {

        SqlSession sqlSession = null;
        Project response = null;

        Map<String, Long> projectParam = new HashMap<String, Long>();
        projectParam.put("userId", userId);

        try {

            sqlSession = DBSessionManager.getInstance().getSessionFactory()
                    .openSession();

            LOG.debug("SqlSession opened for Project mapper");

            ProjectMapper projectMapper = sqlSession
                    .getMapper(ProjectMapper.class);

            sqlSession.insert("getProjectByUserId", projectParam);
            sqlSession.commit();

            response = projectMapper.getProjectByUserId(userId);

        } finally {
            if (sqlSession != null) {
                sqlSession.close();
                LOG.debug("SqlSession closed");
            } else {
                System.out.println("_sql session null");
            }

        }

        return response;
    }

And in xml file i have the following code.

<select id="getProjectByUserId" resultMap="projectResultMap"
        parameterType="map" flushCache="false" useCache="false">
        SELECT
        project_id,
        user_id, project_name,
        created_date,
        last_updated_date FROM
        project
        WHERE
        user_id=#{userId}
    </select>

When I replaced (hard coded the value) the user_id=#{userId} part as user_id=1 expected result is returned. But when I pass it from client application though the value is set to the map correctly the query doesn't get it correct and results in a null return. What am I doing wrong here.

My ProjectMapper class's method definition is,

public Project getProjectByUserId(long userIdParam);

Update: Following is the service interface method,

 @GET
 @Path("{userId}/{projectName}")
 @Produces("application/json")
 public Project getProjectByUser(@PathParam("userId") long userId);

and implementation of the above calls the data layer method (first mentioned)


Solution

  • Try to define your mapper like this:

    public Project getProjectByUserId(@Param("userId") long userIdParam);