简体   繁体   English

选择语句中的Mybatis动态列

[英]Mybatis dynamic column in select statement

I'm trying to select dynamic column. 我正在尝试选择动态列。 Below are my codes: 以下是我的代码:

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

This is the result I'm getting: 这是我得到的结果:

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

What am I doing wrong? 我究竟做错了什么?

its cannot becauase your parameter is map so you can change like this and try that 它不能因为您的参数是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>

you can also use the SQL section like in the official docs: 您还可以像在官方文档中那样使用SQL部分:

http://www.mybatis.org/mybatis-3/sqlmap-xml.html http://www.mybatis.org/mybatis-3/sqlmap-xml.html

Section SQL SQL节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM