springMVC+Hibernate4+spring整合实例二(实例代码部分)
UserController.java 代码:
package com.edw.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.edw.entity.User; import com.edw.service.IUserManager; @Controller //类似Struts的Action @RequestMapping("/user") public class UserController { @Resource(name="userManager") // 获取spring配置文件中bean的id为userManager的,并注入 private IUserManager userManager; @RequestMapping("/addUser") // 请求url地址映射,类似Struts的action-mapping public String addUser(HttpServletRequest request){ System.out.println(request.getParameter("name").trim()); if(userManager.addUser(new User(request.getParameter("name").trim()))) { // 重定向 return "redirect:/user/getAllUser"; }else { return "/fail"; } } @RequestMapping("/updateUser") public String updateUser(User user,HttpServletRequest request){ //String result = "{\"result\":\"error\"}"; if (userManager.updateUser(user)) { user = userManager.getOneUser(user); request.setAttribute("user", user); //result = "{\"result\":\"success\"}"; return "/UserEdit"; }else { return "/fail"; } } @RequestMapping("/delUser") public void delUser(User user,HttpServletResponse response){ String result = "{\"result\":\"error\"}"; if(userManager.delUser(user)){ result = "{\"result\":\"success\"}"; } PrintWriter out = null; response.setContentType("application/json"); try { out = response.getWriter(); out.write(result); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping("/toAddUser") public String toAddUser(){ return "/UserAdd"; } @RequestMapping("/toUpdateUser") public String toUpdateUser(User user,HttpServletRequest request){ User user1=userManager.getOneUser(user); request.setAttribute("user1", user1); return "/UserEdit"; } @RequestMapping("/getAllUser") public String getAllUser(HttpServletRequest request){ List userList=userManager.getAllUser(); request.setAttribute("userlist", userList); return "/UserMain"; } }
<pre name="code" class="java"><strong><span style="color:#ff0000;">UserDao.java</span> 代码:</strong>
<pre name="code" class="java">
package com.edw.dao; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.edw.entity.User; @Repository public class UserDao implements IUserDao { private SessionFactory sessionFactory; @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public User getUser(long i) { String hql = "from User u where u.id=?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setLong(0, i); return (User) query.uniqueResult(); } @Override public List<User> getAllUser() { String hql = "from User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); return query.list(); } @Override public boolean addUser(User user) { try{ sessionFactory.getCurrentSession().save(user); }catch(Exception e){ e.printStackTrace(); return false; } return true; } @Override public boolean delUser(long id) { String hql = "delete User u where u.id = ?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setLong(0, id); return (query.executeUpdate() > 0); } @Override public boolean updateUser(User user) { String hql = "update User u set u.name = ? where u.id = ?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setString(0, user.getName()); // query.setString(1, user.getAge()); query.setLong(1, user.getId()); return (query.executeUpdate() > 0); } }
User.java 代码:
package com.edw.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { private int id; private String name; public User(){ } public User(String name){ this.name = name; } @GeneratedValue @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
UserManager.java 代码:
package com.edw.service; import java.util.List; import javax.annotation.Resource; import com.edw.dao.UserDao; import com.edw.entity.User; public class UserManager implements IUserManager{ private UserDao userDao; @Resource public void setUserDao(UserDao userDao){ this.userDao = userDao; } @Override public boolean updateUser(User user) { return userDao.updateUser(user); } @Override public User getOneUser(User user) { return userDao.getUser(user.getId()); } @Override public boolean delUser(User user) { return userDao.delUser(user.getId()); } @Override public boolean addUser(User user) { return userDao.addUser(user); } @Override public List getAllUser() { return userDao.getAllUser(); } }
jsp页面部分就不一一给出了,需要的亲可以去 http://download.csdn.net/detail/edwards_june/7876119 下载。。。
总结:
本人也是刚起步学SpringMvc,先前的SSH项目真心庞大,struts中包含了大量无关的jar包,springMVC中基本能胜任,也大大减轻了项目大小,不过也不能认为struts不好,毕竟任何事都有它好的一面,只在与我们在项目中需要什么和谁能提供较好的支持--权衡与折中.本次项目搭建中如有不足之处,希望大家可以提出一起学习,谢谢!!!
springMVC+Hibernate4+spring整合实例二(实例代码部分)的更多相关文章
- springMVC+Hibernate4+Spring整合一(配置文件部分)
本实例采用springMvc hibernate 与 spring 进行整合, 用springmvc 取代了原先ssh(struts,spring,hibernate)中的struts来扮演view层 ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(二):创建流程模型
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 1.maven导包,这里就没有什么多的好说了,直接代码: <depe ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(九):历史任务查询
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(八):完成个人任务
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(五):流程定义列表
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(三):流程模型列表展示
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
随机推荐
- [POI 2015]Kinoman
Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...
- UVA 5009 Error Curves
Problem Description Josephina is a clever girl and addicted to Machine Learning recently. She pays m ...
- LOJ #6041. 事情的相似度
Description 人的一生不仅要靠自我奋斗,还要考虑到历史的行程. 历史的行程可以抽象成一个 01 串,作为一个年纪比较大的人,你希望从历史的行程中获得一些姿势. 你发现在历史的不同时刻,不断的 ...
- hdu 4453 splay
Looploop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 4123 树的最长路+RMQ
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [BZOJ]1047 理想的正方形(HAOI2007)
真·水题.小C本来是不想贴出来的,但是有一股来自东方的神秘力量催促小C发出来. Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和 ...
- bzoj1597[Usaco2008 Mar]土地购买 斜率优化dp
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5524 Solved: 2074[Submit] ...
- bzoj2005 NOI2010 方案统计
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 4387 Solved: 2619[Submit][Statu ...
- POJ 放苹果问题(递归)
首先我们想象有一个函数count f(m,n)可以把m个苹果放到n个盘子中. 根据 n 和 m 的关系可以进一步分析: 特殊的m <=1|| n <= 1时只有一种方法: 当 m < ...
- JConsole/JvisualVM 远程连接失败处理
今天在使用JConsole进行远程连接时,发现IP和端口在Windows下是可以远程telnet的,但是,使用JConsole时却无法连接. 我的环境如下: Windows下运行JConsole,准备 ...