当前位置: 代码迷 >> JavaScript >> Struts2+Spring2.5+ibatis+ExtJS3.1 基于注脚框架搭建(4)
  详细解决方案

Struts2+Spring2.5+ibatis+ExtJS3.1 基于注脚框架搭建(4)

热度:766   发布时间:2012-10-30 16:13:36.0
Struts2+Spring2.5+ibatis+ExtJS3.1 基于注解框架搭建(4)
1、创建实体类StafferInfoBean.xml  StafferInfoBean.java

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="common">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="stafferInfoBean" type="com.huawei.boss.entity.StafferInfoBean"/>
   
      
  <select id="getCurrFormatDate" parameterClass="java.lang.String" resultClass="java.lang.String">
   <![CDATA[
select to_char(sysdate,#dateFormat#) from dual
]]>
  </select>

  <!-- 登录获取用户 -->
  <select id="getLoginUser" parameterClass="stafferInfoBean" resultClass="stafferInfoBean">
   <![CDATA[
select staffId, staffPwd from staffer where staffid = #staffId# and staffpwd = #staffPwd#
]]>
  </select>
</sqlMap>



@Entity
public class StafferInfoBean implements Serializable
{
    /**
     * 注释内容
     */
    private static final long serialVersionUID = -2749598508449992482L;

    private String staffId;
   
    private String staffPwd;
   
    public StafferInfoBean()
    {
       
    }
   
    public String getStaffId()
    {
        return staffId;
    }
   
    public void setStaffId(String staffId)
    {
        this.staffId = staffId;
    }
   
    public String getStaffPwd()
    {
        return staffPwd;
    }
   
    public void setStaffPwd(String staffPwd)
    {
        this.staffPwd = staffPwd;
    }
   
}


2、创建Dao
LoginDao.java

public interface LoginDao
{
    public StafferInfoBean loginDao(StafferInfoBean stafferInfoBean);
}

LoginDaoImpl.java

import javax.annotation.Resource;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
@Repository("loginDaoImpl")
public class LoginDaoImpl implements LoginDao
{
   
    @Resource SqlMapClientTemplate sqlMapClientTemplate;
   
    /**
     * @param stafferInfoBean
     * @return
     */
    @Override
    public StafferInfoBean loginDao(StafferInfoBean stafferInfoBean)
    {
        StafferInfoBean stafferInfo = null;
        try
        {
            stafferInfo = (StafferInfoBean)sqlMapClientTemplate
                    .queryForObject("common.getLoginUser", stafferInfoBean);
           
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return stafferInfo;
    }
   
}

3、迟来的BaseAction
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

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

/**
* 所有Action的基类
*
* @author
* @version [版本号, Mar 7, 2010]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class BaseAction extends ActionSupport
{
   
    /**
     * 序列号
     */
    private static final long serialVersionUID = 1L;
   
    /**
     * 取得HttpServletRequest对象 <功能详细描述>
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    public HttpServletRequest getRequest()
    {
        return ServletActionContext.getRequest();
    }
   
    /**
     * 取得HttpServletRequest对象 <功能详细描述>
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    public HttpServletResponse getResponse()
    {
        return ServletActionContext.getResponse();
    }
   
    /**
     * 取得HttpSession对象 <功能详细描述>
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    public HttpSession getSession()
    {
        return ServletActionContext.getRequest().getSession();
    }
   
    /**
     * 返回日志对象
     *
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected Logger getLog()
    {
        return Logger.getLogger(BaseAction.class);
    }
   
    /**
     * 取得参数 <功能详细描述>
     *
     * @param name
     * @return
     * @see [类、类#方法、类#成员]
     */
    public String getParameter(String name)
    {
        return ServletActionContext.getRequest().getParameter(name);
    }
   
    /**
     * 设置 request 的属性.
     *
     * @param name 属性名
     * @param value 属性值
     */
    public void setAttribute(String name, Object value)
    {
        ServletActionContext.getRequest().setAttribute(name, value);
    }
   
    /**
     * 获取 request 的属性.
     *
     * @param name 属性名
     */
    public Object getAttribute(String name)
    {
        return ServletActionContext.getRequest().getAttribute(name);
    }
   
    /**
     * 读取 session 中的属性值
     *
     * @param name
     * @return
     */
    @SuppressWarnings("unchecked")
    public Object getSessionAttribute(String key)
    {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        return session.get(key);
    }
   
    /**
     * 向 session 设置属性值
     *
     * @param name
     * @param value
     */
    @SuppressWarnings("unchecked")
    public void setSessionAttribute(Object key, Object value)
    {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        session.put(key, value);
    }
   
    /**
     * 对返回值的封装
     * <功能详细描述>
     * @param str
     * @see [类、类#方法、类#成员]
     */
    public void outJsonString(String str)
    {
        getResponse().setContentType("text/javascript;charset=GBK");
        outString(str);
    }
   
    public void outJson(Object obj)
    {
        outJsonString(JSONObject.fromObject(obj).toString());
    }
   
    public void outJsonArray(Object array)
    {
        outJsonArray(JSONArray.fromObject(array).toString());
    }
   
    public void outString(String str)
    {
        try
        {
            PrintWriter out = getResponse().getWriter();
            out.write(str);
        }
        catch (IOException e)
        {
               
        }
    }
   
    public void outXMLString(String xmlStr)
    {
        getResponse().setContentType("application/xml;charset=GBK");
        outString(xmlStr);
    }
}
  相关解决方案