Spring 引导 - 将多个参数传递给存储过程的更简洁或优雅的方式 spring 数据存储库
Spring Boot - more cleaner or elegant way to pass multiple parameters to stored procedure spring data repository
如何在 Repository 接口的参数方法中将多个参数作为 DTO 或实体 class 传递 Spring Data
我有一个过程接收多个 IN 类型的参数,我在 SonarQube 中的代码有问题
Methods should not have too many parameters
我的实体:
@Entity
@NamedStoredProcedureQuery(name = "MySPName", procedureName = "MySPName", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter1", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter2", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter3", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter4", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter5", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter6", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter7", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter8", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter9", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter10", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter11", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter12", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter13", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "resultCode", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "resultMessage", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "name", type = String.class)
})
public class MyEntitySP implements Serializable {
@Id
@Column(name = "resultCode")
private Integer resultCode;
@Column(name = "resultMessage")
private String resultMessage;
@Column(name = "name")
private String name;
}
我的存储库:
public class MyRepository extends JpaRepository<MyEntitySP, Integer> {
@Procedure("MySPName")
MyEntitySP callSP(@Param("parameter1") String parameter1, @Param("parameter2") String parameter2, @Param("parameter3") String parameter3, @Param("parameter4") String parameter4, @Param("parameter5") String parameter5, @Param("parameter6") String parameter6, @Param("parameter7") String parameter7, @Param("parameter8") String parameter8, @Param("parameter9") String parameter9, @Param("parameter10") String parameter10, @Param("parameter11") String parameter11, @Param("parameter12") String parameter12, @Param("parameter13") String parameter13)
}
我可能什么都喜欢Class 参数:
public class MyParameters {
@Param("parameter1")
private String parameter1;
@Param("parameter2")
private String parameter2;
@Param("parameter3")
private String parameter3;
@Param("parameter4")
private String parameter4;
@Param("parameter5")
private String parameter5;
@Param("parameter6")
private String parameter6;
@Param("parameter7")
private String parameter7;
@Param("parameter8")
private String parameter8;
@Param("parameter9")
private String parameter9;
@Param("parameter10")
private String parameter10;
@Param("parameter11")
private String parameter11;
@Param("parameter12")
private String parameter12;
@Param("parameter13)
private String parameter13;
}
也许存储库:
public class MyRepository extends JpaRepository<MyEntitySP, Integer> {
@Procedure("MySPName")
MyEntitySP callSP(MyParameters parameters)
}
可能吗? - 请帮助我*
可以使用 @Query
注释调用存储过程来执行本机查询(参见:https://www.baeldung.com/spring-data-jpa-stored-procedures)。
通过遵循该方法并使用 SPEL(参见:https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions),我认为您可以得到类似的东西:
public class MyRepository extends JpaRepository<MyEntitySP, Integer> {
@Query("MY_PROCEDURE(:#{#parameters.parameter1},
:#{#parameters.parameter2},
....", nativeQuery = true)
MyEntitySP callSP(MyParameters parameters)
}
我在使用 @Procedure
时没有看到任何简单的解决方案,但是,作为 Spring,您可能可以应用一些自定义参数解析器
如何在 Repository 接口的参数方法中将多个参数作为 DTO 或实体 class 传递 Spring Data
我有一个过程接收多个 IN 类型的参数,我在 SonarQube 中的代码有问题 Methods should not have too many parameters
我的实体:
@Entity
@NamedStoredProcedureQuery(name = "MySPName", procedureName = "MySPName", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter1", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter2", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter3", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter4", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter5", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter6", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter7", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter8", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter9", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter10", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter11", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter12", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "parameter13", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "resultCode", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "resultMessage", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "name", type = String.class)
})
public class MyEntitySP implements Serializable {
@Id
@Column(name = "resultCode")
private Integer resultCode;
@Column(name = "resultMessage")
private String resultMessage;
@Column(name = "name")
private String name;
}
我的存储库:
public class MyRepository extends JpaRepository<MyEntitySP, Integer> {
@Procedure("MySPName")
MyEntitySP callSP(@Param("parameter1") String parameter1, @Param("parameter2") String parameter2, @Param("parameter3") String parameter3, @Param("parameter4") String parameter4, @Param("parameter5") String parameter5, @Param("parameter6") String parameter6, @Param("parameter7") String parameter7, @Param("parameter8") String parameter8, @Param("parameter9") String parameter9, @Param("parameter10") String parameter10, @Param("parameter11") String parameter11, @Param("parameter12") String parameter12, @Param("parameter13") String parameter13)
}
我可能什么都喜欢Class 参数:
public class MyParameters {
@Param("parameter1")
private String parameter1;
@Param("parameter2")
private String parameter2;
@Param("parameter3")
private String parameter3;
@Param("parameter4")
private String parameter4;
@Param("parameter5")
private String parameter5;
@Param("parameter6")
private String parameter6;
@Param("parameter7")
private String parameter7;
@Param("parameter8")
private String parameter8;
@Param("parameter9")
private String parameter9;
@Param("parameter10")
private String parameter10;
@Param("parameter11")
private String parameter11;
@Param("parameter12")
private String parameter12;
@Param("parameter13)
private String parameter13;
}
也许存储库:
public class MyRepository extends JpaRepository<MyEntitySP, Integer> {
@Procedure("MySPName")
MyEntitySP callSP(MyParameters parameters)
}
可能吗? - 请帮助我*
可以使用 @Query
注释调用存储过程来执行本机查询(参见:https://www.baeldung.com/spring-data-jpa-stored-procedures)。
通过遵循该方法并使用 SPEL(参见:https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions),我认为您可以得到类似的东西:
public class MyRepository extends JpaRepository<MyEntitySP, Integer> {
@Query("MY_PROCEDURE(:#{#parameters.parameter1},
:#{#parameters.parameter2},
....", nativeQuery = true)
MyEntitySP callSP(MyParameters parameters)
}
我在使用 @Procedure
时没有看到任何简单的解决方案,但是,作为 Spring,您可能可以应用一些自定义参数解析器