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整合实例二(实例代码部分)的更多相关文章

  1. springMVC+Hibernate4+Spring整合一(配置文件部分)

    本实例采用springMvc hibernate 与 spring 进行整合, 用springmvc 取代了原先ssh(struts,spring,hibernate)中的struts来扮演view层 ...

  2. activiti自定义流程之Spring整合activiti-modeler5.16实例(二):创建流程模型

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 1.maven导包,这里就没有什么多的好说了,直接代码: <depe ...

  3. activiti自定义流程之Spring整合activiti-modeler5.16实例(九):历史任务查询

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  4. activiti自定义流程之Spring整合activiti-modeler5.16实例(八):完成个人任务

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  5. activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  6. activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  7. activiti自定义流程之Spring整合activiti-modeler5.16实例(五):流程定义列表

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  8. activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

  9. activiti自定义流程之Spring整合activiti-modeler5.16实例(三):流程模型列表展示

    注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建        (2)创建流程模型:activiti自定义流程之Spring ...

随机推荐

  1. [ZJOI 2007]Hide 捉迷藏

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条双 ...

  2. [SDOI 2008]Cave 洞穴勘测

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  3. [HAOI2009]逆序对数列

    题目描述 对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样 ...

  4. bzoj 2437: [Noi2011]兔兔与蛋蛋

    Description Solution 考虑犯错误的条件:之前是处于必胜状态,该操作之后就变成了必败状态. 我们可以把这个过程看成两人对网格图进行黑白染色,变成了一个二分图模型,即当前位置向相邻不同 ...

  5. ●BZOJ 1096 [ZJOI2007]仓库建设

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1096 题解: 斜率优化DP $(d_i:i 位置到1位置的距离,p_i:i位置的成品数量,c ...

  6. ●BZOJ 1969 [Ahoi2005]LANE 航线规划

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1969 题解: 线段树,树链剖分,反向考虑思路是很巧妙,但是感觉代码真的恶心.. 反着考虑,先 ...

  7. NOIP 2012

    Prob.1 vigenere密码 模拟代码: #include<cstdio> #include<cstring> #include<iostream> usin ...

  8. 基于SSM框架实现简单的登录注册

    一.环境配置 工程目录 在pom.xml添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  9. JAX-RPC 与 JAX-WS 的比较

    引言 Web 服务已经出现很久了.首先是 SOAP,但 SOAP 仅描述消息的情况,然后是 WSDL,WSDL 并不会告诉您如何使用 Java™ 编写 Web 服务.在这种情况下,JAX-RPC 1. ...

  10. JavaScript 题目(作用域)

    var length = 10 function fn(){ alert(this.length) } var obj = { length: 5, method: function(fn) { fn ...