📄 newsdao.java
字号:
package org.imm.dao.hibernate;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.imm.dao.INewsDAO;
import org.imm.model.News;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class NewsDAO extends HibernateDaoSupport implements INewsDAO {
private static final Log log = LogFactory.getLog(NewsDAO.class);
//property constants
public static final String TITLE = "title";
public static final String CONTENT = "content";
protected void initDao() {
//do nothing
}
/* (non-Javadoc)
* @see org.imm.dao.hibernate.INewsDAO#save(org.imm.model.News)
*/
public void save(News transientInstance) {
log.debug("saving News instance");
try {
getHibernateTemplate().saveOrUpdate(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see org.imm.dao.hibernate.INewsDAO#delete(org.imm.model.News)
*/
public void delete(News persistentInstance) {
log.debug("deleting News instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see org.imm.dao.hibernate.INewsDAO#findById(java.lang.Integer)
*/
public News findById( java.lang.Integer id) {
log.debug("getting News instance with id: " + id);
try {
News instance = (News) getHibernateTemplate()
.get("org.imm.model.News", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see org.imm.dao.hibernate.INewsDAO#findByExample(org.imm.model.News)
*/
public List findByExample(News instance) {
log.debug("finding News instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding News instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from News as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByTitle(Object title) {
return findByProperty(TITLE, title);
}
public List findByContent(Object content) {
return findByProperty(CONTENT, content);
}
public News merge(News detachedInstance) {
log.debug("merging News instance");
try {
News result = (News) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(News instance) {
log.debug("attaching dirty News instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(News instance) {
log.debug("attaching clean News instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static INewsDAO getFromApplicationContext(ApplicationContext ctx) {
return (INewsDAO) ctx.getBean("NewsDAO");
}
//////////////////////////////////////////////////////
public void saveOrUpdate(News news,Integer id){
String queryString = "insert into News(newid,title,content) values("+"'news.getNewid()'"+"'news.getTitle()'"+"'news.getContent()'"+"where newid=?"+"'id'";
getHibernateTemplate().save(news,queryString);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -