问题描述
我为spring / hibernate项目配置了一个自定义通用服务DAO - 这个想法是我可以从我的控制器轻松地重用它。
I have configured a custom generic service DAO for my spring / hibernate project - the idea being that I can reuse it easily from my controllers.
它基本上看起来像这个:
It essentially looks like this:
public class DefaultService<T> {
private Class<T> e;
public String className(Class<T> e) {
String clip = e.getName();
clip = clip.substring(clip.lastIndexOf('.') + 1, clip.length());
return clip;
}
public List<T> getAll(Integer status) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM " + className(e) + " WHERE status = " + status);
return query.list();
}
...
引用的是:
@Autowired
public DefaultService<Address> addressService;
addressService.get(1);
然而字符串剪辑= e.getName()
line抛出Null指针异常。如果我将类移动到属性部分(因此 addressService.get(Address.class,1)
),我可以让它工作,但我发现这有些不整洁,特别是在那里是否有多个不同的类被调用。
However the String clip = e.getName()
line throws a Null pointer exception. I can get this to work if I move the class into the attributes section (so addressService.get(Address.class, 1)
but I find this somewhat untidy, especially when there are multiple different classes being called upon.
有没有办法让类正确生成一个值而不必重复添加到我的所有函数中?
Is there some way to get the class to generate a value correctly without repeatedly adding it into all my functions?
提前致谢。
推荐答案
我做了类似的事情,你需要通用类来也是一个构造函数参数,我使用hibernate实体,但你可以传入表名字符串。
I did something similar, you need the generic class to be a constructor argument as well, mine uses hibernate entities, but you could pass in the string of table name.
public class DomainRepository<T> {
@Resource(name = "sessionFactory")
protected SessionFactory sessionFactory;
public DomainRepository(Class genericType) {
this.genericType = genericType;
}
@Transactional(readOnly = true)
public T get(final long id) {
return (T) sessionFactory.getCurrentSession().get(genericType, id);
}
然后您可以进行子类化(如果需要)来自定义或只是设置你在bean中的bean配置如下:t:
You can then subclass (if you need to) to customize or simply set up you bean in the spring config like below t :
<bean id="tagRepository" class="com.yourcompnay.data.DomainRepository">
<constructor-arg value="com.yourcompnay.domain.Tag"/>
</bean>
因此,在您的代码中,您可以像这样引用tagRepository(不需要其他代码,而不是上面发布的代码) ,以及):
So in your code you could then reference tagRepository like so (no other cod eis needed than that posted above, and below) :
@Resource(name = "tagRepository")
private DomainRepository<Tag> tagRepository;
此外,我将其称为存储库而非服务,服务处理不同类型及其交互(不只是一个)。特别是使用SQL字符串的示例:
Also, I would call it a repository not a service, a service deals with different types and their interactions (not just one). And for specifically your example using SQL strings :
public final String tableName;
public DomainRepository(String tableName) {
this.tableName = tableName;
}
public List<T> getAll(Integer status) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM " + tableName + " WHERE status = " + status);
return query.list();
}
并让你的bean定义如此
and have your beans defined like so
<bean id="addressRepository" class="com.yourcompnay.data.DomainRepository">
<constructor-arg value="address"/>
</bean>
然后你也可以在必要时自己创建子类:
And then you can alsow create subclasses youself where necessary :
public class PersonRepository extends DomainRepository<Person> {
public PersonRepository(){
super("person"); //assumes table name is person
}
这篇关于Spring Generic Dao类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!