此例子有几个重要的知识点
1,重写Basedao的作用 就是替代原来SSH项目中每次在DAOimpl包里面的java类都要继承HibernateDaoSupport,降低代码的耦合性,增强了代码的可读性,一旦项目需求发生了更改,就不必大量修改项目的源代码;
这是BaseDAO中的代码
@Autowired//自动装配
private SessionFactory sessionFactory;//建立sessionfactory工厂
private HibernateTemplate hibernateTemplate;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Autowired
public void setHibernateTemplate(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
这是继承DAO接口的实现的代码
@Repository
public class AuctionUserDAOimpl extends BaseDAO implements IAuctionDAO {
private static final Log log = LogFactory.getLog(AuctionUserDAOimpl.class);
protected void initDao() {
// do nothing
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#save(com.hpsvse.entity.AuctionUser)
*/
public void save(AuctionUser transientInstance) {
log.debug("saving AuctionUser instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#delete(com.hpsvse.entity.AuctionUser)
*/
public void delete(AuctionUser persistentInstance) {
log.debug("deleting AuctionUser instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findById(java.lang.Integer)
*/
public AuctionUser findById(java.lang.Integer id) {
log.debug("getting AuctionUser instance with id: " + id);
try {
AuctionUser instance = (AuctionUser) getHibernateTemplate().get(
"com.hpsvse.entity.AuctionUser", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findByExample(com.hpsvse.entity.AuctionUser)
*/
public List findByExample(AuctionUser instance) {
log.debug("finding AuctionUser 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;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findByProperty(java.lang.String, java.lang.Object)
*/
public List findByProperty(String propertyName, Object value) {
log.debug("finding AuctionUser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from AuctionUser as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findByUsername(java.lang.Object)
*/
public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findByUserpass(java.lang.Object)
*/
public List findByUserpass(Object userpass) {
return findByProperty(USERPASS, userpass);
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findByEmail(java.lang.Object)
*/
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#findAll()
*/
public List findAll() {
log.debug("finding all AuctionUser instances");
try {
String queryString = "from AuctionUser";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#merge(com.hpsvse.entity.AuctionUser)
*/
public AuctionUser merge(AuctionUser detachedInstance) {
log.debug("merging AuctionUser instance");
try {
AuctionUser result = (AuctionUser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#attachDirty(com.hpsvse.entity.AuctionUser)
*/
public void attachDirty(AuctionUser instance) {
log.debug("attaching dirty AuctionUser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IAuctionDAO#attachClean(com.hpsvse.entity.AuctionUser)
*/
public void attachClean(AuctionUser instance) {
log.debug("attaching clean AuctionUser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static IAuctionDAO getFromApplicationContext(
ApplicationContext ctx) {
return (IAuctionDAO) ctx.getBean("AuctionUserDAO");
}
此代码是没有继承BaseDAO的类,而是继承HibernateDaoSupport的接口,
public class BidDAOimpl extends HibernateDaoSupport implements IBidDAO {
private static final Log log = LogFactory.getLog(BidDAOimpl.class);
protected void initDao() {
// do nothing
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#save(com.hpsvse.entity.Bid)
*/
public void save(Bid transientInstance) {
log.debug("saving Bid instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#delete(com.hpsvse.entity.Bid)
*/
public void delete(Bid persistentInstance) {
log.debug("deleting Bid instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#findById(java.lang.Integer)
*/
public Bid findById(java.lang.Integer id) {
log.debug("getting Bid instance with id: " + id);
try {
Bid instance = (Bid) getHibernateTemplate().get(
"com.hpsvse.entity.Bid", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#findByExample(com.hpsvse.entity.Bid)
*/
public List findByExample(Bid instance) {
log.debug("finding Bid 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;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#findByProperty(java.lang.String, java.lang.Object)
*/
public List findByProperty(String propertyName, Object value) {
log.debug("finding Bid instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Bid as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#findByBidPrice(java.lang.Object)
*/
public List findByBidPrice(Object bidPrice) {
return findByProperty(BID_PRICE, bidPrice);
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#findAll()
*/
public List findAll() {
log.debug("finding all Bid instances");
try {
String queryString = "from Bid";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#merge(com.hpsvse.entity.Bid)
*/
public Bid merge(Bid detachedInstance) {
log.debug("merging Bid instance");
try {
Bid result = (Bid) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#attachDirty(com.hpsvse.entity.Bid)
*/
public void attachDirty(Bid instance) {
log.debug("attaching dirty Bid instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.hpsvse.entity.IBidDAO#attachClean(com.hpsvse.entity.Bid)
*/
public void attachClean(Bid instance) {
log.debug("attaching clean Bid instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static IBidDAO getFromApplicationContext(ApplicationContext ctx) {
return (IBidDAO) ctx.getBean("BidDAO");
}
两个DAO的实现类相比较,前者是继承BaseDAO接口,而后者是继承HibernateDaoSupport的接口,
两端代码都是完成相同的功能,前者的耦合性较低,而后者的耦合性较高,如果项目的需求发生更改,后者修改代码较为麻烦,而前者较为方便,