我正在尝试选择动态列。以下是我的代码:
//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>
这是我得到的结果:
[空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空,空]
我做错了什么?
最佳答案
这并不是因为你的参数是 map ,所以你可以这样改变并尝试
//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>
关于java - Mybatis在select语句中动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674309/