Hibernate+Spring+Struts2+ExtJS开发CRUD功能_第1页
Hibernate+Spring+Struts2+ExtJS开发CRUD功能_第2页
Hibernate+Spring+Struts2+ExtJS开发CRUD功能_第3页
Hibernate+Spring+Struts2+ExtJS开发CRUD功能_第4页
Hibernate+Spring+Struts2+ExtJS开发CRUD功能_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

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

文档简介

1、 Hibernate+Spring+Struts2+ExtJS开发CRUD功能 新一篇: 学习Hibernate JPA 1、 入门:各种开源框架环境及下载:Hibernate:3.x / 需要hibernate core 和annotations 包。Spring:2.x / Struts2:2.x /2.x/ ExtJS:2.X / JSON:JSON可以到/ 查看详细内容,这里使用j

2、son-lib / 本所需要的包: 2、 配置:(1)首先是配置web.xml,配置方法可以在下面的配置文件代码注释中查看,这里主要是Struts2的配置: struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /*和Spring的配置: contextConfigLocation /WEB-INF/spring/*.xml org.springframework.web.context.ContextLoaderListenerWeb.xml的全部文件: str

3、uts2 contextConfigLocation /WEB-INF/spring/*.xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorClean

4、upListener 10 index.html index.htm index.jsp default.html default.htm default.jsp (2)Hibernate配置: oracle.jdbc.driver.OracleDriver MY MY MY jdbc:oracle:thin:localhost:1521:loon org.hibernate.dialect.Oracle9Dialect true (3)Spring基本配置:配置文件应该在WEB-INF/spring/下面 classpath:hibernate.cfg.xml !- 配置多个hibernat

5、e.cfg.xml classpath:hibernate_admin1.cfg.xml classpath:hibernate_admin2.cfg.xml - PROPAGATION_REQUIRED,readOnly PROPAGATION_REQUIRED,-Exception PROPAGATION_REQUIRED,-Exception !- PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQU

6、IRED,readOnly PROPAGATION_REQUIRED,readOnly PROPAGATION_REQUIRES_NEW - (4)struts.xml文件的配置: /resource/json_struts2.jsp /resource/json_struts2.jsp 3、 建立的项目目录:Root:/resource/ext2.0/ 将下载的ext-2.0-beta1.zip文件解压到该目录/WEB-INF/web.xml/WEB-INF/lib/WEB-INF/classes/struts.xml/WEB-INF/spring/applicationContext.xm

7、l4、 代码清单: Level.java import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table; EntityTable(name = LOON_LEVEL)public class Level implements java.io.Serializable private Long levelid; private String levelname; private String description

8、; public Level() public Level(Long levelid) this.levelid = levelid; public Level(Long levelid, String levelname, String description) this.levelid = levelid; this.levelname = levelname; this.description = description; Id Column(name = LEVELID, unique = true, nullable = false, precision = 5, scale = 0

9、) public Long getLevelid() return this.levelid; public void setLevelid(Long levelid) this.levelid = levelid; Column(name = LEVELNAME, length = 64) public String getLevelname() return this.levelname; public void setLevelname(String levelname) this.levelname = levelname; Column(name = DESCRIPTION, len

10、gth = 256) public String getDescription() return this.description; public void setDescription(String description) this.description = description; ILevelDAO.java import java.util.List;public interface ILevelDAO public Level findLevelById(Long id); public List findAllLevels(); public void persistLevel

11、(Level level); public void removeLevel(Level level); public void removeById(Long id); LevelDAO.java import java.util.List;import org.hibernate.Session;import org.springframework.orm.hibernate3.HibernateCallback;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class LevelD

12、AO extends HibernateDaoSupport implements ILevelDAO public LevelDAO() super(); public Level findLevelById(Long id) return (Level) getHibernateTemplate().get(Level.class, id); public List findAllLevels() return getHibernateTemplate().loadAll(Level.class);/ .find(from Level o);/ public void persistLev

13、el(Level level) getHibernateTemplate().saveOrUpdate(level); public void removeLevel(Level level) getHibernateTemplate().delete(level); public void removeById(final Long id) this.getHibernateTemplate().execute(new HibernateCallback() public Object doInHibernate(Session session) session.createQuery(de

14、lete from Level o where o.levelid= + id + ).executeUpdate(); return 1; ); ILevelService.java import java.util.List;public interface ILevelService public Level findLevelById(Long id) throws Exception; public List findAllLevels() throws Exception; public List findLevelsByExample(Level level) throws Ex

15、ception; public void persistLevel(Level level) throws Exception; public void removeLevel(Level level) throws Exception;public void removeLevelById(Long id) throws Exception; LevelService.java import java.util.List;import privilege.dao.*;import privilege.database.Level;import org.springframework.cont

16、ext.ApplicationContext;public class LevelService implements ILevelService private ILevelDAO dao; private static final String SERVICE_BEAN_ID = LevelService; public LevelService() super(); public static ILevelService getInstance(ApplicationContext context) return (ILevelService) context.getBean(SERVI

17、CE_BEAN_ID); public Level findLevelById(Long id) throws Exception try return getDao().findLevelById(id); catch (RuntimeException e) throw new Exception(findLevelById failed with the id + id + : + e.getMessage(); public void persistLevel(Level level) throws Exception try getDao().persistLevel(level);

18、 catch (RuntimeException e) throw new Exception(persistLevel failed: + e.getMessage(); public void removeLevel(Level level) throws Exception try getDao().removeLevel(level); catch (RuntimeException e) throw new Exception(removeLevel failed: + e.getMessage(); public void removeLevelById(Long id) thro

19、ws Exception try getDao().removeById(id); catch (RuntimeException e) throw new Exception(removeLevel failed: + e.getMessage(); public void setDao(ILevelDAO dao) this.dao = dao; public ILevelDAO getDao() return this.dao; ExtJSONActionSuport.java辅助类,继承了ActionSupportimport com.opensymphony.xwork2.Actio

20、nSupport; public class ExtJSONActionSuport extends ActionSupport private int totalCount = 0;/ 总数 private transient int start = 0;/ 开始数 private transient int limit = 0;/ 限制数量 private String jsonString = ; public String getJsonString() return jsonString; public void setJsonString(String jsonString) th

21、is.jsonString = jsonString; public String jsonExecute() throws Exception return super.execute(); public int getTotalCount() return totalCount; public void setTotalCount(int totalCount) this.totalCount = totalCount; public int getStart() return start; public void setStart(int start) this.start = star

22、t; public int getLimit() return limit; public void setLimit(int limit) this.limit = limit; LevelAction.java import java.util.List;import java.util.ArrayList;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import net.sf.json.JSONArray;import privilege.database.Lev

23、el;import privilege.service.*;import commons.utils.action.ExtJSONActionSuport; public class LevelAction extends ExtJSONActionSuport private static final long serialVersionUID = 1L; private Level level = null; private List levels = new ArrayList(0); private ILevelService levelService = null; private

24、String delData; public String execute() return this.SUCCESS; Override public String jsonExecute() throws Exception if (this.getDelData() != null & !.equals(this.getDelData() if (this.getDelData().indexOf(,) 0) this.levelService.removeLevelById(Long.parseLong(this.getDelData(); System.out.println(del

25、_id: + getDelData(); else String id = this.getDelData().split(,); for (int i = 0; i id.length; i+) System.out.println(del: + idi); this.levelService.removeLevelById(Long.parseLong(idi); HttpSession session = ServletActionContext.getRequest().getSession(); Object o = null;/ session.getAttribute(Level

26、_Data1); if (o = null) try this.levels = this.getLevelService().findAllLevels(); session.setAttribute(Level_Data1, this.levels); System.out.println(query database); catch (Exception e) e.printStackTrace(); else this.setLevels(List) o); this.setTotalCount(this.levels.size(); JSONArray array = JSONArr

27、ay.fromObject(this.levels); / System.out.println(this.getStart() + - + this.getLimit(); this.setJsonString(success:true,totalCount : + this.getTotalCount() + , list: + array.toString() + ); / System.out.println(this.getJsonString(); return super.jsonExecute(); /* * Find an entity by its id (primary

28、key). * * param id * return The found entity instance or null if the entity does not exist. */ public String findLevelById(Long id) try this.level = this.getLevelService().findLevelById(id); catch (Exception e) e.printStackTrace(); JSONArray array = JSONArray.fromObject(this.levels); this.setJsonStr

29、ing(array.toString(); return SUCCESS; public String findLevelById() System.out.println(this.level.getLevelid(); try this.level = this.getLevelService().findLevelById(this.level.getLevelid(); catch (Exception e) e.printStackTrace(); JSONArray array = JSONArray.fromObject(this.level); this.setJsonStri

30、ng(array.toString(); this.setJsonString(success:true,totalCount:1,list: + array.toString() + ); System.out.println(array.toString(); return SUCCESS; /* * return Return all persistent instances of the Level entity. */ public String getAllLevels() try this.levels = this.getLevelService().findAllLevels

31、(); catch (Exception e) e.printStackTrace(); return SUCCESS; /* * Make the given instance managed and persistent. * * return */ public String persistLevel() System.out.println(this.level.getLevelid() + - + this.level.getLevelname() + - + this.level.getDescription(); this.setJsonString(success:true); try this.getLevelService().persi

温馨提示

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

评论

0/150

提交评论