假設我有兩個模型,對于每個模型,我都有一個 JPA 存盤庫介面,如下所示:
public interface IPersonJPARepository extends JpaRepository<Person, Long> {
.....
}
public interface ICountryJPARepository extends JpaRepository<Country, Long> {
.....
}
然后我想為每個模型都有一個 DAL 類,我可以在其中使用 ORM 方法進行 CRUD。例子:
@Repository
public class PersonDal implements IPersonDal {
@Autowired
IPersonRepository repo;
@Override
public List<Person> getAll() {
return repo.findAll();
}
}
@Repository
public class CountryDal implements ICountryDal {
@Autowired
ICountryRepository repo;
@Override
public List<Country> getAll() {
return repo.findAll();
}
}
然后在啟動 Sonarqube 分析我的代碼時出現問題,因為肯定的是,Sonarqube 看到在兩個 getAll() 方法中我都使用同一行來獲取特定模型的所有物件。所以我的問題是這個 Sonarqube 問題的解決方案是什么?
uj5u.com熱心網友回復:
遵循變數的命名約定。變數名可以是代表名詞而不是一般詞。在您的情況下,更改如下:
@Autowired
ICountryRepository countryRepository; // or countryRepo
和
@Autowired
IPersonRepository personRepository; // or personRepo
如果您確實有重復的代碼,請將其復制到介面或超類并使用繼承擴展/實作父類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336940.html
