我有以下方法,
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;
}
在xml文件中,我有以下代码。
<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>
当我替换(硬编码值)时,user_id=#{userId}
部分返回user_id=1
预期结果。但是当我从客户端应用程序传递它时,虽然值正确地设置为映射,但查询并没有使其正确并导致返回null。我在这里做错了什么。
我的ProjectMapper类的方法定义是,
public Project getProjectByUserId(long userIdParam);
更新 以下是服务接口方法
@GET
@Path("{userId}/{projectName}")
@Produces("application/json")
public Project getProjectByUser(@PathParam("userId") long userId);
以及上面的实现调用数据层方法(首先提到)
答案 0 :(得分:2)
尝试像这样定义你的映射器:
public Project getProjectByUserId(@Param("userId") long userIdParam);