编程语言
首页 > 编程语言> > java – select语句中的Mybatis动态列

java – select语句中的Mybatis动态列

作者:互联网

我正在尝试选择动态列.以下是我的代码:

//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]

我究竟做错了什么?

解决方法:

它不能因为你的参数是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>

标签:spring-mybatis,java,spring,mybatis,dynamicquery
来源: https://codeday.me/bug/20190910/1798768.html