我在eclipse中開發Web應用程序Spring和Hibernate。 我想將我的Hibernate查詢或Sql查詢存儲在屬性文件中,並稍後在DAO中使用它。 請讓我知道如何做到這一點? 我使用泛型DAO,有沒有什麼辦法可以進行泛型查詢? 後來我想存儲其他東西,如頁面鏈接,標題也在屬性文件中。春季屬性文件中外化hibernate查詢或sql查詢
所以基本上有三個相互關聯的問題。
因爲我想不出任何方法來進行通用查詢所以我重寫了具體類中的findAll()方法。
我使用
<context:property-placeholder location="classpath:properties/database.properties"/>
加載properties.But的問題是如何用它來獲得Java代碼或類文件HQL?
DAO
public abstract class AbstractHibernateDAO<T extends Serializable> {
public Class<T> clazz;//class object reference
protected SessionFactory mysessionFactory;
public void setClazz(final Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
protected Session getCurrentSession() {
return mysessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
我沈志南DAO
@Repository("categDAO")
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS,value="request")
public class CategoryDAO extends AbstractHibernateDAO<category_pojo>{
public CategoryDAO() {
setClazz(category_pojo.class);
}
@Override
public category_pojo findOneByName(String name) {
return (category_pojo) getCurrentSession().createQuery("from "+clazz.getName()+" where categoryName=:name").setParameter("name",name).uniqueResult();
}
}
你已經看到如何在春季使用@Value ..?檢查這個:http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/Value.html – user2339071
@value被用來初始化變量我在嘗試實現是不同的。例如,使用這個私有的@Value(「$ {xprnOffset}」)int expirationOffset; – beinghuman
你願意在屬性文件中硬編碼查詢嗎? – user2339071