1 package dao.impl; 2 3 4 5 import java.util.List; 6 7 import org.hibernate.Query; 8 import org.hibernate.Session; 9 import org.hibernate.Transaction; 10 import org.hibernate.criterion.Example; 11 /** 12 * 13 * 操作数据库底层类 14 * 15 */ 16 public abstract class BaseHibernateDAO { 17 private Session session; 18 private boolean isClose = false;//是否是调用者关闭Session 19 /** 20 * 21 * @param cla 22 * @param id 23 * @return 24 */ 25 protected Object getObject(Class cla,java.io.Serializable id){ 26 Object object = null; 27 session = this.getSession(); 28 try{ 29 object = session.get(cla, id); 30 }catch(Exception ex){ 31 ex.printStackTrace(); 32 }finally{ 33 if(!isClose){ 34 this.closeSession(); 35 } 36 } 37 return object; 38 } 39 40 /** 41 * 42 * @param cla 43 * @param id 44 * @return 45 */ 46 protected Object loadObject(Class cla,java.io.Serializable id){ 47 Object object = null; 48 session = this.getSession(); 49 try{ 50 object = session.load(cla, id); 51 }catch(Exception ex){ 52 ex.printStackTrace(); 53 }finally{ 54 if(!isClose){ 55 this.closeSession(); 56 } 57 } 58 return object; 59 } 60 61 /** 62 * 添加数据 63 */ 64 protected boolean add(Object object){ 65 Transaction tx = null; 66 session = this.getSession(); 67 try{ 68 tx = session.beginTransaction();//开启事务 69 session.save(object); 70 tx.commit();//提交事务 71 }catch(Exception ex){ 72 if(tx!=null)tx.rollback();//回滚 73 ex.printStackTrace(); 74 return false; 75 }finally{ 76 if(!isClose) 77 this.closeSession(); 78 } 79 return true; 80 } 81 /** 82 * 修改数据 83 */ 84 protected boolean update(Object object){ 85 Transaction tx = null; 86 session = this.getSession(); 87 try{ 88 tx = session.beginTransaction();//开启事务 89 session.update(object); 90 tx.commit();//提交事务 91 }catch(Exception ex){ 92 if(tx!=null)tx.rollback();//回滚 93 ex.printStackTrace(); 94 return false; 95 }finally{ 96 if(!isClose) 97 this.closeSession(); 98 } 99 return true; 100 } 101 /** 102 * 删除数据 103 */ 104 protected boolean delete(Class cls,java.io.Serializable id){ 105 Transaction tx = null; 106 Object object = this.getObject(cls, id); 107 session = this.getSession(); 108 try{ 109 tx = session.beginTransaction();//开启事务 110 session.delete(object);//获得对象并作删除 111 tx.commit();//提交事务 112 }catch(Exception ex){ 113 if(tx!=null)tx.rollback();//回滚 114 ex.printStackTrace(); 115 return false; 116 }finally{ 117 if(!isClose) 118 this.closeSession(); 119 } 120 return true; 121 } 122 /** 123 * 删除数据 124 */ 125 protected boolean deleteAll(String sql){ 126 Transaction tx = null; 127 session = this.getSession(); 128 try{ 129 tx = session.beginTransaction();//开启事务 130 Query query = session.createQuery(sql.toString()); 131 132 int i = query.executeUpdate(); 133 System.out.println("======="+i); 134 //获得对象并作删除 135 tx.commit();//提交事务 136 }catch(Exception ex){ 137 if(tx!=null)tx.rollback();//回滚 138 ex.printStackTrace(); 139 return false; 140 }finally{ 141 if(!isClose) 142 this.closeSession(); 143 } 144 return true; 145 } 146 147 /** 148 * 根据条件进行高级查询 149 */ 150 protected List search(Class clazz,Object condition){ 151 List list = null; 152 try { 153 List results = this.getSession().createCriteria(clazz).add(Example.create(condition)).list(); 154 return results; 155 } catch (Exception e) { 156 return list; 157 }finally{ 158 if(!isClose) 159 this.closeSession(); 160 } 161 } 162 163 /** 164 * 根据SQL查询 165 */ 166 protected List selectBySql(String sql){ 167 List list = null; 168 try{ 169 list = this.getSession().createSQLQuery(sql).list(); 170 return list; 171 }catch(Exception ex){ 172 ex.printStackTrace(); 173 return list; 174 }finally{ 175 if(!isClose) 176 this.closeSession(); 177 } 178 } 179 180 /** 181 * 根据HQL查询 182 */ 183 protected List selectByHql(String Hql){ 184 List list = null; 185 session = this.getSession(); 186 try{ 187 list = session.createQuery(Hql).list(); 188 return list; 189 }catch(Exception ex){ 190 ex.printStackTrace(); 191 return list; 192 }finally{ 193 if(!isClose) 194 this.closeSession(); 195 } 196 } 197 198 199 200 /** 201 * 202 * @return 203 */ 204 protected Session getSession() { 205 206 if(this.session==null){ 207 this.session = HibernateSessionFactory.getSession(); 208 System.out.println("-----开启Session-----"); 209 } 210 return this.session; 211 } 212 public void setSession(Session session) { 213 this.session = session; 214 } 215 216 public void closeSession(){ 217 218 this.session = null; 219 HibernateSessionFactory.closeSession(); 220 System.out.println("-----关闭Session-----"); 221 222 } 223 public void setClose(boolean isClose) { 224 this.isClose = isClose; 225 } 226 227 }