java - Mybatis在select语句中动态列

标签 java spring mybatis spring-mybatis dynamicquery

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

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

相关文章:

oracle - 使用 HSQL 而不是 Oracle 对 MyBatis 进行单元测试

java - Java中如何将泛型类型转换为具体类型

java - 如何在 Spring Boot 中建立数据库连接之前运行自定义代码?

java.lang.NoSuchMethodError : org. hibernate.SessionFactory.openSession()Lorg/hibernate/Session;

java - 为什么当记录 > 约 600 条时查询性能急剧下降

mysql - 我想传递相同的ajax属性

java - 从 Android Processing Sketch 保存图像

java - JACK(Java Android Compiler Kit) 字母错误

java - 将 JPanel 的区域转换为 BufferedImage

Teiid Excel 数据源的 java.nio.channels.OverlappingFileLockException