場(chǎng)景
mysql數(shù)據(jù)庫(kù)中
字段為`authority_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '權(quán)限ID',
問(wèn)題
public interface BmRoleAuthorityRepository extends JpaRepository{
@Query(value = "SELECT authority_id FROM bm_role_authority where role_id = ?1", nativeQuery = true)
List getAllAuthorityIdByRoleId(Long roleId);
直接執(zhí)行,獲取到的List是
之后我想循環(huán)authorityIdList
for (Long authorityId : authorityIdList) {
結(jié)果報(bào)錯(cuò):
java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
解決方案
沒(méi)有看到有什么特別好的方法,只能將BigInterger的List先轉(zhuǎn)換為String,然后再轉(zhuǎn)換為L(zhǎng)ong List
List authorityIdList = bmRoleAuthorityRepository.getAllAuthorityIdByRoleId(roleId);
if(authorityIdList == null || authorityIdList.isEmpty()) {
authorityIdList = new ArrayList<>();
}
String idArray = JSONArray.toJSONString(authorityIdList);
之后再處理這個(gè)String,將其轉(zhuǎn)換為L(zhǎng)ong list
// [1,2,3] convert to String[]
String[] split = StringUtils.split(StringUtils.substring(allAuthorityIdByRoleId, 1,
allAuthorityIdByRoleId.length() - 1), ",");
// guava 將String[] convert to Long[]
authorityIdList.addAll(Lists.transform(Arrays.asList(split), Longs.stringConverter()));