SSH实现的增删改查实例.doc_第1页
SSH实现的增删改查实例.doc_第2页
SSH实现的增删改查实例.doc_第3页
SSH实现的增删改查实例.doc_第4页
SSH实现的增删改查实例.doc_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SSH实现的增删改查实例 分类: ssh2010-10-01 17:124204人阅读评论(7)收藏举报一.整合步骤1. 通过MyEclipse向导,添加struts功能2. 通过MyEclipse向导,添加Hibernate3功能:生成会话工厂的那个步骤中一定要将那个对号要去掉,不能由hibernate来生成,而是交给Spring来生成;还有就是导入jar包的时候选择复制到lib目录下这一项。3. 通过MyEclipse向导,导入实现Spring功能,注意导入jar包的时候选择复制到lib目录下这一项。3. 利用MyEclipse反向工程的方法,以Spring生成dao对象的方式创建Hibernate DAO,相关POJO及其xxx.hbm.xml。4. DAO实现类加入Transactional标记。5. 修改applicationContext.xml文件,增加Spring事务管理、DAO等bean的配置。6. 编写action类。7. 在applicationContext.xml文件中添加Action的代理bean。8. 在struts的配置文件中,添加相应的Action,类名指向Spring中的代理bean,并加入和。9. 编写Jsp文件。10. 发布web项目。11. 启动web服务器,运行项目二.SSH实现关于书籍增删改查实例1.创建mysql数据库及其表create database book;create table book(id int not null primary key auto_increment,bookname varchar(30),bookauthor varchar(30);2.表现层(1)index.jsp(首页)java view plaincopyprint?1. 2. 3. 4. 5. 6. 欢迎 7. 8. 9. 查看书籍列表 10. 11. (2)list.jsp(书籍列表页面)c-sharp view plaincopyprint?1. 2. 3. 4. 9. 10. 11. 12. base href= 13. 书籍列表页面 14. 15. 16. 17. /* 给链接加入鼠标移过变色和去除下划线功能 */ 24. a:hover 25. color: red; 26. text-decoration: none 27. 28. 29. 30. 书籍列表页面 31. 32. 33. 34. 35. 36. 38. 39. 40. 书籍ID 41. 42. 43. 书籍名称 44. 45. 46. 作者 47. 48. 49. 价格 50. 51. 52. 操作 53. 54. 55.56. 57. 58. 59. $book.id 60. 61. 62. $book.bookname 63. 64. 65. $book.bookauthor 66. 67. 68. $book.bookprice 69. 70. 71. a href=/book.do?method=modifybook&id=$book.id修改 72. a href=/book.do?method=deletebook&id=$book.id删除 73. 74. 75. 76.77. 78. 79. 抱歉,没有找到相关的记录! 80. 81. a href=/new.jsp添加书籍 82. form action=/book.do?method=searchbook method=post onsubmit=return checkSearchForm(this); 83. 84. 85. 查找书籍 86. 87. 书籍名: 88. 89. 90. 91. 92. 93. (3)new.jsp(新增书籍页面)c-sharp view plaincopyprint?1. 2. 3. 4. 5. 6. 7. 添加书籍 8. 9. 10. 11. 12. 13. 14. 15. 添加书籍 16. 17. form action=/book.do?method=addbook onsubmit=return checkForm(this); method=post 18. 19. 20. 21. 22. 书籍名: 23. 24. 25.26. 27. 28. 29. 30. 31. 32. 作者: 33. 34. 35.36. 37. 38. 39. 40. 41. 价格: 42. 43. 44.45. 46. 47. 48. 49. 50.51. 52. 53. 54.55. 56. 57. 58. 59. 60. 61. input type=button onclick=document.location=/book.do?method=listbook; 62. value= 63. 返回列表 64. 65. (4)edit.jsp(书籍修改页面)c-sharp view plaincopyprint?1. 2. 3. 4. 5. 修改书籍 6. 7. 8. 9. 10. 11. 12. 13. 修改书籍 14. 15. form action=/book.do?method=updatebook onsubmit=return checkForm(this); method=post 16. 17. 18. 19. 20. 21. 书籍名: 22. 23. 24.25. 26. 27. 28. 29. 30. 31. 作者: 32. 33. 34.35. 36. 37. 38. 39. 40. 价格: 41. 42. 43.44. 45. 46. 47. 48. 49.50. 51. 52. 53.54. 55. 56. 57. 58. 59. 60. 61. 62. (5)error.jsp(错误公用页面)c-sharp view plaincopyprint?1. 2. 7. 8. 9. 10. base href= 11. 出错了! 12. 13. 14. 出错了! 15. 详细信息是: 16. $message 17. 返回 18. 19. (6)form.jsc-sharp view plaincopyprint?1. / 验证表单输入不为空的脚本代码 2. function checkForm(form) 3. if (form.bookname.value = ) 4. alert(书名不能为空!); 5. form.bookname.focus(); 6. return false; 7. 8. if (form.bookauthor.value = ) 9. alert(作者不能为空!); 10. form.bookauthor.focus(); 11. return false; 12. 13. if (form.bookprice.value = ) 14. alert(价格不能为空!); 15. form.bookprice.focus(); 16. return false; 17. 18. return true; 19. 20. function checkSearchForm(form) 21. if(form.bookname.value.match(/s*$/) 22. alert(查询条件不能为空!); 23. form.bookname.focus(); 24. return false; 25. 26. return true; 27. 3.公用类及其javabean(1)EncodingFilter.java(过滤器)c-sharp view plaincopyprint?1. package filter; 2. import java.io.IOException; 3. import javax.servlet.Filter; 4. import javax.servlet.FilterChain; 5. import javax.servlet.FilterConfig; 6. import javax.servlet.ServletException; 7. import javax.servlet.ServletRequest; 8. import javax.servlet.ServletResponse; 9. public class EncodingFilter implements Filter 10. protected FilterConfig config; 11. protected String Encoding = null; 12. public void init(FilterConfig config) throws ServletException 13. this.config = config; 14. this.Encoding = config.getInitParameter(Encoding); 15. 16. public void doFilter(ServletRequest request, ServletResponse response, 17. FilterChain chain) throws IOException, ServletException 18. if (request.getCharacterEncoding() = null) 19. if (Encoding != null) 20. request.setCharacterEncoding(Encoding); 21. response.setCharacterEncoding(Encoding); 22. 23. 24. chain.doFilter(request, response); 25. 26. public void destroy() 27. 28. (2)book.javac-sharp view plaincopyprint?1. package dao; 2. /* 3. * Book entity. author MyEclipse Persistence Tools 4. */ 5. public class Book implements java.io.Serializable 6. / Fields 7. private Integer id; 8. private String bookname; 9. private String bookauthor; 10. private Float bookprice; 11. / Constructors 12. /* default constructor */ 13. public Book() 14. 15. /* full constructor */ 16. public Book(String bookname, String bookauthor, Float bookprice) 17. this.bookname = bookname; 18. this.bookauthor = bookauthor; 19. this.bookprice = bookprice; 20. 21. / Property accessors 22. public Integer getId() 23. return this.id; 24. 25. public void setId(Integer id) 26. this.id = id; 27. 28. public String getBookname() 29. return this.bookname; 30. 31. public void setBookname(String bookname) 32. this.bookname = bookname; 33. 34. public String getBookauthor() 35. return this.bookauthor; 36. 37. public void setBookauthor(String bookauthor) 38. this.bookauthor = bookauthor; 39. 40. public Float getBookprice() 41. return this.bookprice; 42. 43. public void setBookprice(Float bookprice) 44. this.bookprice = bookprice; 45. 46. 4.DAO层BookDAO.javac-sharp view plaincopyprint?1. package dao; 2. import java.util.List; 3. import org.hibernate.LockMode; 4. import org.hibernate.Query; 5. import org.slf4j.Logger; 6. import org.slf4j.LoggerFactory; 7. import org.springframework.context.ApplicationContext; 8. import org.springframework.context.support.ClassPathXmlApplicationContext; 9. import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 10. import org.springframework.transaction.annotation.Transactional; 11. /* 12. * A data access object (DAO) providing persistence and search support for Book 13. * entities. Transaction control of the save(), update() and delete() operations 14. * can directly support Spring container-managed transactions or they can be 15. * augmented to handle user-managed Spring transactions. Each of these methods 16. * provides additional information for how to configure it for the desired type 17. * of transaction control. 18. * 19. * see dao.Book 20. * author MyEclipse Persistence Tools 21. */ 22. Transactional 23. public class BookDAO extends HibernateDaoSupport 24. private static final Logger log = LoggerFactory.getLogger(BookDAO.class); 25. / property constants 26. public static final String BOOKNAME = bookname; 27. public static final String BOOKAUTHOR = bookauthor; 28. public static final String BOOKPRICE = bookprice; 29. protected void initDao() 30. / do nothing 31. 32. public void save(Book transientInstance) 33. log.debug(saving Book instance); 34. try 35. getHibernateTemplate().save(transientInstance); 36. log.debug(save successful); 37. catch (RuntimeException re) 38. log.error(save failed, re); 39. throw re; 40. 41. 42. public void update(Book transientInstance) 43. log.debug(saving Book instance); 44. try 45. getHibernateTemplate().update(transientInstance); 46. log.debug(save successful); 47. catch (RuntimeException re) 48. log.error(save failed, re); 49. throw re; 50. 51. 52. public void delete(Book persistentInstance) 53. log.debug(deleting Book instance); 54. try 55. getHibernateTemplate().delete(persistentInstance); 56. log.debug(delete successful); 57. catch (RuntimeException re) 58. log.error(delete failed, re); 59. throw re; 60. 61. 62. public Book findById(java.lang.Integer id) 63. log.debug(getting Book instance with id: + id); 64. try 65. Book instance = (Book) getHibernateTemplate().get(dao.Book, id); 66. return instance; 67. catch (RuntimeException re) 68. log.error(get failed, re); 69. throw re; 70. 71. 72. public List findByExample(Book instance) 73. log.debug(finding Book instance by example); 74. try 75. List results = getHibernateTemplate().findByExample(instance); 76. log.debug(find by example successful, result size: 77. + results.size(); 78. return results; 79. catch (RuntimeException re) 80. log.error(find by example failed, re); 81. throw re; 82. 83. 84. public List findByProperty(String propertyName, Object value) 85. log.debug(finding Book instance with property: + propertyName 86. + , value: + value); 87. try 88. String queryString = from Book as model where model. 89. + propertyName + like = ; 90. return getHibernateTemplate().find(queryString, value); 91. catch (RuntimeException re) 92. log.error(find by property name failed, re); 93. throw re; 94. 95. 96. public List findByBookname(String bookname) 97. String sql=from Book where bookname like %+bookname+%; 98. Query query=this.getSession().createQuery(sql); 99. return query.list(); 100. 101. public List findByBookauthor(Object bookauthor) 102. return findByProperty(BOOKAUTHOR, bookauthor); 103. 104. public List findByBookprice(Object bookprice) 105. return findByProperty(BOOKPRICE, bookprice); 106. 107. public List findAll() 108. log.debug(finding all Book instances); 109. try 110. String queryString = from Book; 111. return getHibernateTemplate().find(queryString); 112. catch (RuntimeException re) 113. log.error(find all failed, re); 114. throw re; 115. 116. 117. public Book merge(Book detachedInstance) 118. log.debug(merging Book instance); 119. try 120. Book result = (Book) getHibernateTemplate().merge(detachedInstance); 121. log.debug(merge successful); 122. return result; 123. catch (RuntimeException re) 124. log.error(merge failed, re); 125. throw re; 126. 127. 128. public void attachDirty(Book instance) 129. log.debug(attaching dirty Book instance); 130. try 131. getHibernateTemplate().saveOrUpdate(instance); 132. log.debug(attach successful); 133. catch (RuntimeException re) 134. log.error(attach failed, re); 135. throw re; 136. 137. 138. public void attachClean(Book instance) 139. log.debug(attaching clean Book instance); 140. try 141. getHibernateTemplate().lock(instance, LockMode.NONE); 142. log.debug(attach successful); 143. catch (RuntimeException re) 144. log.error(attach failed, re); 145. throw re; 146. 147. 148. public static BookDAO getFromApplicationContext(ApplicationContext ctx) 149. return (BookDAO) ctx.getBean(BookDAO); 150. 151. public static void main(String args) 152. ApplicationContext ctx = 153. new 154. ClassPathXmlApplicationContext(applicationContext.xml); 155. BookDAO dao = (BookDAO)ctx.getBean(BookDAO); 156. Book book = new Book(); 157. book.setBookname(数学); 158. book.setBookauthor(张三); 159. book.setBookprice(12.0f); 160. dao.save(book); 161. 162. 5.service层(1)IBookManager.java(接口)c-sharp view plaincopyprint?1. package service; 2. import java.util.List; 3. import dao.Book; 4. public interface IBookManager 5. /* 6. * 根据ID查找用户信息。 7. * 8. * param id 9. * 用户编号 10. * return 找到的用户对象,找不到时返回null 11. */ 12. public Book findById(int id); 13. /* 14. * 更新用户对象。 15. * 16. * param Book 17. * 被更新的用户 18. * return 更新成功与否 19. */ 20. public boolean update(Book Book); 21. public boolean save(Book Book); 22. /* 23. * 删除用户对象。 24. * 25. * param Book 26. * 被删除的用户 27. * return 删除成功与否 28. */ 29. public boolean delete(Book Book); 30. /* 31. * 根据用户名查找用户。 32. * 33. * param username 34. * 用户名 35. * return 包含此用户名的用户列表 36. */ 37. public List findByBookname(String username); 38. public List findAll(); 39. (2)BookManager.java(实现类)c-sharp view plaincopyprint?1. package service; 2. import java.util.List; 3. import org.springframework.context.ApplicationContext; 4. import org.springframework.context.support.ClassPathXmlApplicationContext; 5. import dao.Book; 6. import dao.BookDAO; 7. public class BookManager implements IBookManager 8. private BookDAO bookdao; 9.10. public boolean delete(Book book) 11. try 12. bookdao.delete(book); 13. return true; 14. catch (Exception e) 15. 16. return false; 17. 18. public Book findById(int id) 19. return bookdao.findById(id); 20. 21. public List findAll() 22. return bookdao.findAll(); 23. 24. public List findByBookname(String bookname) 25. return bookdao.findByBookname(bookname); 26. 27. public boolean update(Book Book) 28. try 29. bookdao.update(Book); 30. return true; 31. catch (Exception e) 32. 33. return false; 34.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论