HibernateTemplate是Spring框架下的一个数据库操作类org.springframework.orm.hibernate3.HibernateTemplate
HibernateDaoSupport也是Spring框架下的一个接口类
Hibernate Dao支持
org.springframework.orm.hibernate3.support.HibernateDaoSupport
HibernateTemplate常用方法
void delete(Object entity):删除指定持久化实例
deleteAll(Collection entities):删除集合内全部持久化类实例
find(String queryString):根据HQL查询字符串来返回实例集合
find(String hql, Object value);跟句一个条件查询返回实例集合
find(String hql, Object []values);根据多个条件查询返回实例集合
get(Class entityClass, Serializable id):根据主键加载特定持久化类的实例
save(Object entity):保存新的实例
saveOrUpdate(Object entity):根据实例状态,选择保存或者更新
update(Object entity):更新实例的状态,要求entity是持久状态
编程实例
- String name = Global.nvlToString(busAppoinInfo.getBusAppoinName());// 姓名
- String cardNber = Global.nvlToString(busAppoinInfo.getBusAppoinMemberNumber());// 会员账号
- StringBuffer hql = new StringBuffer();//hql语句
- List<Object> param = new ArrayList<Object>();
- try {
- //查询语句
- hql.append("from TripBusAppoinInfo busAppoin where (1=1)");
- //设置查询条件
- if(!name.equals("")){
- hql.append("and lower(busAppoin.busAppoinName) like lower(?) ");
- param.add("%"+name+"%");
- }
- if(!cardNber.equals("")){
- hql.append("and busAppoin.busAppoinMemberNumber = ? ");
- param.add(cardNber);
- }
- hql.append(" Order by busAppoin.busAppoinTime");//排序条件
- Object []values = param.toArray(new Object[param.size()]);
- HibernateTemplate template = this.getHibernateTemplate();
- //执行hql查询
- List<TripBusAppoinInfo> busAppoinList = this.getHibernateTemplate().find(hql.toString(), values);
- return busAppoinList;
- } catch (RuntimeException re) {
- SysLog.log(SysLog.ERROR, re);
- throw new DataAccessException();
- }