论坛首页 入门技术论坛

Spring,Struts2.0,Ibatis框架整合_2

浏览 2414 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-28   最后修改:2010-01-28
数据持久层部分,接口类,IUserDao.java:

/** 
	* File Name:IUserDao.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.dao.in;

import java.util.List;

import com.vo.User;


/**
 * Project Name:ZhiMing   ** Class Name:IUserDao 
 * Author:Musoon        ** Created Time:2010-1-27 下午05:03:35 
 * Changed By:Musoon   ** Changed Time:2010-1-27 下午05:03:35 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public interface IUserDao {

	public List<User> findAllUsers();
	
	public User findUserByKey(int id);
	
	public void deleteUser(User user);
	
	public void addUser(User user);
	
	public void updateUser(User user);
	
}



实现类,为了方便,这里用SqlMapClientDaoSupport,不过感觉用最原始的SqlClientMap更灵活一点。UserDao.java:

/** 
	* File Name:UserDao.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.dao.in.IUserDao;
import com.vo.User;


/**
 * Project Name:ZhiMing   ** Class Name:UserDao 
 * Author:Musoon        ** Created Time:2010-1-27 下午05:04:03 
 * Changed By:Musoon   ** Changed Time:2010-1-27 下午05:04:03 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public class UserDao extends SqlMapClientDaoSupport implements IUserDao {
	
	public void addUser(User user) {
		this.getSqlMapClientTemplate().insert("saveUser", user);
	}

	public void deleteUser(User user) {
		this.getSqlMapClientTemplate().delete("deleteUser", user);
	}

	public List<User> findAllUsers() {
		List<User> list = null;
		list = this.getSqlMapClientTemplate().queryForList("getAllUsers");
		return list;
	}

	public User findUserByKey(int id) {
		User user = null;
		user = (User) this.getSqlMapClientTemplate().queryForObject("findUserById", id);
		return user;
	}

	public void updateUser(User user) {
		this.getSqlMapClientTemplate().update("updateUser", user);
	}

}



业务逻辑层,接口类,IUserService.java:

/** 
	* File Name:UserService.java * Version: * Date:2010-1-4 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.service.in;

import java.util.List;

import com.vo.User;


/**
 * Project Name:ZhiMing   ** Class Name:UserService 
 * Author:Musoon        ** Created Time:2010-1-4 上午12:22:26 
 * Changed By:Musoon   ** Changed Time:2010-1-4 上午12:22:26 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public interface IUserService {

	public List<User> findAllUsers();
	
	public User findUserByKey(int id);
	
	public void deleteUser(User user);
	
	public void addUser(User user);
	
	public void updateUser(User user);
	
}



对应的实现类,UserServiceImpl.java:

/** 
	* File Name:UserServiceImpl.java * Version: * Date:2010-1-4 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.service.impl;

import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.dao.in.IUserDao;
import com.service.in.IUserService;
import com.vo.User;


/**
 * Project Name:ZhiMing   ** Class Name:UserServiceImpl 
 * Author:Musoon        ** Created Time:2010-1-4 上午12:24:03 
 * Changed By:Musoon   ** Changed Time:2010-1-4 上午12:24:03 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public class UserServiceImpl extends SqlMapClientTemplate implements
		IUserService {
	
	private IUserDao userDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void addUser(User user) {
		userDao.addUser(user);
	}

	public void deleteUser(User user) {
		userDao.deleteUser(user);
	}

	public List<User> findAllUsers() {
		return userDao.findAllUsers();
	}

	public User findUserByKey(int id) {
		return userDao.findUserByKey(id);
	}

	public void updateUser(User user) {
		userDao.updateUser(user);
	}

}



action部分,基类BaseAction.java:

/** 
	* File Name:BaseAction.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;



/**
 * Project Name:ZhiMing   ** Class Name:BaseAction 
 * Author:Musoon        ** Created Time:2010-1-27 下午06:45:35 
 * Changed By:Musoon   ** Changed Time:2010-1-27 下午06:45:35 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 7620009925942346125L;
	
	ActionContext context = ActionContext.getContext();
	HttpServletRequest request;
	HttpServletResponse response;
	SessionMap session;
	
	//获取request,response,session方式一,非IoC方式,不用实现SessionAware, ServletRequestAware, ServletResponseAware
	//ActionContext context = ActionContext.getContext();
	//HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
	//HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
	//Map session = context.getSession();
	//SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
	
	//获取request,response,session方式二,IoC方式,必须实现SessionAware, ServletRequestAware, ServletResponseAware
	public void setSession(Map map) {
		this.session = (SessionMap) map;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
}



UserAction.java:

/** 
	* File Name:UserAction.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.action;

import java.util.List;

import com.service.in.IUserService;
import com.vo.User;


/**
 * Project Name:ZhiMing   ** Class Name:UserAction 
 * Author:Musoon        ** Created Time:2010-1-27 下午05:24:13 
 * Changed By:Musoon   ** Changed Time:2010-1-27 下午05:24:13 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public class UserAction extends BaseAction{
		
	private static final long serialVersionUID = 3836787603947654532L;
	
	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String findAllUsers() throws Exception {
		
		List<User> userList = userService.findAllUsers();//查找所有用户
		//session.put("userList", userList);
		request.setAttribute("userList", userList);//把用户列表设进用户请求request中
		return SUCCESS;
	}
	
}


论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics