java MyBatis在偶尔使用selectByPrimaryKey时无法获取记录

krcsximq  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(153)

今天我发现同样的http请求到后端API服务器,有时它可以获取文章,有时它不能获取文章,这是我的代码:

  1. Article article = articleMapper.selectByPrimaryKey(id);

这是使用MyBatis Generator自动生成的代码:

  1. <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
  2. <!--
  3. WARNING - @mbggenerated
  4. This element is automatically generated by MyBatis Generator, do not modify.
  5. -->
  6. select
  7. <include refid="Base_Column_List" />
  8. from article
  9. where id = #{id,jdbcType=BIGINT}
  10. </select>

相同的HTTP请求,具有相同的文章ID。我不知道哪里出了问题。我的PostgreSQL版本是13。这是我的自动生成xml配置:

  1. <table tableName="article"
  2. enableCountByExample="true"
  3. enableUpdateByExample="true"
  4. enableDeleteByExample="true"
  5. enableSelectByExample="true"
  6. selectByExampleQueryId="true">
  7. <generatedKey column="ID" sqlStatement="JDBC" identity="true" />
  8. </table>

有没有线索能查出代码的错误我是一个新手关于PostgreSQL作为后端数据库。

  • 您要查询的文章是否存在?我相信ID是存在的。
  • 是否会同时进行修改?不,不可能。我再也不会编辑写进数据库后的文章。没有交易,文章是旧文章。
2jcobegt

2jcobegt1#

我在使用selectByPrimaryKey(null)查询记录后观察到了这一点。我在selectByPrimaryKey之前添加了一个检查,以确保它不会获得空参数。其他selectByPrimary(id)都可以。

相关问题