问题
like this:
public interface XXXRepository extends CrudRepository<XXX, Integer> {
@Query(value = "select * from ?1 where ...", nativeQuery = true)
List<XXX> findByXXX(String tableName, ...);}
It gives MYSQL syntax error with upon codes. The syntax error shows that the table name in the SQL is surrounded with "'".
回答1:
This is not possible. Parameters are only allowed in the where clause.
回答2:
You can use entitymanger in jpa project.
@Autowired EntityManager entityManager;
entityManager.createNativeQuery("select * from "+tableName+"")
来源:https://stackoverflow.com/questions/40956013/how-to-replace-table-name-with-value-from-parameter-while-using-spring-data-jpa