SSH中HQL更新语句应该怎么改?

q31450251 2014-07-11 03:31:40
下面为dao 包更新段方法 代码
public void update(String username,User user) {
this.getHibernateTemplate().update(username, user);
}
下面为biz 业务层包 更新段方法代码
public void update(String username, User user) {
//调用DAO查询数据库,获得数据库里面的用户对象
String hql="update User set name=" + user.getName() + ",pass=" + user.getPass()
+ " where name=" + username;
User dbUser = userDAO.getUserByHql(hql);
}
下面为action 调用业务层 更新方法 然后执行跳转
public String update(){
try{
userBiz.update(username, user);
}catch(Exception e){
e.printStackTrace();
return "update";
}
return "update-success";
}
请问下:这样写 报错 Not supported for DML operations 说不支持数据库操作,请问应该怎么改,或者哪个好人的发个SSH框架(包括增删改查的最好)代码给我!先谢谢大家了!
...全文
3196 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_34981236 2016-05-20
  • 打赏
  • 举报
回复
hql语句的语法格式错误~ 你既然都用了hibernate支持类,就别拼hql语句了,直接set属性值,再调用getHibernateTemplate().update(obj)就行了~
q31450251 2014-07-17
  • 打赏
  • 举报
回复
谢谢大家的 解答,已经解决了。 原因是:UserDaoImpl 继承 HibernateDaoSupport 的原因,后来把继承关系删了,在spring配置文件里面添加了个 HibernateTemplate的bean 就 能用了。 getHibernateTemplate().delete(user); /*Session session=sf.getCurrentSession(); session.delete(user);*/ public class UserDAOImpl implements UserDAO { private SessionFactory sf; private HibernateTemplate hibernateTemplates; public HibernateTemplate getHibernateTemplates() { return hibernateTemplates; } @Resource(name="hibernateTemplate") public void setHibernateTemplates(HibernateTemplate hibernateTemplates) { this.hibernateTemplates = hibernateTemplates; } public SessionFactory getSf() { return sf; } @Resource(name="sf") //用来装配bean public void setSf(SessionFactory sf) { this.sf = sf; }
shixitong 2014-07-11
  • 打赏
  • 举报
回复
把getUserByHql改为executeBySql试试
javaLLJ123 2014-07-11
  • 打赏
  • 举报
回复
分页对象Page.java: [java] view plaincopyprint? 01.package com.hoo.entity; 02. 03.import java.util.ArrayList; 04.import java.util.List; 05. 06./** 07. * <b>function:</b> 分页entity,封装分页数据 08. * @project NetWorkService 09. * @package com.hoo.entity 10. * @fileName Page.java 11. * @createDate 2010-8-3 上午10:32:03 12. * @author hoojo 13. * @email hoojo_@126.com 14. * @blog http://blog.csdn.net/IBM_hoojo 15. */ 16.public class Page<T> { 17. //当前页数 18. private int currentPage; 19. //总页数 20. private int totalsPage; 21. //每页显示记录条数 22. private int pageSize; 23. //总记录条数 24. private int totalsCount; 25. //查询返回结果 26. private List<T> result = new ArrayList<T>(); 27. //分页链接 28. private String uri; 29. 30. public String getUri() { 31. return uri; 32. } 33. public void setUri(String uri) { 34. this.uri = uri; 35. } 36. public int getCurrentPage() { 37. return currentPage; 38. } 39. public void setCurrentPage(int currentPage) throws Exception { 40. if (currentPage < 0) { 41. currentPage = 0; 42. } 43. this.currentPage = currentPage; 44. } 45. public int getTotalsPage() { 46. try { 47. if (totalsCount % pageSize == 0) { 48. totalsPage = totalsCount / pageSize; 49. } else { 50. totalsPage = (totalsCount / pageSize) + 1; 51. } 52. } catch (Exception e) { 53. throw new RuntimeException(e); 54. } 55. return totalsPage; 56. } 57. public void setTotalsPage(int totalsPage) { 58. if (totalsPage < 0) { 59. totalsPage = 0; 60. } 61. this.totalsPage = totalsPage; 62. } 63. public int getPageSize() { 64. return pageSize; 65. } 66. public void setPageSize(int pageSize) { 67. if (pageSize <= 0) { 68. pageSize = 20; 69. } 70. this.pageSize = pageSize; 71. } 72. public int getTotalsCount() { 73. return totalsCount; 74. } 75. public void setTotalsCount(int totalsCount) { 76. if (totalsCount < 0) { 77. totalsCount = 0; 78. } 79. this.totalsCount = totalsCount; 80. } 81. public List<T> getResult() { 82. return result; 83. } 84. public void setResult(List<T> result) { 85. this.result = result; 86. } 87.} 增删改查简单示例: [java] view plaincopyprint? 01.package com.hoo.dao.impl; 02. 03.import java.util.List; 04. 05.import com.hoo.dao.IBaseDao; 06.import com.hoo.entity.UserInfo; 07. 08.public class TestDaoImpl<T extends UserInfo> implements ITestDao<T> { 09. private IBaseDao dao; 10. //setter方法注入dao 11. public void setDao(IBaseDao dao) { 12. this.dao = dao; 13. } 14. 15. //查询 16. public <T extends UserInfo> List<T> getSortListByParentId(T entity) throws Exception { 17. String hql = "...."; 18. if (entity == null || entity.getUserName() == null || "".equals(entity.getId())) { 19. hql += " ..."; 20. } else { 21. hql += " ..."; 22. } 23. return dao.getList(hql); 24. } 25. 26. //添加 27. public boolean addUser(T entity) { 28. return dao.add(entity); 29. } 30. 31. //删除 32. public boolean removeUser(T entity) { 33. return dao.remove(entity); 34. } 35. 36. //修改 37. public boolean editUser(T entity) { 38. return dao.edit(entity); 39. } 40.}
javaLLJ123 2014-07-11
  • 打赏
  • 举报
回复
293. /** 294. * <b>function:</b> 传入一个entity对象Class和String型主键,返回该对象 295. * @createDate 2010-8-2 下午05:44:53 296. * @author hoojo 297. * @param <T> 返回、传入对象类型 298. * @param c 对象Class 299. * @param id 主键 300. * @return T 返回传入类型对象 301. * @throws Exception 302. */ 303. public <T> T getById(Class<T> c, String id) throws Exception { 304. T ety = null; 305. try { 306. ety = (T) this.getHibernateTemplate().get(c, id); 307. } catch (Exception e) { 308. throw new RuntimeException(e); 309. } 310. return ety; 311. } 312. 313. /** 314. * <b>function:</b> 传入hql查询语句和object数组类型的参数,返回查询list集合 315. * @createDate 2010-8-2 下午05:52:36 316. * @author hoojo 317. * @param hql 查询的hql语句 318. * @param obj 查询参数 319. * @return 返回list集合 320. * @throws Exception 321. */ 322. public <T> List<T> getList(String hql, Object[] obj) throws Exception { 323. List<T> list = null; 324. try { 325. list = (List<T>) this.getHibernateTemplate().find(hql, obj); 326. } catch (Exception e) { 327. throw new RuntimeException(e); 328. } 329. return list; 330. } 331. 332. /** 333. * <b>function:</b> 传入一个将要删除的entity对象,返回删除是否成功 334. * @createDate 2010-8-2 下午05:42:02 335. * @author hoojo 336. * @param <T> 传入对象类型 337. * @param entity 将要传入的对象 338. * @return boolean true/false 339. * @throws Exception 340. */ 341. public <T> boolean remove(T entity) throws Exception { 342. boolean bo = false; 343. try { 344. this.getHibernateTemplate().delete(entity); 345. bo = true; 346. } catch (Exception e) { 347. bo = false; 348. throw new RuntimeException(e); 349. } 350. return bo; 351. } 352. 353. /** 354. * <b>function:</b> 传入删除的hql语句,删除记录 355. * @createDate 2010-8-3 上午09:53:49 356. * @author hoojo 357. * @param hql 将要被执行删除的hql语句 358. * @return 是否删除成功 359. * @throws Exception 360. */ 361. public boolean remove(String hql) throws Exception { 362. try { 363. return this.executeByHql(hql) > 0 ? true : false; 364. } catch (Exception e) { 365. throw new RuntimeException(e); 366. } 367. } 368. 369. /** 370. * <b>function:</b> 动态查询 371. * @createDate 2010-8-3 上午10:53:37 372. * @author hoojo 373. * @param <T> 查询类的类型 374. * @param c 动态查询组合对象 375. * @return list集合 376. * @throws Exception 377. */ 378. public <T> List<T> getList(Class<T> c) throws Exception { 379. List<T> list = null; 380. try { 381. this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(c)); 382. } catch (Exception e) { 383. throw new RuntimeException(e); 384. } 385. return list; 386. } 387. 388. /** 389. * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; 390. * list集合保存总记录调试和记录结果 391. * @createDate 2010-8-2 下午05:54:01 392. * @author hoojo 393. * @param queryHql 查询记录hql语句 394. * @param queryCountHql 查询记录条数hql语句 395. * @param firstResult 当前查询页 396. * @param maxResult 每页显示多少条 397. * @return List返回集合 集合0保存查询结果、集合1保存总记录条数 398. * @throws Exception 399. */ 400. public List<?> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception { 401. List<Object> list = new ArrayList<Object>(); 402. try { 403. Session session = this.getSession(); 404. list.add(session.createQuery(queryHql) 405. .setFirstResult(firstResult).setMaxResults(maxResult).list()); 406. list.add(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult()); 407. } catch (Exception e) { 408. throw new RuntimeException(e); 409. } 410. return list; 411. } 412. 413. /** 414. * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合; 415. * @createDate 2010-8-3 上午11:16:59 416. * @author hoojo 417. * @param queryHql list集合结果查询 418. * @param queryCountHql 总记录调试查询 419. * @param page 分页对象 420. * @throws Exception 421. */ 422. public <T> void showPage(String queryHql, String queryCountHql, Page<T> page) throws Exception { 423. try { 424. Session session = this.getSession(); 425. page.setResult(session.createQuery(queryHql) 426. .setFirstResult(page.getCurrentPage()).setMaxResults(page.getPageSize()).list()); 427. page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString())); 428. } catch (Exception e) { 429. throw new RuntimeException(e); 430. } 431. } 432. 433. /** 434. * <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页 435. * @createDate 2010-8-3 上午11:04:39 436. * @author hoojo 437. * @param queryCountHql hql查询count语句总条数 438. * @param cResult DetachedCriteria 动态查询条件 439. * @param firstResult 起始 440. * @param maxResult 最大页数 441. * @return List<?> 查询集合 442. * @throws Exception 443. */ 444. public List<?> showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception { 445. List<Object> list = new ArrayList<Object>(); 446. try { 447. Session session = this.getSession(); 448. list.add(this.getHibernateTemplate().findByCriteria(cResult, firstResult, maxResult)); 449. list.add(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult()); 450. } catch (Exception e) { 451. throw new RuntimeException(e); 452. } 453. return list; 454. } 455. 456. /** 457. * <b>function:</b> 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity 458. * @createDate 2010-8-3 上午11:14:30 459. * @author hoojo 460. * @param queryCountHql 查询count语句 461. * @param cResult DetachedCriteria 动态查询组合类 462. * @param page Page分页实体类 463. * @throws Exception 464. */ 465. public <T> void showPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception { 466. try { 467. Session session = this.getSession(); 468. page.setResult(this.getHibernateTemplate().findByCriteria(cResult, page.getCurrentPage(), page.getPageSize())); 469. page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString())); 470. } catch (Exception e) { 471. throw new RuntimeException(e); 472. } 473. } 474. 475. /** 476. * <b>function:</b> 传入查询条件DetachedCriteria进行查询 477. * @createDate 2010-8-3 上午11:55:28 478. * @author hoojo 479. * @param <T> 类型 480. * @param dc DetachedCriteria动态条件查询 481. * @return List 482. * @throws Exception 483. */ 484. public <T> List<T> find(DetachedCriteria dc) throws Exception { 485. List<T> list = new ArrayList<T>(); 486. try { 487. list = (List<T>) this.getHibernateTemplate().findByCriteria(dc); 488. } catch (Exception e) { 489. throw new RuntimeException(e); 490. } 491. return list; 492. } 493. 494. /** 495. * <b>function:</b> 暴露基类session供用户使用 496. * @createDate 2010-8-3 上午11:59:54 497. * @author hoojo 498. * @return Session 499. */ 500. public Session session() { 501. return this.getSession(); 502. } 503. 504. /** 505. * <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作 506. * @createDate 2010-8-3 上午11:58:51 507. * @author hoojo 508. * @return HibernateTemplate 509. */ 510. public HibernateTemplate getTemplate() { 511. return this.getHibernateTemplate(); 512. } 513.}
javaLLJ123 2014-07-11
  • 打赏
  • 举报
回复
SSH: dao继承hibernatedaosupport 然后: 01.package com.hoo.dao.impl; 02. 03.import java.io.Serializable; 04.import java.util.ArrayList; 05.import java.util.List; 06. 07.import org.hibernate.Session; 08.import org.hibernate.criterion.DetachedCriteria; 09.import org.springframework.orm.hibernate3.HibernateTemplate; 10.import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 11. 12.import com.hoo.dao.IBaseDao; 13.import com.hoo.entity.Page; 14. 15./** 16. * <b>function:</b> 删除改查组件实现类 17. * @project NetWorkService 18. * @package com.hhh.dao 19. * @fileName BaseDAOImpl.java 20. * @createDate 2010-8-2 下午05:58:45 21. * @author hoojo 22. * @email hoojo_@126.com 23. * @blog http://blog.csdn.net/IBM_hoojo 24. */ 25.@SuppressWarnings("unchecked") 26.public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao { 27. 28. /** 29. * <b>function:</b> 增加一个entity对象,返回是否添加成功 30. * @createDate 2010-8-2 下午05:28:38 31. * @author hoojo 32. * @param <T> 对象类型 33. * @param entity 对象 34. * @return boolean true/false 35. * @throws Exception 36. */ 37. public <T> boolean add(T entity) throws Exception { 38. boolean bo = false; 39. try { 40. Serializable io = this.getHibernateTemplate().save(entity); 41. if (io != null) { 42. bo = true; 43. } 44. } catch (Exception e) { 45. bo = false; 46. throw new RuntimeException(e); 47. } 48. return bo; 49. } 50. 51. /** 52. * <b>function:</b> 添加一个entity对象,返回添加对象的Integer类型的主键 53. * @createDate 2010-8-2 下午05:29:39 54. * @author hoojo 55. * @param <T> 对象类型 56. * @param entity 将要添加的对象 57. * @return Integer 返回主键 58. * @throws Exception 59. */ 60. public <T> Integer addAndGetId4Integer(T entity) throws Exception { 61. Integer id = null; 62. try { 63. id = (Integer) this.getHibernateTemplate().save(entity); 64. } catch (Exception e) { 65. throw new RuntimeException(e); 66. } 67. return id; 68. } 69. 70. /** 71. * <b>function:</b> 添加一个对象并且返回该对象的String类型的主键 72. * @createDate 2010-8-2 下午05:31:32 73. * @author hoojo 74. * @param <T> 对象类型 75. * @param entity 将要添加的对象 76. * @return String 返回的主键 77. * @throws Exception 78. */ 79. public <T> String addAndGetId4String(T entity) throws Exception { 80. String id = null; 81. try { 82. id = (String) this.getHibernateTemplate().save(entity); 83. } catch (Exception e) { 84. throw new RuntimeException(e); 85. } 86. return id; 87. } 88. 89. /** 90. * <b>function:</b> 修改entity对象,返回是否修改成功 91. * @createDate 2010-8-2 下午05:35:47 92. * @author hoojo 93. * @param <T> 对象类型 94. * @param entity 将要修改的对象 95. * @return boolean true/false 是否修改成功 96. * @throws Exception 97. */ 98. public <T> boolean edit(T entity) throws Exception { 99. boolean bo = false; 100. try { 101. this.getHibernateTemplate().update(entity); 102. bo = true; 103. } catch (Exception e) { 104. bo = false; 105. throw new RuntimeException(e); 106. } 107. return bo; 108. } 109. 110. /** 111. * <b>function:</b> 传入hql语句执行修改,返回是否修改成功 112. * @createDate 2010-8-2 下午05:36:31 113. * @author hoojo 114. * @param hql 查询的hql语句 115. * @return boolean true/false 返回是否修改成功 116. * @throws Exception 117. */ 118. public boolean edit(String hql) throws Exception { 119. boolean bo = false; 120. try { 121. int count = this.getHibernateTemplate().bulkUpdate(hql); 122. bo = count > 0 ? true : false; 123. } catch (Exception e) { 124. bo = false; 125. throw new RuntimeException(e); 126. } 127. return bo; 128. } 129. 130. /** 131. * <b>function:</b> 执行修改的hql语句,返回修改的行数 132. * @createDate 2010-8-2 下午05:38:58 133. * @author hoojo 134. * @param hql 修改语句 135. * @return int 返回修改的行数 136. * @throws Exception 137. */ 138. public int editByHql(String hql) throws Exception { 139. int count = 0; 140. try { 141. count = this.getHibernateTemplate().bulkUpdate(hql); 142. } catch (Exception e) { 143. throw new RuntimeException(e); 144. } 145. return count; 146. } 147. 148. /** 149. * <b>function:</b> 传入hql语句执行 150. * @createDate 2010-8-2 下午04:42:26 151. * @author hoojo 152. * @param hql String hql语句 153. * @return int 影响行数 154. * @throws Exception 155. */ 156. public int executeByHql(String hql) throws Exception { 157. try { 158. return this.getHibernateTemplate().bulkUpdate(hql); 159. } catch (Exception e) { 160. throw new RuntimeException(e); 161. } 162. } 163. 164. /** 165. * <b>function:</b> 传入hql语句执行查询,返回list集合 166. * @createDate 2010-8-3 上午10:00:34 167. * @author hoojo 168. * @param hql 查询的hql语句 169. * @return List集合 170. * @throws Exception 171. */ 172. public <T> List<T> findByHql(String hql) throws Exception { 173. List list = null; 174. try { 175. list = (List<T>) this.getHibernateTemplate().find(hql); 176. } catch (Exception e) { 177. throw new RuntimeException(e); 178. } 179. return list; 180. } 181. 182. /** 183. * <b>function:</b> 执行原生态的sql语句,添加、删除、修改语句 184. * @createDate 2010-8-2 下午05:33:42 185. * @author hoojo 186. * @param sql 将要执行的sql语句 187. * @return int 188. * @throws Exception 189. */ 190. public int executeBySql(String sql) throws Exception { 191. try { 192. return this.getSession().createSQLQuery(sql).executeUpdate(); 193. } catch (Exception e) { 194. throw new RuntimeException(e); 195. } 196. } 197. 198. /** 199. * <b>function:</b> 传入sql语句执行查询,返回list集合 200. * @createDate 2010-8-3 上午10:00:34 201. * @author hoojo 202. * @param sql 查询的sql语句 203. * @return List集合 204. * @throws Exception 205. */ 206. public <T> List<T> findBySql(String sql) throws Exception { 207. List list = null; 208. try { 209. list = (List<T>) this.getSession().createSQLQuery(sql).list(); 210. } catch (Exception e) { 211. throw new RuntimeException(e); 212. } 213. return list; 214. } 215. 216. /** 217. * <b>function:</b> 传入一个entity对象Class和Serializable类型主键,返回该对象 218. * @createDate 2010-8-2 下午05:48:36 219. * @author hoojo 220. * @param <T> 返回、传入对象类型 221. * @param c 对象Class 222. * @param id 主键 223. * @return T 返回该类型的对象 224. * @throws Exception 225. */ 226. public <T> T get(Class<T> c, Serializable id) throws Exception { 227. T ety = null; 228. try { 229. ety = (T) this.getHibernateTemplate().get(c, id); 230. } catch (Exception e) { 231. throw new RuntimeException(e); 232. } 233. return ety; 234. } 235. 236. /** 237. * <b>function:</b> 传入hql语句,查询对象 238. * @createDate 2010-8-2 下午05:49:31 239. * @author hoojo 240. * @param <T> 返回对象类型 241. * @param hql 查询的hql语句 242. * @return 对象T 243. * @throws Exception 244. */ 245. public <T> T get(String hql) throws Exception { 246. T ety = null; 247. try { 248. ety = (T) this.getSession().createQuery(hql).setMaxResults(1).uniqueResult(); 249. } catch (Exception e) { 250. throw new RuntimeException(e); 251. } 252. return ety; 253. } 254. 255. /** 256. * <b>function:</b> 通过hql语句查询List集合 257. * @createDate 2010-8-2 下午05:51:05 258. * @author hoojo 259. * @param hql 查询hql语句 260. * @return List<?> 261. * @throws Exception 262. */ 263. public <T> List<T> getList(String hql) throws Exception { 264. List<T> list = null; 265. try { 266. list = (List<T>) this.getHibernateTemplate().find(hql); 267. } catch (Exception e) { 268. throw new RuntimeException(e); 269. } 270. return list; 271. } 272. 273. /** 274. * <b>function:</b> 传入一个entity对象Class和Integer类型主键,返回该对象 275. * @createDate 2010-8-2 下午05:47:20 276. * @author hoojo 277. * @param <T> 返回、传入对象类型 278. * @param c 对象Class 279. * @param id 主键 280. * @return T 返回该类型的对象 281. * @throws Exception 282. */ 283. public <T> T getById(Class<T> c, Integer id) throws Exception { 284. T ety = null; 285. try { 286. ety = (T) this.getHibernateTemplate().get(c, id); 287. } catch (Exception e) { 288. throw new RuntimeException(e); 289. } 290. return ety; 291. } 292.
小灯光环 2014-07-11
  • 打赏
  • 举报
回复
hql语句的语法格式错误~ 你既然都用了hibernate支持类,就别拼hql语句了,直接set属性值,再调用getHibernateTemplate().update(obj)就行了~
shixitong 2014-07-11
  • 打赏
  • 举报
回复
String hql="update User set name=" + user.getName() + ",pass=" + user.getPass()
 + " where name=" + username;
User dbUser = userDAO.getUserByHql(hql);
这个是不是有问题?userDao 贴出来看看

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧