我目前在我的(小)项目中使用分层体系结构,并考虑使用用于插入/更新/删除方法的反射来扩展我的BaseRepository(类特定的存储库从中继承)。分层体系结构 - 存储库和反射
我BaseRepository.java
看起来是这样的:
public abstract class BaseRepository<CLASS extends BaseDomain<CLASS>>{
}
哪里BaseDomain
就是:
public class BaseDomain<CLASS extends BaseDomain<CLASS>> {
private Long id;
//getters setts etc.
}
现在我的问题是在deleteById方法在BaseRepository:
public int deleteById(Connection connection, Long id) throws SQLException {
PreparedStatement delete= connection.prepareStatement("DELETE FROM ? WHERE id = ?");
delete.setObject(1, entity) //heres the problem, where do I get entity from?
delete.setObject(2, id);
return 0;
}
我的具体版本库像这样扩展了Base:
public class UserRepository extends BaseRepository<User>{
}
如何得到User
类的BaseRepository
所以我可以用它在删除/更新等方法呢?
感谢您的帮助!
我不认为你需要一个连接对象的方法的参数列表。 – kevinjom