首先给出基本Dao层代码:
GenericDao.java
1 package com.agen.dao; 2 3 import java.io.Serializable; 4 import java.util.Collection; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.hibernate.Criteria; 9 import org.hibernate.criterion.Criterion; 10 import org.hibernate.criterion.DetachedCriteria; 11 12 import com.github.pagehelper.PageInfo; 13 14 15 public interface GenericDao<T, PK extends Serializable> { 16 /** 17 * 查询全部,可以排序 18 * @param orderBy 19 * @param isAsc 20 * @return List<T> 21 */ 22 public List<T> list(Criteria criteria); 23 24 /** 25 * 查询全部,可以排序 26 * @param orderBy 27 * @param isAsc 28 * @return List<T> 29 */ 30 public List<T> list(String orderBy, boolean isAsc); 31 32 /** 33 * 离线查询 34 * @param criteria 35 * @return List<T> 36 */ 37 public List<T> list(DetachedCriteria criteria); 38 39 /** 40 * 根据Criteria查询条件,获取总数 41 * @param criteria 42 * @return int 43 * @throws SecurityException 44 * @throws NoSuchFieldException 45 * @throws IllegalAccessException 46 * @throws IllegalArgumentException 47 */ 48 public int countAll(Criteria criteria); 49 50 /** 51 * 获取总数(默认为entityClass) 即查询总条数 52 * @return int 53 */ 54 public int countAll(); 55 56 /** 57 * 根据I判断是否存在 58 * @param id 59 * @return boolean 60 */ 61 public boolean exists(PK id); 62 63 /** 64 * 保存实体 65 * @param t 实体参数 66 */ 67 public void save(T t); 68 69 /** 70 * 保存或者更新实体 71 * @param t 实体 72 */ 73 public void saveOrUpdate(T t); 74 75 /** 76 * 加载实体的通过load方法 77 * @param id 实体的id 78 * @return 查询出来的实体 79 */ 80 public T load(PK id); 81 82 /** 83 * 合并实体 84 * @param entity 85 */ 86 public void merge(T entity); 87 88 /** 89 * 查找全部 90 */ 91 public List<T> findAll(); 92 93 /** 94 * 通过get方法加载实体的 95 * @param id 实体的id 96 * @return 查询出来的实体 97 */ 98 public T get(PK id); 99 100 /** 101 * contains 102 * @param t 实体 103 * @return 是否包含 104 */ 105 public boolean contains(T t); 106 107 /** 108 * delete 109 * @param t 110 * 删除实体 111 */ 112 public void delete(T t); 113 114 /** 115 * 根据ID删除数据 116 * @param Id 实体id 117 * @return 是否删除成功 118 */ 119 public boolean deleteById(PK Id); 120 121 /** 122 * 删除所有 123 * @param entities 实体的Collection集合 124 */ 125 public void deleteAll(Collection<T> entities); 126 127 /** 128 * 执行Hql语句 要求 hql中参数顺序与可变参数 中参数顺序相一致 129 * @param hqlString hql 130 * @param values 不定参数数组 131 */ 132 public void queryHql(String hqlString, Object... values); 133 134 /** 135 * 执行Sql语句(不建议用,影响扩展) 136 * @param sqlString sql 137 * @param values 不定参数数组 138 */ 139 public void querySql(String sqlString, Object... values); 140 141 /** 142 * 根据HQL语句查找唯一实体 143 * 144 * @param hqlString HQL语句 145 * @param values 不定参数的Object数组 146 * @return 查询实体 147 */ 148 public T getByHQL(String hqlString, Object... values); 149 150 /** 151 * 根据SQL语句查找唯一实体(不建议用,影响扩展) 152 * @param sqlString SQL语句 153 * @param values 不定参数的Object数组 154 * @return 查询实体 155 */ 156 157 /** 158 * 根据HQL语句,得到对应的list 159 * @param hqlString HQL语句 160 * @param values 不定参数的Object数组 161 * @return 查询多个实体的List集合 162 */ 163 public List<T> getListByHQL(String hqlString, Object... values); 164 165 /** 166 * 根据SQL语句,得到对应的list(不建议用,影响扩展) 167 * @param sqlString HQL语句 168 * @param values 不定参数的Object数组 169 * @return 查询多个实体的List集合 170 */ 171 public List<T> getListBySQL(String sqlString, Object... values); 172 173 /** 174 * refresh 刷新实体,强制与数据库两步 refresh方法应该是数据库的数据更新到本地的person实体中,而不是本地person更新数据到数据库中 也就是执行refresh方法是更新了java代码中变量的数据值 175 * @param t 实体 176 */ 177 public void refresh(T t); 178 179 /** 180 * update 181 * @param t 182 * 更新的是数据库中的数据 183 */ 184 public void update(T t); 185 186 /** 187 * 根据HQL得到记录数 188 * @param hql HQL语句 189 * @param values 不定参数的Object数组 190 * @return 记录总数 191 */ 192 public Long countByHql(String hql, Object... values); 193 194 /** 195 * HQL分页查询 196 * 197 * @param hql HQL语句 198 * @param countHql 查询记录条数的HQL语句 199 * @param pageNo 下一页 200 * @param pageSize 一页总条数 201 * @param values 不定Object数组参数 202 * @return PageResults的封装类,里面包含了页码的信息以及查询的数据List集合 203 */ 204 public PageInfo<T> findPageByHql(String hql, String countHql, int pageNo, int pageSize, Object... values); 205 206 /** 207 * 按属性查找对象列表,匹配方式为相等 208 * @param propertyName 209 * @param value 210 * @return List<T> 211 */ 212 public List<T> list(String propertyName, Object value); 213 214 /** 215 * 根据criterion查询条件获取数据列表 216 * @param criterion 217 * @return List<T> 218 */ 219 public List<T> list(Criterion criterion); 220 221 /** 222 * 按Criteria查询对象列表 223 * @param criterions 224 * @return List<T> 225 */ 226 public List<T> list(Criterion... criterions); 227 228 /** 229 * 按属性查找唯一对象,匹配方式为相等 230 * @param propertyName 231 * @param value 232 * @return T 233 */ 234 public T uniqueResult(String propertyName, Object value); 235 236 /** 237 * 按Criteria查询唯一对象 238 * @param criterions 239 * @return T 240 */ 241 public T uniqueResult(Criterion... criterions); 242 243 /** 244 * 按Criteria查询唯一对象 245 * @param criteria 246 * @return T 247 */ 248 public T uniqueResult(Criteria criteria); 249 250 /** 251 * 按criteria查询某个Integer类型的字段 252 * @param criteria 253 * @return 254 */ 255 public Integer uniqueResultInt(Criteria criteria); 256 257 /** 258 * 为Criteria添加distinct transformer 259 * @param criteria 260 * @return Criteria 261 */ 262 public Criteria distinct(Criteria criteria); 263 264 /** 265 * 刷新session 266 */ 267 public void flush(); 268 269 /** 270 * 清空session 271 */ 272 public void clear(); 273 274 /** 275 * 创建Criteria实例 276 */ 277 public Criteria createCriteria(); 278 279 /** 280 * 根据Criterion条件创建Criteria 281 * @param criterions 282 * @return Criteria 283 */ 284 public Criteria createCriteria(Criterion... criterions); 285 286 /** 287 * 分页查询Criteria 288 * @param criteria 289 * @param pageNo 下页页码 290 * @param pageSize 页面数据量 291 * @return List<T> 292 */ 293 public List<T> findPage(Criteria criteria, int pageNo, int pageSize); 294 295 /** 296 * 分页查询Criteria 297 * @param criteria 298 * @param pageNo 299 * @param pageSize 300 * @return PageInfo<T> 301 * @throws SecurityException 302 * @throws NoSuchFieldException 303 * @throws IllegalAccessException 304 * @throws IllegalArgumentException 305 */ 306 public PageInfo<T> findQuery(Criteria criteria, int pageNo, int pageSize); 307 308 /** 309 * 310 * @param hql 311 * @param pageNo 312 * @param pageSize 313 * @param map 314 * @return List<T> 315 */ 316 public List<T> findQuery(String hql, int pageNo, int pageSize, Map<?, ?> map); 317 }
GenericDaoImpl.java
1 package com.agen.dao.impl; 2 3 import java.io.Serializable; 4 import java.lang.reflect.Field; 5 import java.util.ArrayList; 6 import java.util.Collection; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Map.Entry; 10 11 import org.hibernate.Criteria; 12 import org.hibernate.Query; 13 import org.hibernate.QueryException; 14 import org.hibernate.ScrollableResults; 15 import org.hibernate.Session; 16 import org.hibernate.SessionFactory; 17 import org.hibernate.criterion.CriteriaSpecification; 18 import org.hibernate.criterion.Criterion; 19 import org.hibernate.criterion.DetachedCriteria; 20 import org.hibernate.criterion.Order; 21 import org.hibernate.criterion.Projections; 22 import org.hibernate.criterion.Restrictions; 23 import org.hibernate.internal.CriteriaImpl; 24 import org.hibernate.internal.CriteriaImpl.Subcriteria; 25 import org.springframework.beans.factory.annotation.Autowired; 26 import org.springframework.transaction.annotation.Transactional; 27 import org.springframework.util.Assert; 28 29 import java.lang.reflect.ParameterizedType; 30 31 import com.agen.dao.GenericDao; 32 import com.github.pagehelper.PageInfo; 33 34 public class GenericDaoImpl<T, PK extends Serializable> implements GenericDao<T, PK> { 35 /** 36 * 不建议直接使用 37 */ 38 @Autowired 39 private SessionFactory sessionFactory; 40 41 public Session getSession() { 42 // 需要开启事物,才能得到CurrentSession 43 return sessionFactory.getCurrentSession(); 44 } 45 46 47 protected Class<T> entityClass; 48 49 protected Class getEntityClass() { 50 if (entityClass == null) { 51 if(((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments().length > 0) { 52 entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 53 } 54 } 55 return entityClass; 56 } 57 58 public SessionFactory getSessionFactory() { 59 return sessionFactory; 60 } 61 62 public void setSessionFactory(SessionFactory sessionFactory) { 63 this.sessionFactory = sessionFactory; 64 } 65 66 @Override 67 public Criteria createCriteria() { 68 return getSession().createCriteria(getEntityClass()); 69 } 70 71 72 @Override 73 public void save(T t) { 74 this.getSession().save(t); 75 } 76 77 @Override 78 public void saveOrUpdate(T t) { 79 Assert.notNull(t); 80 this.getSession().saveOrUpdate(t); 81 } 82 83 @Override 84 public T load(PK id) { 85 Assert.notNull(id); 86 T load = (T) this.getSession().load(getEntityClass(), id); 87 return load; 88 } 89 90 @Override 91 public T get(PK id) { 92 Assert.notNull(id); 93 T load = (T) this.getSession().get(getEntityClass(), id); 94 return load; 95 } 96 97 @Override 98 public boolean contains(T t) { 99 Assert.notNull(t); 100 return this.getSession().contains(t); 101 } 102 103 @Override 104 public void delete(T t) { 105 Assert.notNull(t); 106 this.getSession().delete(t); 107 } 108 109 @Override 110 public boolean deleteById(PK Id) { 111 Assert.notNull(Id); 112 T t = get(Id); 113 if (t == null) return false; 114 delete(t); 115 return true; 116 } 117 118 @Override 119 public void deleteAll(Collection<T> entities) { 120 Assert.notNull(entities); 121 for (Object entity : entities) { 122 this.getSession().delete(entity); 123 } 124 } 125 126 @Override 127 public void queryHql(String hqlString, Object... values) { 128 Query query = this.getSession().createQuery(hqlString); 129 if (values != null) { 130 for (int i = 0; i < values.length; i++) { 131 query.setParameter(i, values[i]); 132 } 133 } 134 query.executeUpdate(); 135 } 136 137 /** 138 * 根据hql 和 map集合中的数据 进行相对应的 insert update delete操作 139 * @param hqlString 140 * @param paras 141 */ 142 public void queryHql(String hqlString, Map<String, Object> paras) { 143 Query query = this.getSession().createQuery(hqlString); 144 if (paras != null) { 145 for (Entry<String, Object> en : paras.entrySet()) { 146 query.setParameter(en.getKey(), en.getValue()); 147 } 148 } 149 query.executeUpdate(); 150 } 151 152 @Override 153 public void querySql(String sqlString, Object... values) { 154 Query query = this.getSession().createSQLQuery(sqlString); 155 if (values != null) { 156 for (int i = 0; i < values.length; i++) { 157 query.setParameter(i, values[i]); 158 } 159 } 160 query.executeUpdate(); 161 } 162 163 @Override 164 public T getByHQL(String hqlString, Object... values) { 165 Query query = this.getSession().createQuery(hqlString); 166 if (values != null) { 167 for (int i = 0; i < values.length; i++) { 168 query.setParameter(i, values[i]); 169 } 170 } 171 return (T) query.uniqueResult(); 172 } 173 174 @Override 175 public List<T> getListByHQL(String hqlString, Object... values) { 176 Query query = this.getSession().createQuery(hqlString); 177 if (values != null) { 178 for (int i = 0; i < values.length; i++) { 179 query.setParameter(i, values[i]); 180 } 181 } 182 return query.list(); 183 } 184 185 @Override 186 public List<T> getListBySQL(String sqlString, Object... values) { 187 Query query = this.getSession().createSQLQuery(sqlString); 188 if (values != null) { 189 for (int i = 0; i < values.length; i++) { 190 query.setParameter(i, values[i]); 191 } 192 } 193 return query.list(); 194 } 195 196 @Override 197 public void refresh(T t) { 198 this.getSession().refresh(t); 199 } 200 201 @Override 202 public void update(T t) { 203 this.getSession().update(t); 204 } 205 206 @Override 207 public Long countByHql(String hql, Object... values) { 208 Query query = this.getSession().createQuery(hql); 209 if (values != null) { 210 for (int i = 0; i < values.length; i++) { 211 query.setParameter(i, values[i]); 212 } 213 } 214 return (Long) query.uniqueResult(); 215 } 216 217 @Override 218 public PageInfo<T> findPageByHql(String hql, String countHql, int pageNo, int pageSize, Object... values) { 219 PageInfo<T> retValue = new PageInfo<T>(); 220 Query query = this.getSession().createQuery(hql); 221 if (values != null) { 222 for (int i = 0; i < values.length; i++) { 223 query.setParameter(i, values[i]); 224 } 225 } 226 int currentPage = pageNo > 1 ? pageNo : 1; 227 retValue.setPages(currentPage); 228 retValue.setPageSize(pageSize); 229 if (countHql == null) { 230 ScrollableResults results = query.scroll(); 231 results.last(); 232 retValue.setTotal(results.getRowNumber() + 1); 233 } else { 234 Long count = countByHql(countHql, values); 235 retValue.setTotal(count.intValue()); 236 } 237 List<T> itemList = query.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list(); 238 if (itemList == null) { 239 itemList = new ArrayList<T>(); 240 } 241 retValue.setList(itemList); 242 return retValue; 243 } 244 245 246 @Override 247 public void merge(T entity) { 248 // TODO Auto-generated method stub 249 getSession().merge(entity); 250 } 251 252 @Override 253 public boolean exists(PK id) { 254 return null != get(id); 255 } 256 257 @Override 258 public int countAll() { 259 Criteria criteria = createCriteria(); 260 return Integer.valueOf(criteria.setProjection(Projections.rowCount()).uniqueResult().toString()); 261 } 262 263 @Override 264 public int countAll(Criteria criteria) { 265 criteria.setProjection(null); 266 return Integer.valueOf(criteria.setProjection(Projections.rowCount()).uniqueResult().toString()); 267 } 268 269 @Override 270 public List<T> list(Criteria criteria) { 271 return criteria.list(); 272 } 273 274 @Override 275 public List<T> list(DetachedCriteria criteria) { 276 return (List<T>) list(criteria.getExecutableCriteria(getSession())); 277 } 278 279 @Override 280 public List<T> list(String orderBy, boolean isAsc) { 281 Criteria criteria = createCriteria(); 282 if (isAsc) { 283 criteria.addOrder(Order.asc(orderBy)); 284 } else { 285 criteria.addOrder(Order.desc(orderBy)); 286 } 287 return criteria.list(); 288 } 289 290 @Override 291 public List<T> list(String propertyName, Object value) { 292 Criterion criterion = Restrictions.like(propertyName, "%"+ value +"%"); 293 return list(criterion); 294 } 295 296 297 @Override 298 public List<T> list(Criterion criterion) { 299 Criteria criteria = createCriteria(); 300 criteria.add(criterion); 301 return criteria.list(); 302 } 303 304 @Override 305 public List<T> list(Criterion... criterions) { 306 return createCriteria(criterions).list(); 307 } 308 309 @Override 310 public T uniqueResult(String propertyName, Object value) { 311 Criterion criterion = Restrictions.eq(propertyName, value); 312 return (T) createCriteria(criterion).uniqueResult(); 313 } 314 315 @Override 316 public T uniqueResult(Criterion... criterions) { 317 Criteria criteria = createCriteria(criterions); 318 return uniqueResult(criteria); 319 } 320 321 @Override 322 public T uniqueResult(Criteria criteria) { 323 return (T) criteria.uniqueResult(); 324 } 325 326 @Override 327 public Integer uniqueResultInt(Criteria criteria) { 328 return (Integer) criteria.uniqueResult(); 329 } 330 331 332 @Override 333 public Criteria distinct(Criteria criteria) { 334 criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); 335 return criteria; 336 } 337 338 @Override 339 public void flush() { 340 getSession().flush(); 341 } 342 343 @Override 344 public void clear() { 345 getSession().clear(); 346 } 347 348 349 @Override 350 public Criteria createCriteria(Criterion... criterions) { 351 Criteria criteria = createCriteria(); 352 for (Criterion c : criterions) { 353 criteria.add(c); 354 } 355 return criteria; 356 } 357 358 @Override 359 public List<T> findPage(Criteria criteria, int pageNo, int pageSize) { 360 criteria.setFirstResult((pageNo - 1) * pageSize); 361 criteria.setMaxResults(pageSize); 362 return list(criteria); 363 } 364 365 @Override 366 public PageInfo<T> findQuery(Criteria criteria, int pageNo, int pageSize) { 367 try { 368 Assert.isTrue(pageNo >= 1, "pageNO should start from 1"); 369 while(criteria instanceof Subcriteria){ 370 criteria = ((Subcriteria)criteria).getParent(); 371 } 372 //拆分order by子句 373 while(criteria instanceof Subcriteria){ 374 criteria = ((Subcriteria)criteria).getParent(); 375 } 376 Field field = CriteriaImpl.class.getDeclaredField("orderEntries"); 377 field.setAccessible(true); 378 List<?> orderEntrys = (List<?>) field.get(criteria); 379 field.set(criteria, new ArrayList()); 380 //统计总数 381 long totalCount = countAll(criteria); 382 criteria.setProjection(null); 383 //统计完了再把order by子句加上 这样保证了sql语句不会出错 384 field.set(criteria, orderEntrys); 385 List<T> list = findPage(criteria, pageNo, pageSize); 386 if (totalCount < 1) { 387 return new PageInfo<T>(); 388 } 389 PageInfo<T> page = new PageInfo<T>(); 390 page.setPageNum(pageNo); 391 page.setTotal(totalCount); 392 page.setPages((int) (totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1)); 393 page.setPageSize(pageSize); 394 page.setList(list); 395 return page; 396 } catch (Exception e) { 397 // TODO: handle exception 398 e.printStackTrace(); 399 throw new QueryException("查询出错!"); 400 } 401 402 } 403 404 @Override 405 public List<T> findQuery(String hql, int pageNo, int pageSize, Map<?, ?> map) { 406 // TODO Auto-generated method stub 407 if(null == hql) return null; 408 Query query = getSession().createQuery(hql); 409 for (Entry<?, ?> en : map.entrySet()) { 410 query.setParameter(en.getKey().toString(), en.getValue()); 411 } 412 if(pageNo > 0) query.setFirstResult(pageNo); 413 if(pageSize > 0) query.setFirstResult(pageSize); 414 return query.list(); 415 } 416 417 @Override 418 public List<T> findAll() { 419 // TODO Auto-generated method stub 420 return createCriteria().list(); 421 } 422 }
GenericService.java
1 package com.agen.service; 2 3 import java.io.Serializable; 4 import java.util.Collection; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.hibernate.Criteria; 9 import org.hibernate.criterion.Criterion; 10 import org.hibernate.criterion.DetachedCriteria; 11 12 import com.github.pagehelper.PageInfo; 13 14 public interface GenericService<T, PK extends Serializable> { 15 /** 16 * 查询全部,可以排序 17 * @param orderBy 18 * @param isAsc 19 * @return List<T> 20 */ 21 public List<T> list(Criteria criteria); 22 23 /** 24 * 查询全部,可以排序 25 * @param orderBy 26 * @param isAsc 27 * @return List<T> 28 */ 29 public List<T> list(String orderBy, boolean isAsc); 30 31 /** 32 * 离线查询 33 * @param criteria 34 * @return List<T> 35 */ 36 public List<T> list(DetachedCriteria criteria); 37 38 /** 39 * 根据Criteria查询条件,获取总数 40 * @param criteria 41 * @return int 42 * @throws SecurityException 43 * @throws NoSuchFieldException 44 * @throws IllegalAccessException 45 * @throws IllegalArgumentException 46 */ 47 public int countAll(Criteria criteria); 48 49 /** 50 * 获取总数(默认为entityClass) 即查询总条数 51 * @return int 52 */ 53 public int countAll(); 54 55 /** 56 * 根据I判断是否存在 57 * @param id 58 * @return boolean 59 */ 60 public boolean exists(PK id); 61 62 /** 63 * 保存实体 64 * @param t 实体参数 65 */ 66 public void save(T t); 67 68 /** 69 * 保存或者更新实体 70 * @param t 实体 71 */ 72 public void saveOrUpdate(T t); 73 74 /** 75 * 加载实体的通过load方法 76 * @param id 实体的id 77 * @return 查询出来的实体 78 */ 79 public T load(PK id); 80 81 /** 82 * 合并实体 83 * @param entity 84 */ 85 public void merge(T entity); 86 87 /** 88 * 查找全部 89 */ 90 public List<T> findAll(); 91 92 /** 93 * 通过get方法加载实体的 94 * @param id 实体的id 95 * @return 查询出来的实体 96 */ 97 public T get(PK id); 98 99 /** 100 * contains 101 * @param t 实体 102 * @return 是否包含 103 */ 104 public boolean contains(T t); 105 106 /** 107 * delete 108 * @param t 109 * 删除实体 110 */ 111 public void delete(T t); 112 113 /** 114 * 根据ID删除数据 115 * @param Id 实体id 116 * @return 是否删除成功 117 */ 118 public boolean deleteById(PK Id); 119 120 /** 121 * 删除所有 122 * @param entities 实体的Collection集合 123 */ 124 public void deleteAll(Collection<T> entities); 125 126 /** 127 * 执行Hql语句 要求 hql中参数顺序与可变参数 中参数顺序相一致 128 * @param hqlString hql 129 * @param values 不定参数数组 130 */ 131 public void queryHql(String hqlString, Object... values); 132 133 /** 134 * 执行Sql语句(不建议用,影响扩展) 135 * @param sqlString sql 136 * @param values 不定参数数组 137 */ 138 public void querySql(String sqlString, Object... values); 139 140 /** 141 * 根据HQL语句查找唯一实体 142 * 143 * @param hqlString HQL语句 144 * @param values 不定参数的Object数组 145 * @return 查询实体 146 */ 147 public T getByHQL(String hqlString, Object... values); 148 149 /** 150 * 根据SQL语句查找唯一实体(不建议用,影响扩展) 151 * @param sqlString SQL语句 152 * @param values 不定参数的Object数组 153 * @return 查询实体 154 */ 155 156 /** 157 * 根据HQL语句,得到对应的list 158 * @param hqlString HQL语句 159 * @param values 不定参数的Object数组 160 * @return 查询多个实体的List集合 161 */ 162 public List<T> getListByHQL(String hqlString, Object... values); 163 164 /** 165 * 根据SQL语句,得到对应的list(不建议用,影响扩展) 166 * @param sqlString HQL语句 167 * @param values 不定参数的Object数组 168 * @return 查询多个实体的List集合 169 */ 170 public List<T> getListBySQL(String sqlString, Object... values); 171 172 /** 173 * refresh 刷新实体,强制与数据库两步 refresh方法应该是数据库的数据更新到本地的person实体中,而不是本地person更新数据到数据库中 也就是执行refresh方法是更新了java代码中变量的数据值 174 * @param t 实体 175 */ 176 public void refresh(T t); 177 178 /** 179 * update 180 * @param t 181 * 更新的是数据库中的数据 182 */ 183 public void update(T t); 184 185 /** 186 * 根据HQL得到记录数 187 * @param hql HQL语句 188 * @param values 不定参数的Object数组 189 * @return 记录总数 190 */ 191 public Long countByHql(String hql, Object... values); 192 193 /** 194 * HQL分页查询 195 * 196 * @param hql HQL语句 197 * @param countHql 查询记录条数的HQL语句 198 * @param pageNo 下一页 199 * @param pageSize 一页总条数 200 * @param values 不定Object数组参数 201 * @return PageResults的封装类,里面包含了页码的信息以及查询的数据List集合 202 */ 203 public PageInfo<T> findPageByHql(String hql, String countHql, int pageNo, int pageSize, Object... values); 204 205 /** 206 * 按属性查找对象列表,匹配方式为相等 207 * @param propertyName 208 * @param value 209 * @return List<T> 210 */ 211 public List<T> list(String propertyName, Object value); 212 213 /** 214 * 根据criterion查询条件获取数据列表 215 * @param criterion 216 * @return List<T> 217 */ 218 public List<T> list(Criterion criterion); 219 220 /** 221 * 按Criteria查询对象列表 222 * @param criterions 223 * @return List<T> 224 */ 225 public List<T> list(Criterion... criterions); 226 227 /** 228 * 按属性查找唯一对象,匹配方式为相等 229 * @param propertyName 230 * @param value 231 * @return T 232 */ 233 public T uniqueResult(String propertyName, Object value); 234 235 /** 236 * 按Criteria查询唯一对象 237 * @param criterions 238 * @return T 239 */ 240 public T uniqueResult(Criterion... criterions); 241 242 /** 243 * 按Criteria查询唯一对象 244 * @param criteria 245 * @return T 246 */ 247 public T uniqueResult(Criteria criteria); 248 /** 249 * 按照criteria返回类型为Integer类型的某个字段的值 250 * @param criteria 251 * @return 252 */ 253 public Integer uniqueResultInt(Criteria criteria); 254 255 256 257 /** 258 * 为Criteria添加distinct transformer 259 * @param criteria 260 * @return Criteria 261 */ 262 public Criteria distinct(Criteria criteria); 263 264 /** 265 * 刷新session 266 */ 267 public void flush(); 268 269 /** 270 * 清空session 271 */ 272 public void clear(); 273 274 /** 275 * 创建Criteria实例 276 */ 277 public Criteria createCriteria(); 278 279 /** 280 * 根据Criterion条件创建Criteria 281 * @param criterions 282 * @return Criteria 283 */ 284 public Criteria createCriteria(Criterion... criterions); 285 286 /** 287 * 分页查询Criteria 288 * @param criteria 289 * @param pageNo 下页页码 290 * @param pageSize 页面数据量 291 * @return List<T> 292 */ 293 public List<T> findPage(Criteria criteria, int pageNo, int pageSize); 294 295 /** 296 * 分页查询Criteria 297 * @param criteria 298 * @param pageNo 299 * @param pageSize 300 * @return PageInfo<T> 301 * @throws SecurityException 302 * @throws NoSuchFieldException 303 * @throws IllegalAccessException 304 * @throws IllegalArgumentException 305 */ 306 public PageInfo<T> findQuery(Criteria criteria, int pageNo, int pageSize); 307 308 /** 309 * 310 * @param hql 311 * @param pageNo 312 * @param pageSize 313 * @param map 314 * @return List<T> 315 */ 316 public List<T> findQuery(String hql, int pageNo, int pageSize, Map<?, ?> map); 317 }
GenericServiceImpl.java
1 package com.agen.service.impl; 2 3 import java.io.Serializable; 4 import java.util.Collection; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.hibernate.Criteria; 9 import org.hibernate.criterion.Criterion; 10 import org.hibernate.criterion.DetachedCriteria; 11 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.stereotype.Component; 13 import org.springframework.stereotype.Service; 14 import org.springframework.transaction.annotation.Propagation; 15 import org.springframework.transaction.annotation.Transactional; 16 17 import com.agen.dao.impl.GenericDaoImpl; 18 import com.agen.service.GenericService; 19 import com.github.pagehelper.PageInfo; 20 21 @Transactional 22 public class GenericServiceImpl<T, PK extends Serializable> implements GenericService<T, PK> { 23 24 25 private GenericDaoImpl<T, PK> dao; 26 /** 27 * 设值注入Dao 28 * @param dao 29 */ 30 @Autowired 31 public void setDao(GenericDaoImpl<T, PK> dao) { 32 this.dao = dao; 33 } 34 35 @Override 36 public List<T> list(Criteria criteria) { 37 // TODO Auto-generated method stub 38 return dao.list(criteria); 39 } 40 41 @Override 42 public List<T> list(String orderBy, boolean isAsc) { 43 // TODO Auto-generated method stub 44 return dao.list(orderBy, isAsc); 45 } 46 47 @Override 48 public List<T> list(DetachedCriteria criteria) { 49 // TODO Auto-generated method stub 50 return dao.list(criteria); 51 } 52 53 @Override 54 public int countAll(Criteria criteria) { 55 // TODO Auto-generated method stub 56 return dao.countAll(criteria); 57 } 58 59 @Override 60 public int countAll() { 61 // TODO Auto-generated method stub 62 return dao.countAll(); 63 } 64 65 @Override 66 public boolean exists(PK id) { 67 // TODO Auto-generated method stub 68 return dao.exists(id); 69 } 70 71 @Override 72 @Transactional(propagation = Propagation.REQUIRED) 73 public void save(T t) { 74 // TODO Auto-generated method stub 75 dao.save(t); 76 } 77 78 @Override 79 @Transactional(propagation = Propagation.REQUIRED) 80 public void saveOrUpdate(T t) { 81 // TODO Auto-generated method stub 82 dao.saveOrUpdate(t); 83 } 84 85 @Override 86 @Transactional(propagation = Propagation.REQUIRED) 87 public T load(PK id) { 88 // TODO Auto-generated method stub 89 return dao.load(id); 90 } 91 92 @Override 93 @Transactional(propagation = Propagation.REQUIRED) 94 public void merge(T entity) { 95 // TODO Auto-generated method stub 96 dao.merge(entity); 97 } 98 99 @Override 100 public List<T> findAll() { 101 // TODO Auto-generated method stub 102 return dao.findAll(); 103 } 104 105 @Override 106 @Transactional(propagation = Propagation.REQUIRED) 107 public T get(PK id) { 108 // TODO Auto-generated method stub 109 return dao.get(id); 110 } 111 112 @Override 113 public boolean contains(T t) { 114 // TODO Auto-generated method stub 115 return dao.contains(t); 116 } 117 118 @Override 119 public void delete(T t) { 120 // TODO Auto-generated method stub 121 dao.delete(t); 122 } 123 124 @Override 125 @Transactional(propagation = Propagation.REQUIRED) 126 public boolean deleteById(PK Id) { 127 // TODO Auto-generated method stub 128 return dao.deleteById(Id); 129 } 130 131 @Override 132 public void deleteAll(Collection<T> entities) { 133 // TODO Auto-generated method stub 134 dao.deleteAll(entities); 135 } 136 137 @Override 138 public void queryHql(String hqlString, Object... values) { 139 // TODO Auto-generated method stub 140 dao.queryHql(hqlString, values); 141 } 142 143 @Override 144 public void querySql(String sqlString, Object... values) { 145 // TODO Auto-generated method stub 146 dao.querySql(sqlString, values); 147 } 148 149 @Override 150 public T getByHQL(String hqlString, Object... values) { 151 // TODO Auto-generated method stub 152 return dao.getByHQL(hqlString, values); 153 } 154 155 @Override 156 public List<T> getListByHQL(String hqlString, Object... values) { 157 // TODO Auto-generated method stub 158 return dao.getListByHQL(hqlString, values); 159 } 160 161 @Override 162 public List<T> getListBySQL(String sqlString, Object... values) { 163 // TODO Auto-generated method stub 164 return dao.getListBySQL(sqlString, values); 165 } 166 167 @Override 168 public void refresh(T t) { 169 // TODO Auto-generated method stub 170 dao.refresh(t); 171 } 172 173 @Override 174 @Transactional(propagation = Propagation.REQUIRED) 175 public void update(T t) { 176 // TODO Auto-generated method stub 177 dao.update(t); 178 } 179 180 @Override 181 public Long countByHql(String hql, Object... values) { 182 // TODO Auto-generated method stub 183 return dao.countByHql(hql, values); 184 } 185 186 @Override 187 public PageInfo<T> findPageByHql(String hql, String countHql, int pageNo, 188 int pageSize, Object... values) { 189 // TODO Auto-generated method stub 190 return dao.findPageByHql(hql, countHql, pageNo, pageSize, values); 191 } 192 193 @Override 194 public List<T> list(String propertyName, Object value) { 195 // TODO Auto-generated method stub 196 return dao.list(propertyName, value); 197 } 198 199 @Override 200 public List<T> list(Criterion criterion) { 201 // TODO Auto-generated method stub 202 return dao.list(criterion); 203 } 204 205 @Override 206 public List<T> list(Criterion... criterions) { 207 // TODO Auto-generated method stub 208 return dao.list(criterions); 209 } 210 211 @Override 212 public T uniqueResult(String propertyName, Object value) { 213 // TODO Auto-generated method stub 214 return dao.uniqueResult(propertyName, value); 215 } 216 217 @Override 218 public T uniqueResult(Criterion... criterions) { 219 // TODO Auto-generated method stub 220 return dao.uniqueResult(criterions); 221 } 222 223 @Override 224 @Transactional(propagation = Propagation.REQUIRED, readOnly = true) 225 public T uniqueResult(Criteria criteria) { 226 // TODO Auto-generated method stub 227 return dao.uniqueResult(criteria); 228 } 229 230 @Override 231 @Transactional(propagation = Propagation.REQUIRED, readOnly = true) 232 public Integer uniqueResultInt(Criteria criteria){ 233 return dao.uniqueResultInt(criteria); 234 } 235 236 @Override 237 public Criteria distinct(Criteria criteria) { 238 // TODO Auto-generated method stub 239 return dao.distinct(criteria); 240 } 241 242 @Override 243 public void flush() { 244 // TODO Auto-generated method stub 245 dao.flush(); 246 } 247 248 @Override 249 public void clear() { 250 // TODO Auto-generated method stub 251 dao.clear(); 252 } 253 254 @Override 255 public Criteria createCriteria() { 256 // TODO Auto-generated method stub 257 return dao.createCriteria(); 258 } 259 260 @Override 261 public Criteria createCriteria(Criterion... criterions) { 262 // TODO Auto-generated method stub 263 return dao.createCriteria(criterions); 264 } 265 266 @Override 267 public List<T> findPage(Criteria criteria, int pageNo, int pageSize) { 268 // TODO Auto-generated method stub 269 return dao.findPage(criteria, pageNo, pageSize); 270 } 271 272 @Override 273 public PageInfo<T> findQuery(Criteria criteria, int pageNo, int pageSize) { 274 // TODO Auto-generated method stub 275 return dao.findQuery(criteria, pageNo, pageSize); 276 } 277 278 @Override 279 public List<T> findQuery(String hql, int pageNo, int pageSize, Map<?, ?> map) { 280 // TODO Auto-generated method stub 281 return dao.findQuery(hql, pageNo, pageSize, map); 282 } 283 284 }
我们看到实体包下的各个实体:
接下来 自动生成每一层的各个实体对应的相对应的代码文件:
1 package com.agen.test.util; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 import org.junit.Test; 8 9 public class CreateJava { 10 11 @Test 12 public void justCreateJava() throws IOException{ 13 File file = new File("F:/workspace2/performance/src/main/java/com/agen/entity"); 14 File []list = file.listFiles(); 15 for (File file2 : list) { 16 String fileName = file2.getName().substring(0,file2.getName().lastIndexOf(".")); 17 createDao(fileName); 18 createDaoImpl(fileName); 19 createService(fileName); 20 createServiceImpl(fileName); 21 } 22 } 23 /** 24 * 创建Dao层 25 * @param fileName 26 * @throws IOException 27 */ 28 public void createDao(String fileName) throws IOException{ 29 //拼接 DaoImpl内容 30 String content = "package com.agen.dao;\r\n" 31 + "\r\n" 32 + "import com.agen.entity."+fileName+";\r\n" 33 + "public interface "+fileName+"Dao extends GenericDao<"+fileName+", String> {\r\n" 34 + "\r\n" 35 + "}"; 36 37 //指定将Dao文件生成到对应的指定位置 38 FileWriter writer = new FileWriter(new File("F:/workspace2/performance/src/main/java/com/agen/dao/"+fileName+"Dao.java")); 39 writer.write(content); 40 writer.close(); 41 } 42 43 /** 44 * 创建DaoImpl层 45 */ 46 public void createDaoImpl(String fileName) throws IOException{ 47 //拼接 DaoImpl内容 48 String content = "package com.agen.dao.impl;\r\n" 49 + "\r\n" 50 + "import org.springframework.stereotype.Repository;\r\n" 51 + "import com.agen.dao."+fileName+"Dao;\r\n" 52 + "import com.agen.entity."+fileName+";\r\n" 53 + "@Repository \r\n" 54 + "public class "+fileName+"DaoImpl extends GenericDaoImpl<"+fileName+", String> implements "+fileName+"Dao {\r\n" 55 + "\r\n" 56 + "}"; 57 58 //指定将DaoImpl文件生成到对应的指定位置 59 FileWriter writer = new FileWriter(new File("F:/workspace2/performance/src/main/java/com/agen/dao/impl/"+fileName+"DaoImpl.java")); 60 writer.write(content); 61 writer.close(); 62 } 63 64 /** 65 * 创建 Service层 66 * @param fileName 67 * @throws IOException 68 */ 69 public void createService(String fileName) throws IOException{ 70 //拼接Service内容 71 String content = "package com.agen.service;\r\n" 72 + "import com.agen.entity."+fileName+";\r\n" 73 + "public interface "+fileName+"Service extends GenericService<"+fileName+", String> {\r\n" 74 + "\r\n" 75 + "}"; 76 77 FileWriter writer = new FileWriter(new File("F:/workspace2/performance/src/main/java/com/agen/service/"+fileName+"Service.java")); 78 writer.write(content); 79 writer.close(); 80 } 81 82 /** 83 * 创建ServiceImpl 84 * @throws IOException 85 */ 86 public void createServiceImpl(String fileName) throws IOException{ 87 //拼接Service内容 88 String content = "package com.agen.service.impl;\r\n" 89 + "import org.springframework.beans.factory.annotation.Autowired;\r\n" 90 + "import org.springframework.stereotype.Service;\r\n" 91 + "import com.agen.dao."+fileName+"Dao;\r\n" 92 + "import com.agen.entity."+fileName+";\r\n" 93 + "import com.agen.service."+fileName+"Service;\r\n" 94 + "@Service \r\n" 95 + "public class "+fileName+"ServiceImpl extends GenericServiceImpl<"+fileName+", String> implements "+fileName+"Service {\r\n" 96 + "\r\n" 97 + "@Autowired\r\n" 98 + "private "+fileName+"Dao dao;\r\n" 99 + "}"; 100 FileWriter writer = new FileWriter(new File("F:/workspace2/performance/src/main/java/com/agen/service/impl/"+fileName+"ServiceImpl.java")); 101 writer.write(content); 102 writer.close(); 103 } 104 105 }
生成完刷新
效果如下:
时间: 2024-11-09 00:33:31