我试图选择动态列。以下是我的代码:
//call the dao method
String columns = "first_name";
userDao.sample(1, columns);
//call mapper
List<User> sample(@Param("userId") int userId, @Param("columns") String columns);
//mapper
<select id="sample" resultMap="user" parameterType="map">
SELECT
#{columns}
FROM
user
WHERE
userId = #{userId}
</select>
这是我得到的结果:
[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null, null,null,null,null]
我做错了什么?
答案 0 :(得分:2)
它不能因为您的参数是地图所以您可以像这样更改并尝试
//call the dao method
String columns = "first_name";
HashMap map = new HashMap();
map.put("userId",userId);
map.put("columns",columns);
userDao.sample(map);
//call mapper
List<User> sample(HashMap map);
//mapper
<select id="sample" resultMap="user" parameterType="map">
SELECT
#{columns}
FROM
user
WHERE
userId = #{userId}
</select>
答案 1 :(得分:-1)