• Hibernate实现增删改查


    1. /** 
    2. * @author fly.zhou 
    3. */  
    4. public interface IBaseDao {  
    5. //增加对应实体的一条记录    
    6. public boolean save(Object o);  
    7. //更新对应实体的一条记录  
    8. public boolean update(Object o);  
    9. //增加或者更新对应实体的一条记录  
    10. public boolean saveOrUpdate(Object o);  
    11. //保存一系列对象集合  
    12. public boolean saveOrUpdateAll(Collection l);  
    13. //删除对应实体的一条记录  
    14. public boolean delete(Object o);  
    15. //根据id删除对应实体的一条记录  
    16. public boolean delete(Class c, Serializable id);  
    17. //执行hql语句删除一条记录  
    18. public Integer delete(String hql, Object... values);  
    19. //删除一系列数据  
    20. public boolean deleteAll(Collection l);  
    21. //执行hql语句查找  
    22. public List find(String hql);  
    23. //分页查询,多参数条件  
    24. public List find(String hql, DataGridReq dgr, List values);  
    25. //分页查询,多参数条件  
    26. public List find(String hql, DataGridReq dgr, Object... values);  
    27. //不带分页查询,多参数条件  
    28. public List find(String hql, Object... values);  
    29. //不带分页查询,多参数条件  
    30. public boolean update(String hql, Object... values);  
    31. //根据主键ID查询对应实体的一条记录  
    32. public Object get(Class clazz, Serializable id);  
    33. //获取某实体对象  
    34. public Object load(Class c, Serializable id);  
    35. //获取总记录数  
    36. public Long total(String hql, List values);  
    37. //获取总记录数  
    38. public Long total(String hql, Object... values);  
    39. //更新对应实体的某一条记录  
    40. public boolean updateOneByProperty(Class clazz, Serializable id, String pName, Object pValue);  
    41. //更新对应实体的某几条记录  
    42. public boolean updateOneByPropertys(Class clazz, Serializable id, List<String> pName, List<Object> pValue);  
    43. //更新对应实体的某几条记录(封装成map)  
    44. public boolean updateOneByPropertys(Class clazz, Serializable id, Map<String, Object> map);  
    45. //根据属性名以及对应的属性值查找一条记录  
    46. public Object getSingleByProperty(Class clazz, String pName, Object pValue);  
    47. //判断是否有对应的属性名和属性值存在,存在返回true  
    48. public boolean ifHasOneByProperty(Class clazz, String pName, Object pValue);  
    49. //根据一系列属性以及对应的属性值查询一条记录  
    50. public Object getSingleByPropertys(Class clazz, List<String> pName, List<Object> pValue);  
    51. //判断是否有一系列对应的属性名和属性值存在,存在返回true  
    52. public boolean ifHasOneByPropertys(Class clazz, List<String> pName, List<Object> pValue);  
    53. //根据一系列属性以及对应的属性值(封装成Map)查询一条记录  
    54. public Object getSingleByPropertys(Class clazz, Map<String, Object> map);  
    55. //判断是否有一系列对应的属性名和属性值(封装成Map)存在,存在返回true  
    56. public boolean ifHasOneByPropertys(Class clazz, Map<String, Object> map);  
    57. //通过某一对应的属性名和属性值,查询某一个属性的值  
    58. public Object getValueByPropertys(Class clazz, Map<String, Object> map, String selectName);  
    59. //通过一系列对应的属性名和属性值,查询某一个属性的值  
    60. public Object getValueByProperty(Class clazz, String pName, Object pValue, String selectName);  
    61. //通过一系列对应的属性名和属性值,查询某一个属性的值  
    62. public Object getValueByPropertys(Class clazz, List<String> pNames, List<Object> pValues, String selectName);  
    63. //查询对应实体的所有记录  
    64. public List<Object> getObjects(Class clazz);  
    65. //查询符合属性名以及对应的属性值的一系列记录  
    66. public List<Object> getObjectsByProperty(Class clazz, String pName, Object pValue);  
    67. //根据任意属性查询名以及对应的属性值的一系列记录  
    68. public List<Object> getObjectsByAnyProperty(Class clazz, List<String> pName, List<Object> pValue);  
    69. //查询符合一系列属性名以及对应的属性值的一系列记录  
    70. public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<Object> pValue);  
    71. //根据某属性对应的属性值在某一范围内的一系列记录  
    72. public List<Object> getObjectsByProperty(Class clazz, String pName, String operator, Object pValue);  
    73. //根据某属性对应的属性值在某一范围内的一系列记录  
    74. public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<String> operator, List<Object> pValue);  
    75. }  



    2》接口的实现 BaseDaoImpl.java

    [java] view plaincopy

    1. public class BaseDaoImpl implements IBaseDao {  
    2. private static final Logger logger = Logger.getLogger(BaseDaoImpl.class);  
    3. private HibernateTemplate hibernateTemplate;  
    4. public HibernateTemplate getHibernateTemplate() {  
    5. hibernateTemplate.setCacheQueries(true);// 开启二级查询缓存  
    6. return hibernateTemplate;  
    7. }  
    8. @Autowired  
    9. public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
    10. this.hibernateTemplate = hibernateTemplate;  
    11. }  
    12. @Override  
    13. public boolean delete(Object o) {  
    14. //        logger.info("删除");  
    15. try {  
    16. this.getHibernateTemplate().delete(o);  
    17. return true;  
    18. } catch (Exception e) {  
    19. return false;  
    20. }  
    21. }  
    22. @Override  
    23. public boolean delete(Class c, Serializable id) {  
    24. //        logger.info("删除");  
    25. try {  
    26. this.getHibernateTemplate().delete(get(c, id));  
    27. return true;  
    28. } catch (Exception e) {  
    29. return false;  
    30. }  
    31. }  
    32. @Override  
    33. public Integer delete(final String hql, final Object... values) {  
    34. return this.getHibernateTemplate().execute(new HibernateCallback<Integer>() {  
    35. @Override  
    36. public Integer doInHibernate(Session session) throws HibernateException, SQLException {  
    37. Query q = session.createQuery(hql);  
    38. if (values != null && values.length > 0) {  
    39. for (int i = 0; i < values.length; i++) {  
    40. q.setParameter(i, values[i]);  
    41. }  
    42. }  
    43. return q.executeUpdate();  
    44. }  
    45. });  
    46. }  
    47. @Override  
    48. public boolean deleteAll(Collection l) {  
    49. //        logger.info("删除");  
    50. try {  
    51. this.getHibernateTemplate().deleteAll(l);  
    52. return true;  
    53. } catch (Exception e) {  
    54. return false;  
    55. }  
    56. }  
    57. @Override  
    58. public boolean update(Object o) {  
    59. //        logger.info("更新");  
    60. try {  
    61. this.getHibernateTemplate().update(o);  
    62. return true;  
    63. } catch (Exception e) {  
    64. e.printStackTrace();  
    65. return false;  
    66. }  
    67. }  
    68. @Override  
    69. public boolean save(Object o) {  
    70. //        logger.info("保存");  
    71. try {  
    72. this.getHibernateTemplate().save(o);  
    73. return true;  
    74. } catch (Exception e) {  
    75. return false;  
    76. }  
    77. }  
    78. @Override  
    79. public boolean saveOrUpdate(Object o) {  
    80. try {  
    81. this.getHibernateTemplate().saveOrUpdate(o);  
    82. return true;  
    83. } catch (Exception e) {  
    84. return false;  
    85. }  
    86. }  
    87. @Override  
    88. public boolean saveOrUpdateAll(Collection l) {  
    89. //        logger.info("编辑");  
    90. try {  
    91. this.getHibernateTemplate().saveOrUpdateAll(l);  
    92. return true;  
    93. } catch (Exception e) {  
    94. return false;  
    95. }  
    96. }  
    97. @Override  
    98. public List find(String hql) {  
    99. //        logger.info("查询");  
    100. return this.getHibernateTemplate().find(hql);  
    101. }  
    102. @Override  
    103. public List find(final String hql, final DataGridReq dgr, final List values) {  
    104. return this.getHibernateTemplate().execute(new HibernateCallback<List>() {  
    105. @Override  
    106. public List doInHibernate(Session session) throws HibernateException, SQLException {  
    107. Query q = session.createQuery(hql);  
    108. if (values != null && values.size() > 0) {  
    109. for (int i = 0; i < values.size(); i++) {  
    110. q.setParameter(i, values.get(i));  
    111. }  
    112. }  
    113. if (dgr == null) {  
    114. return q.list();  
    115. }  
    116. return q.setFirstResult(dgr.getStart()).setMaxResults(dgr.getLimit() * dgr.getPage()).list();  
    117. }  
    118. });  
    119. }  
    120. @Override  
    121. public List find(final String hql, final DataGridReq dgr, final Object... values) {  
    122. return this.getHibernateTemplate().execute(new HibernateCallback<List>() {  
    123. @Override  
    124. public List doInHibernate(Session session) throws HibernateException, SQLException {  
    125. Query q = session.createQuery(hql);  
    126. if (values != null && values.length > 0) {  
    127. for (int i = 0; i < values.length; i++) {  
    128. q.setParameter(i, values[i]);  
    129. }  
    130. }  
    131. if (dgr == null) {  
    132. return q.list();  
    133. }  
    134. return q.setFirstResult(dgr.getStart()).setMaxResults(dgr.getLimit() * dgr.getPage()).list();  
    135. }  
    136. });  
    137. }  
    138. @Override  
    139. public List find(String hql, Object... values) {  
    140. return this.getHibernateTemplate().find(hql, values);  
    141. }  
    142. @Override  
    143. public boolean update(final String hql, final Object... values) {  
    144. try {  
    145. this.getHibernateTemplate().bulkUpdate(hql, values);  
    146. return true;  
    147. } catch (Exception e) {  
    148. return false;  
    149. }  
    150. }  
    151. @Override  
    152. public Object get(Class c, Serializable id) {  
    153. return this.getHibernateTemplate().get(c, id);  
    154. }  
    155. @Override  
    156. public Object load(Class c, Serializable id) {  
    157. return this.getHibernateTemplate().load(c, id);  
    158. }  
    159. @Override  
    160. public Long total(final String hql, final List values) {  
    161. return this.getHibernateTemplate().execute(new HibernateCallback<Long>() {  
    162. @Override  
    163. public Long doInHibernate(Session session) throws HibernateException, SQLException {  
    164. Query q = session.createQuery(hql);  
    165. if (values != null && values.size() > 0) {  
    166. for (int i = 0; i < values.size(); i++) {  
    167. q.setParameter(i, values.get(i));  
    168. }  
    169. }  
    170. return (Long) q.uniqueResult();  
    171. }  
    172. });  
    173. }  
    174. @Override  
    175. public Long total(final String hql, final Object... values) {  
    176. return this.getHibernateTemplate().execute(new HibernateCallback<Long>() {  
    177. @Override  
    178. public Long doInHibernate(Session session) throws HibernateException, SQLException {  
    179. Query q = session.createQuery(hql);  
    180. if (values != null && values.length > 0) {  
    181. for (int i = 0; i < values.length; i++) {  
    182. q.setParameter(i, values[i]);  
    183. }  
    184. }  
    185. return (Long) q.uniqueResult();  
    186. }  
    187. });  
    188. }  
    189. @Override  
    190. public boolean updateOneByProperty(Class clazz, Serializable id, String pName, Object pValue) {  
    191. String hql = "update " + clazz.getName() + " entity set entity." + pName + " = '" + pValue + "' where entity.id = " + id;  
    192. getHibernateTemplate().bulkUpdate(hql);  
    193. return true;  
    194. }  
    195. @Override  
    196. public boolean updateOneByPropertys(Class clazz, Serializable id, List<String> pName, List<Object> pValue) {  
    197. String hql = "update " + clazz.getName() + " entity set entity.";  
    198. int maxIndex = pName.size() - 1;  
    199. for (int i = 0; i < maxIndex; i++) {  
    200. hql += pName.get(i) + " = '" + pValue.get(i) + "', entity.";  
    201. }  
    202. hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'where entity.id = " + id;  
    203. System.out.println(hql);  
    204. getHibernateTemplate().bulkUpdate(hql);  
    205. return true;  
    206. }  
    207. @Override  
    208. public boolean updateOneByPropertys(Class clazz, Serializable id, Map<String, Object> map) {  
    209. String hql = "update " + clazz.getName() + " entity set entity.";  
    210. Set set = map.entrySet();  
    211. if (set != null) {  
    212. Iterator iterator = set.iterator();  
    213. while (iterator.hasNext()) {  
    214. Entry entry = (Entry) iterator.next();  
    215. Object key = entry.getKey();  
    216. Object value = entry.getValue();  
    217. hql += key + " = '" + value + "' and entity.";  
    218. }  
    219. }  
    220. hql = hql.substring(0, hql.length() - 12);  
    221. hql += "'where entity.id = " + id;  
    222. System.out.println(hql);  
    223. getHibernateTemplate().bulkUpdate(hql);  
    224. return true;  
    225. }  
    226. /** 
    227. * 根据属性查询 
    228. * @param clazz 
    229. * @param pName 
    230. * @param pValue 
    231. * @return 
    232. */  
    233. @Override  
    234. public Object getSingleByProperty(Class clazz, String pName, Object pValue) {  
    235. String hql = "from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
    236. List<Object> os = getHibernateTemplate().find(hql);  
    237. if (os != null && !os.isEmpty()) {  
    238. return os.get(0);  
    239. } else {  
    240. return null;  
    241. }  
    242. }  
    243. /** 
    244. * 根据属性查询 
    245. * @param clazz 
    246. * @param pName 
    247. * @param pValue 
    248. * @return 
    249. */  
    250. @Override  
    251. public boolean ifHasOneByProperty(Class clazz, String pName, Object pValue) {  
    252. String hql = "from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
    253. List<Object> os = getHibernateTemplate().find(hql);  
    254. if (os != null && !os.isEmpty()) {  
    255. return true;  
    256. } else {  
    257. return false;  
    258. }  
    259. }  
    260. /** 
    261. * 根据属性查询 
    262. * @param clazz 
    263. * @param pName 
    264. * @param pValue 
    265. * @return 
    266. */  
    267. @Override  
    268. public List<Object> getObjectsByAnyProperty(Class clazz, List<String> pName, List<Object> pValue) {  
    269. String hql = "from " + clazz.getName() + " entity where entity.";  
    270. int maxIndex = pName.size() - 1;  
    271. for (int i = 0; i < maxIndex; i++) {  
    272. hql += pName.get(i) + " = '" + pValue.get(i) + "' or entity.";  
    273. }  
    274. hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
    275. System.out.println(hql);  
    276. List<Object> os = getHibernateTemplate().find(hql);  
    277. if (os != null && !os.isEmpty()) {  
    278. return os;  
    279. } else {  
    280. return null;  
    281. }  
    282. }  
    283. @Override  
    284. public boolean ifHasOneByPropertys(Class clazz, List<String> pName, List<Object> pValue) {  
    285. String hql = "from " + clazz.getName() + " entity where entity.";  
    286. int maxIndex = pName.size() - 1;  
    287. for (int i = 0; i < maxIndex; i++) {  
    288. hql += pName.get(i) + " = '" + pValue.get(i) + "' and entity.";  
    289. }  
    290. hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
    291. System.out.println(hql);  
    292. List<Object> os = getHibernateTemplate().find(hql);  
    293. if (os != null && !os.isEmpty()) {  
    294. return true;  
    295. } else {  
    296. return false;  
    297. }  
    298. }  
    299. @Override  
    300. public Object getSingleByPropertys(Class clazz, List<String> pName, List<Object> pValue) {  
    301. String hql = "from " + clazz.getName() + " entity where entity.";  
    302. int maxIndex = pName.size() - 1;  
    303. for (int i = 0; i < maxIndex; i++) {  
    304. hql += pName.get(i) + " = '" + pValue.get(i) + "' and entity.";  
    305. }  
    306. hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
    307. System.out.println(hql);  
    308. List<Object> os = getHibernateTemplate().find(hql);  
    309. if (os != null && !os.isEmpty()) {  
    310. return os.get(0);  
    311. } else {  
    312. return null;  
    313. }  
    314. }  
    315. @Override  
    316. public Object getSingleByPropertys(Class clazz, Map<String, Object> map) {  
    317. String hql = "from " + clazz.getName() + " entity where entity.";  
    318. Set set = map.entrySet();  
    319. if (set != null) {  
    320. Iterator iterator = set.iterator();  
    321. while (iterator.hasNext()) {  
    322. Entry entry = (Entry) iterator.next();  
    323. Object key = entry.getKey();  
    324. Object value = entry.getValue();  
    325. hql += key + " = '" + value + "' and entity.";  
    326. }  
    327. }  
    328. hql = hql.substring(0, hql.length() - 12);  
    329. System.out.println("hql = " + hql);  
    330. List<Object> os = getHibernateTemplate().find(hql);  
    331. if (os != null && !os.isEmpty()) {  
    332. return os.get(0);  
    333. } else {  
    334. return null;  
    335. }  
    336. }  
    337. @Override  
    338. public boolean ifHasOneByPropertys(Class clazz, Map<String, Object> map) {  
    339. String hql = "from " + clazz.getName() + " entity where entity.";  
    340. Set set = map.entrySet();  
    341. if (set != null) {  
    342. Iterator iterator = set.iterator();  
    343. while (iterator.hasNext()) {  
    344. Entry entry = (Entry) iterator.next();  
    345. Object key = entry.getKey();  
    346. Object value = entry.getValue();  
    347. hql += key + " = '" + value + "' and entity.";  
    348. }  
    349. }  
    350. hql = hql.substring(0, hql.length() - 12);  
    351. List<Object> os = getHibernateTemplate().find(hql);  
    352. if (os != null && !os.isEmpty()) {  
    353. return true;  
    354. } else {  
    355. return false;  
    356. }  
    357. }  
    358. /** 
    359. * 查询所有 
    360. */  
    361. @Override  
    362. public List<Object> getObjects(Class clazz) {  
    363. String hql = "from " + clazz.getName();  
    364. List list = getHibernateTemplate().find(hql);  
    365. return list;  
    366. }  
    367. /** 
    368. * 根据属性查询 全部 
    369. * @param clazz 
    370. * @param pName 
    371. * @param pValue 
    372. * @return 
    373. */  
    374. @Override  
    375. public List<Object> getObjectsByProperty(Class clazz, String pName, Object pValue) {  
    376. String hql = "from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
    377. return getHibernateTemplate().find(hql);  
    378. }  
    379. @Override  
    380. public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<Object> pValue) {  
    381. String hql = "from " + clazz.getName() + " entity where entity.";  
    382. int maxIndex = pName.size() - 1;  
    383. for (int i = 0; i < maxIndex; i++) {  
    384. hql += pName.get(i) + " = '" + pValue.get(i) + "' and ";  
    385. }  
    386. hql += pName.get(maxIndex) + " = '" + pValue.get(maxIndex) + "'";  
    387. return getHibernateTemplate().find(hql);  
    388. }  
    389. /** 
    390. * 根据属性查询 全部 
    391. * @param clazz 
    392. * @param pName 
    393. * @param pValue 
    394. * @return 
    395. */  
    396. @Override  
    397. public List<Object> getObjectsByProperty(Class clazz, String pName, String operator, Object pValue) {  
    398. String hql = "from " + clazz.getName() + " entity where entity." + pName + " " + operator + pValue;  
    399. return getHibernateTemplate().find(hql);  
    400. }  
    401. @Override  
    402. public List<Object> getObjectsByPropertys(Class clazz, List<String> pName, List<String> operator, List<Object> pValue) {  
    403. String hql = "from " + clazz.getName() + " entity where entity.";  
    404. int maxIndex = pName.size() - 1;  
    405. for (int i = 0; i < maxIndex; i++) {  
    406. hql += pName.get(i) + " " + operator.get(i) + " '" + pValue.get(i) + "' and ";  
    407. }  
    408. hql += pName.get(maxIndex) + " " + operator.get(maxIndex) + " '" + pValue.get(maxIndex) + "'";  
    409. System.out.println("hql = " + hql);  
    410. return getHibernateTemplate().find(hql);  
    411. }  
    412. @Override  
    413. public Object getValueByPropertys(Class clazz, Map<String, Object> map, String selectName) {  
    414. String hql = "select entity." + selectName + "from " + clazz.getName() + " entity where entity.";  
    415. Set set = map.entrySet();  
    416. if (set != null) {  
    417. Iterator iterator = set.iterator();  
    418. while (iterator.hasNext()) {  
    419. Entry entry = (Entry) iterator.next();  
    420. Object key = entry.getKey();  
    421. Object value = entry.getValue();  
    422. hql += key + " = '" + value + "' and entity.";  
    423. }  
    424. }  
    425. hql = hql.substring(0, hql.length() - 12);  
    426. System.out.println("hql = " + hql);  
    427. return getHibernateTemplate().find(hql);  
    428. }  
    429. @Override  
    430. public Object getValueByProperty(Class clazz, String pName, Object pValue, String selectName) {  
    431. String hql = "select entity." + selectName + " from " + clazz.getName() + " entity where entity." + pName + " = '" + pValue + "'";  
    432. System.out.println("hql = " + hql);  
    433. return getHibernateTemplate().find(hql);  
    434. }  
    435. @Override  
    436. public Object getValueByPropertys(Class clazz, List<String> pNames, List<Object> pValues, String selectName) {  
    437. String hql = "select entity." + selectName + "from " + clazz.getName() + " entity where entity.";  
    438. int maxIndex = pNames.size() - 1;  
    439. for (int i = 0; i < maxIndex; i++) {  
    440. hql += pNames.get(i) + " = '" + pValues.get(i) + "' and ";  
    441. }  
    442. hql += pNames.get(maxIndex) + " = '" + pValues.get(maxIndex) + "'";  
    443. System.out.println("hql = " + hql);  
    444. return getHibernateTemplate().find(hql);  
    445. }  
    446. }  



    3》值得注意的地方Map如何获取key

    [java] view plaincopy

    1. Set set = map.entrySet();  
    2. if (set != null) {  
    3. Iterator iterator = set.iterator();  
    4. while (iterator.hasNext()) {  
    5. Entry entry = (Entry) iterator.next();  
    6. Object key = entry.getKey();  
    7. Object value = entry.getValue();  
    8. hql += key + " = '" + value + "' and entity.";  
    9. }  
    10. }  
  • 相关阅读:
    升级到 classpath 'com.android.tools.build:gradle:1.0.0-rc1
    OnScrollListenerPro
    dp和px的转换
    ListView 中判断是否滚动到底部
    为什么setAdapter之后不能addHeadView或者addfooterView
    SwipeRefreshLayout使用小记
    Git-Flow 带你飞!
    在aws的ec2服务器上搭建nginx+php的环境
    PHP程序员的技术成长规划
    GeoHash核心原理解析
  • 原文地址:https://www.cnblogs.com/xinxinjiayuan/p/4930878.html
Copyright © 2020-2023  润新知