[英]How to get column names in result set in Spring data JPA
我有列出用戶的簡單程序。 我使用@NamedStoredProcedureQueries
進行過程聲明,並使用EntityManager.createNamedStoredProcedureQuery
進行StoredProcedureQuery
。
它正確返回結果,但我需要列名,以便我知道哪個值對應哪個列。
我的代碼是這樣的
實體類
@Entity
@NamedStoredProcedureQueries({ @NamedStoredProcedureQuery(name =
"sGetUserList", procedureName = "sGetUserList", parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "user_id", type =
Integer.class) })
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String email;
//getters and setters
}
自定義存儲庫
public interface UserRepositoryCustom {
List<?> testProc() ;
}
存儲庫
public interface UserRepository extends JpaRepository<User, Long>,
UserRepositoryCustom{
}
存儲庫實現
public class UserRepositoryImpl implements UserRepositoryCustom{
@PersistenceContext
EntityManager em;
public List<Object> testProc() {
StoredProcedureQuery q = em.createNamedStoredProcedureQuery("sGetUserList");
q.setParameter("user_id", 1);
List<Object> res = q.getResultList();
return res;
}
}
我需要列名的結果。
在這里,我寫了一個方法從你可以得到JSON 你可以得到列和值的鍵值對
@Transactional
@Component
public class CustomRepository<T> {
@Autowired
private EntityManager em;
private ObjectMapper mapper = new ObjectMapper();
public List<T> getResultOfQuery(String argQueryString,Class<T> valueType) {
try {
Query query = em.createNativeQuery(argQueryString);
NativeQueryImpl nativeQuery = (NativeQueryImpl) query;
nativeQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> result = nativeQuery.getResultList();
List<T> resultList = result.stream()
.map(o -> {
try {
return mapper.readValue(mapper.writeValueAsString(o),valueType);
} catch (Exception e) {
ApplicationLogger.logger.error(e.getMessage(),e);
}
return null;
}).collect(Collectors.toList());
return resultList;
} catch (Exception ex) {
ApplicationLogger.logger.error(ex.getMessage(),ex);
throw ex;
}
}
}
唯一的條件是您的查詢和 pojo 屬性名稱應該相同
您可以在 Map 中獲取列名稱及其值。 即Map<'column-name', value>
。
Query query = entityManager.createNativeQuery("{call <<Your procedure>>}");
NativeQueryImpl nativeQuery = (NativeQueryImpl) query;
nativeQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> result = nativeQuery.getResultList();
這在您希望使用列名作為 HTML 中的占位符的地方非常有用,其中 value 將在運行時替換它。
我不確定我是否理解你在這里想要做什么。 如果您想讓所有用戶都使用 Spring 數據,則不應實施 UserRepository。 Spring Data 為你做這件事。
事實上 JpaRepository 已經有你需要的方法。
List<User> findAll();
您可以調用它來獲取所有用戶的列表,而無需擔心列名稱。
只需將您的存儲庫注入您需要的位置並調用該方法即可獲取所有用戶:
@Autowire
UserRepository userRepository;
List<Users> allUsers = userRepository.findAll();
編輯:如果您有特殊原因想要使用存儲過程,盡管有一種 Spring Data 方法可以在不自己實現 UserRepository 的情況下執行此操作。 您可以通過定義以下方法來做到這一點:
public interface UserRepository extends JpaRepository<User, Long>{
@Procedure(name = "sGetUserList")
List<User> sGetUserList(@Param("user_id") Integer userId);
}
同樣,使用此方法解析列名應該沒有任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.