使用 Mule 3.5.x+ 从 mysql 数据库获取受影响的行值

Get affected-rows value from mysql DB using Mule 3.5.x+

在尝试写入 table 时,我需要知道该行是否已更新(创建新行或更新现有行)。我会从受影响的行值中得到这个值。

以前,解决方案是构建一个类似于 org.mule.transport.jdbc.sqlstrategy.SimpleUpdateSqlStatementStrategy 的 class 并将其重新实现为 return 受影响的行值而不是行本身。

但是,JDBC 传输似乎已被弃用,取而代之的是数据库连接器,我发现很难找到代码或如何实现该功能。

解决方案:

大卫的回复让我走上正轨!谢谢大卫。

不过,此问题已在 5.1.27 及更高版本的 mysql-connector-java 中修复。

所以,需要升级 POM。

mysql mysql-connector-java 5.1.27

我使用 DBCP2 获得了正确的结果,如下所示:

    <spring:beans>
         <spring:bean id= "jdbcDataSource" class ="org.apache.commons.dbcp.BasicDataSource" name= "Bean">
          <spring:property name= "driverClassName" value ="com.mysql.jdbc.Driver" />           
          <spring:property name= "username" value = "root"/>
          <spring:property name= "password" value = "root"/>  
          <spring:property name="url" value="jdbc:mysql://localhost:3306/local1?useAffectedRows=true"/>
            <!-- Following properties added for having auto reconnect  mechanism-->
          <spring:property name= "testOnBorrow" value = "true"/>
          <spring:property name= "validationQuery" value = "select 1"/>
         </spring:bean>
    </spring:beans>   <db:mysql-config name="MySQL_Configuration" dataSource-ref="jdbcDataSource" doc:name="MySQL_Configuration"/>

来自documentation

target #[payload] The enricher expression used to enrich the current message with the result of the SQL processing.

这应该允许您定义一个表达式,告诉 Mule 在哪里存储更新结果,即更新的行数。

来自the integration test suite

<db:update config-ref="dbConfig"
           target="#[header:OUTBOUND:updateCount]">
  <db:parameterized-query>update PLANET set NAME='Mercury' where POSITION=4</db:parameterized-query>
</db:update>

这会设置一个名为 updateCount 的出站 属性 以及更新的行数。我个人建议改为使用 #[flowVars['updateCount']] 设置流变量,但这取决于您的用例。