SSH2_增删查改实例_第1页
SSH2_增删查改实例_第2页
SSH2_增删查改实例_第3页
SSH2_增删查改实例_第4页
SSH2_增删查改实例_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

1、SSH2 增删查改实例0)软件和框架版本:MyEclipse 6.0.1Tomcat 6.0.14Sturts2 1.8Hibernate 3Spring 2.0Mysql 5.1一)引入包 (共 73 个,不一定都需要,但是我的项目是这么多,经过调试,有没包冲突)1,010KBantantlrl, 6. 5. j ar6KBa ant-juni tT6 5. jar73KBantlauncher 1.6. 5, jar9KBantlr2. 7. 6. jar434KB/ antswing-l. 6.5. j er7KBaopalliance. jar5KBa . j ar26KBa as(r

2、*attrs. jar17KBbo5cygstl. j ar1,429KB/ c3p00. 9. 0. jar476KBcglib*2, L 3. jar276KBcheckstyleall, jar1,177KB/ cleaninporls. jar140KB显 conmonscollecti ons*2. 1. 1. j ar172KBcomons-dbcpL 2. 2. jar119KB/ coffmons*11 eupload!. 2. L jar57KBcc(rmons-xc-l. 3. 2. jar86KB/ cwffionsloggingrl 0. 4. j ar38KBcomo

3、nspooll. 5. 4. jar94KB4corcurrent*!, 3. 2, jar168KScorrector- j ar18KBdo4jl.6. 1. jar307KBa ehcachel. 2. jar116KBfree(ftarker-2. 3. 15. jar859KBahileriLateS. jar2,128KBjM jais. jar102KBjaccl_0-r. jar24KBjavabase64 1.3. 1. jar5KBjavassist. j ar449KBjanenl. lbeta7. jar222KB112465739173013969881211,521

4、KBKBKBKBKBKBKBKBKBKB50657057821971,574405219119343716235314523054164799357157263711813266022638016939531jboss-cache, j arjboss-commoit j arIjbossjmx. j arjboss-system, j ar jdbc2_0-stdext. jarm jgroups2. 2. 8. jarjstl-1.2. jarjarjta. jarjuitit3 8. L jar!log4jT 2. 11. jarmysqlconnectorjava5. 1. 12bin

5、. jarognl2. 7. 3- jarZ org. springfraniework. aop3. 0. 3. RELEASE, j ar org. springframework. asm3- 0. 3. RELEASE, jar org. springframework. aspects3. 0. 3 RELEASE, jar org. springfraniework. beaiis3. 0. 3. RELEASE, jar org. spr i ngfr am ew ork. cont ex t3. 0. 3. RELEASE, jar org. springframework.

6、context. support3. 0- 3. RELEASE, j ar org. springframework. core3. 0, 3. RELEASE, jar org. springframework. expression3. 0. 3. RELEASE, jar org. springframework. instrwnent*3. 0. 3. RELEASE, jar org. springframework. instrument. tomcat3. 0. 3. RELEASE, j ar org. springframework. jdbc3. 0. 3. RELEAS

7、E, jar org. springframework. jms3. 0. 3. RELEASE, j arIorg. springframework. orm3. 0. 3. RELEASE, jar org springfraniework. oxm3 0. 3. RELEASE, jar org. springframework. transactson3. 0, 3. RELEASE, jar org. springframework. web3 0. 3. RELEASE, jar org. springfrainework, web. portlet3. 0. 3. RELEASE

8、, j ar org. springframework, web. servlet3. 0. 3. RELEASE, jar org. springframework. web. struts3- 0- 3. RELEASE, jar 上j osc負ch2. L jar proxoolY) 8. 3. jarstruts2-core2,1.8,1.jarstrats2,prinrplugxn-2. 18.1 jarswarmcachel. 0rc2, j arsyndi 型2. jar ver si oncheck, j ar xrces-2 6.2. jar J xml 令pis, jar

9、xyorPcoy2 1.6. jar二)创建数据库表建立数据库octtest,并创建user表,表里面一共4个字段:id,姓,名,年龄。语句如下:create database octtest;user octtest;DROP TABLE IF EXISTS users;CREATE TABLE users (id int(10) unsigned NOT NULL,firstname varchar(50) NOT NULL,lastname varchar(50) NOT NULL,age int(10) unsigned NOT NULL,PRIMARY KEY (id) ENGINE

10、=InnoDB DEFAULT CHARSET=utf8;(三)用 myeclipse 中自带的 hibernate reverse engineering 工具,根据刚才创建的 user 表, 自动生成实体 User 和相应的User.hbm.xml 配置文件。1)在 myeclipse 中的 视图中创建连接数据库连接,如下图:R Database DriverEdit Database Connection DriverEdit the uennection driverVDriver template.Driver name.MySQL Connectior/JabcConnection

11、 VKL;User jisme :Fas sword:Driver JAKsj ibclmysql ;/#*#G: I开发包iDo胃nJarElinFsqligniiEctQr=jw5- 1. lE=biiL. jarAM JARS RemoveDriver classnamel com.mysql. jdtc. DriverTest Driver Cciukect tcg lyEclifse startup回 3妙a password(I Savdsswords are stored on your computer in & file that* s di iCTilt, butnot i

12、inpossible for an intruder to red.Kext I Finish 填入mysql数据库相应的路径,用户名,密码等信息。注意:这里要引入 mysql-connector-java的jar包,需要从外部导入。2)连接数据库正常后,选择octtest数据库中的user表,右键-hibernate reverse engineering。自动生成实体 User和相应的User.hbm.xml配置文件。(正常情况下可以生成,但是笔者生成engineer有问题,所以只能够手动创建 User类和相应的User.hbm.xml,所以如果读者顺利创建,可跳过 2a,2b两部)2a)

13、创建User实体类User.javaP ackage com.bean;p ublic class User p ublic Integer id;p ublic String firstname;p ublic String lastname;p ublic int age;p ublic Integer getld() return id;p ublic void setld(lnteger id) this.id = id;p ublic String getFirstname() return firstname;p ublic void setFirstname(String fir

14、stname) this.firstname = firstname;p ublic String getLastname() return lastname;p ublic void setLastname(String lastname) this.lastname = lastname;p ublic int getAge() return age;p ublic void setAge(int age) this.age = age;2b)创建User实体类对应的映射文件,这个映射文件作用是告诉hibernate要把这个User对象和数据库中的user表联系起来。User.hbm.xm

15、l:v?xml version=1.0 encoding=utf-8?v!DOCT YPEhibernate-ma ppingP UBLIC-/Hibernate/Hibernate Mapp ing DTD 3.0/EN /hibernate-mapping-3.0.dtd vhibernate-ma ppi ng vclass name=com.bean.User table=usersvid name=id typ e=java.lang.lnteger column=id vgenerator class=increment

16、/v/idvprop ertyname=firstnametyp e=stringcolumn=firstname/vprop erty name=lastname typ e=string column=lastname/vprop ertyname=agetyp e=java.lang.lntegercolumn=age/三)编写底层 dao 操作:对 User 实体操作,主要还是 增删查改 4 个。养成好习惯,无论是 dao 操作还是业务逻辑,最好都先用接口写好要用到的方法,再用实现类去实现。dao 接口:package com.dao;import java.util.List;imp

17、ort com.bean.User;public interface UserDAO public void saveUser(User user);public void removeUser(User user);public User findUserById(Integer id);public List findAllUser();public void updateUser(User user);dao 实现:package com.dao.impl;import java.util.List;importorg.springframework.orm.hibernate3.sup

18、port.HibernateDaoSupport;import com.bean.User;import com.dao.UserDAO;public class UserDAOImpl extends HibernateDaoSupport implements UserDAO SuppressWarnings(unchecked)public List findAllUser() String hql = from User user order by user.id desc;return (List) this.getHibernateTemplate().find(hql);vwel

19、come-file-listUser user = (User)this.getHibernateTe mpl ate().get(User.class,id);return user;p ublic void removeUser(User user) this.getHibernateTe mpl ate().delete(user);p ublic void saveUser(User user) this.getHibernateTe mpl ate().save(user);p ublic void up dateUser(User user) this.getHibernateTe

20、 mpl ate(). up date(user);(四)编写 struts.xmlapplicationContext.xmlweb.xml 配置文件。其中, web.xml 文件是可以一次写完。struts.xml 和app licati onCon text.xml需要注意保存路径。web.xml :(保存路径是:项目名 /WebRoot/WEB-INF ) struts2 org.a pache.struts2.dis patcher.FilterDis patcher struts2 /*v/url-pattern org.s pringframework.web.context.C

21、ontextLoaderListener vwelcome-fileindex.js pv/welcome-filev/web-a ppapplicationContext.xml:(保存路径是:泾x项目名/WebRoot/WEB-INF ),这个是默认路径v?xml version=1.0 encoding=UTF-8? vbeans xmln s=http: /www.s pringframeworkag/schema/beans xmln s:xsi=http: //2001/XMLSchema-instance xsi:schemaLocati on=http: /

22、www.s /schema/beans htt p: //schema/beans/s pnng-beans-2.0.xsd vbean id=dataSource class=org.a mons.dbc p.BasicDataSource destroy-method=closevprop erty name=driverClassName value=com.mysql.jdbc.Driverv/prop erty vprop erty name=url value=jdbc:mysql:

23、/localhost:3306/octtestv/prop erty vprop erty name=username value=rootv/prop ertyvprop erty name=p assword value=mysqlv/prop erty vprop erty name=maxActive value=100v/prop ertyvprop erty name=maxIdlevalue=30v/prop erty vprop erty name=maxWait value=500v/prop ertyv/bean vbean id=sessionFactory class=

24、org.s pringframework.orm.hibernate3.LocalSessionFactoryBeanvprop erty name=dataSource ref=dataSourcev/prop erty vprop erty name=hibernate Prop ertiesvpropsvprop key=hibernate.dialectorg.hibernate.dialect.MySQLDialectv/propvprop key=hibernate.show_sqltruev/propv/props v/prop ertyvprop erty name=ma pp

25、 ingResourcesvlistvvaluecom/bean/User.hbm.xmlv/valuev!-vvaluecom/gzjs/up datewa pi/entity/As_Active_CertBean.hbm.xmlv/valuevvaluecom/gzjs/up datewa pi/entity/Cert_DataBean.hbm.xmlv/valuevvaluecom/gzjs/up datewa pi/entity/Revoke_CertBean.hbm.xmlv/valuevvaluecom/gzjs/up datewa pi/entity/User.hbm.xmlv/

26、valuev/list v/prop erty struts.xml :(保存路径是:项目名 /WebRoot/WEB-INF/classes )v?xml version=1.0 encoding=UTF-8?v!DOCT YPE struts P UBLIC-/A pacheSoftware Foundation/DTD Struts Configuration2.0/ENhtt p:/struts.a /dtds/struts-2.0.dtdvp ackage name=user extends=struts-default/listUser.actionv/resul

27、t/save.js pv/resultvaction name=listUser class=listUserActionvresult name=success/listUser.js pv/resultv/actionvactionname=showUserRecordclass=showUserRecordActionvresult name=success/showRecord.js pv/resultv/actionvaction name=up dateUser class=up dateUserActionvresultname=successtyp e=redirect/lis

28、tUser.actionv/resultvresult name=i npu t/showRecord.js pv/resultv/actionvaction name=deleteUser class=deleteUserActionvresultname=successtyp e=redirect/listUser.actionv/resultv/actionv/p ackagev/struts(五)编写action类,有两种方法,一种是 crud每种操作一个action,另一种是crud集成到一个UserAction中。本文采用第一种。SaveUserActi on:p ackage c

29、om.action;import com.bean.User;import com .op ensy mp hony.xwork2.ActionS upport;import com.service.UserService;public class SaveUserAction extends ActionSupport private User user;private UserService userService;public User getUser() return user;public void setUser(User user) this.user = user;Overri

30、de public String execute() throws Exception this.userService.save(this.user);return SUCCESS;public UserService getUserService() return userService;public void setUserService(UserService userService) this.userService = userService;ListUserAction:package com.action;import java.util.List;import com.bea

31、n.User;import com.opensymphony.xwork2.ActionSupport;import com.service.UserService;public class ListUserAction extends ActionSupport private List list;Override public String execute() throws Exception private UserService userService;list = userService.findAllUser();return SUCCESS;public UserService

32、getUserService() return userService;public void setUserService(UserService userService) this.userService = userService;public List getList() return list;public void setList(List list) this.list = list;DeleteUserAction:package com.action;import com.bean.User;import com.opensymphony.xwork2.ActionSuppo

33、rt;import com.service.UserService;public class DeleteUserAction extends ActionSupport private User user;private UserService userService;public User getUser() return user;public void setUser(User user) this.user = user;Override public String execute() throws Exception return SUCCESS;public UserServic

34、e getUserService() return userService;this.userService.delete(this.user);public void setUserService(UserService userService) this.userService = userService;ShowUserRecordAction :package com.action;import com.bean.User;import com.opensymphony.xwork2.ActionSupport;import com.service.UserService;public

35、 class ShowUserRecordAction extends ActionSupport private User user;private UserService userService;public User getUser() return user;public void setUser(User user) this.user = user;Override public String execute() throws Exception user = this.userService.findById(user.getId();return SUCCESS;public

36、UserService getUserService() return userService;public void setUserService(UserService userService) this.userService = userService;UpdateUserAction :package com.action;import com.bean.User;import com.opensymphony.xwork2.ActionSupport;import com.service.UserService;public class UpdateUserAction exten

37、ds ActionSupport private User user;private UserService userService;public User getUser() return user;public void setUser(User user) this.user = user;Override public String execute() throws Exception this.userService.update(user);return SUCCESS;public UserService getUserService() return userService;public void setUserService(UserService userService) this.userService = userService;(七) 编写业务逻辑service 接口:package com.service;import java.util.List;import com.bean.User;public List findAllUser();public void save(User user);pub

温馨提示

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

评论

0/150

提交评论