实现了hibernate 、struts、spring 的整合 有员工的注册、分页、删除、修改功能。还有多文件上传、无刷新上传【业务后台】...

实现了hibernate 、struts、spring 的整合 有员工的注册、分页、删除、修改功能。还有多文件上传、无刷新上传

form:
package com.sunjob.struts.forms;

import java.sql.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.springframework.web.struts.DelegatingActionProxy;

import com.pojos.Dep;

/*
* 复制了pojo
* 1.改java.util.Date为java.sql.Date
* 2.爱好要改成数组,且名字请修改(除了日期型外,改了类型就要改名字)
* 3.不能出现pojo类,把部门改部门id
* 4.文件上传,请把String改成FormFile
* 最后生成set/get方法
*/
public class EmpForm extends ActionForm {
private Integer empId;
private int depid; // 不能出现pojo对象,改成类型,改了名字
private String empName;
private Integer empSex;
private String[] empLoves; // 改成数组,请改名字
private FormFile empFacePics; // 文件上传,改成FormFile且改了名字
private Date empBirthday; // java.sql包

//赋初值
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
empSex = 1;
depid = 2;
empLoves = new String[]{"吃","乐"};
empBirthday = new Date(System.currentTimeMillis());
}

public Integer getEmpId() {
return empId;
}

public void setEmpId(Integer empId) {
this.empId = empId;
}

public int getDepid() {
return depid;
}

public void setDepid(int depid) {
this.depid = depid;
}

public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}

public Integer getEmpSex() {
return empSex;
}

public void setEmpSex(Integer empSex) {
this.empSex = empSex;
}

public String[] getEmpLoves() {
return empLoves;
}

public void setEmpLoves(String[] empLoves) {
this.empLoves = empLoves;
}

public FormFile getEmpFacePics() {
return empFacePics;
}

public void setEmpFacePics(FormFile empFacePics) {
this.empFacePics = empFacePics;
}

public Date getEmpBirthday() {
return empBirthday;
}

public void setEmpBirthday(Date empBirthday) {
this.empBirthday = empBirthday;
}
}



action:

package com.sunjob.struts.actions;


import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import net.sf.dozer.util.mapping.DozerBeanMapper;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import com.pojos.Emp;
import com.service.IEmpService;
import com.sunjob.struts.forms.EmpForm;

public class EmpAction extends DispatchAction {
private IEmpService iempService;



public void setIempService(IEmpService iempService) {
this.iempService = iempService;
}

//多文件上传
public ActionForward fileupload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//得到文件上传集合(界面上name作为键,FormFile作为值)
Map<String,FormFile> map = form.getMultipartRequestHandler().getFileElements();

//得到所有键
Set<String> files = map.keySet();
for (String key : files) { //文件name
FormFile formFile = map.get(key);
if (formFile != null && formFile.toString().length()>0) {
String filename = formFile.getFileName(); // 文件名字
byte[] filedata = formFile.getFileData(); // 文件内容
// 虚转实
String realPath = request.getSession().getServletContext()
.getRealPath("/upload");
FileOutputStream fileOutputStream = new FileOutputStream(realPath
+ "/" + filename); // 输出流,写文件
fileOutputStream.write(filedata); // 把数组内容写文件
fileOutputStream.close();// 关闭流

//empPojo.setEmpFacePic(filename); //文件名写数据库
}
}

return null;
}

//根据主键查emp转到修改界面
public ActionForward getEmpById2UpdateView(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//根据主键查找emp
int id = Integer.parseInt(request.getParameter("id"));
Emp empPojo = iempService.findEmpById(id);
EmpForm empForm = (EmpForm) form;
//pojo-->empForm
DozerBeanMapper beanMapper = new DozerBeanMapper();
beanMapper.map(empPojo, empForm);
//部门
if (empPojo.getDep()!=null)
empForm.setDepid(empPojo.getDep().getDepId());
//爱好
if (empPojo.getEmpLove()!=null){
empForm.setEmpLoves(empPojo.getEmpLove().split(";"));
}

//找部门
List deps = iempService.getAllDeps();
request.setAttribute("deps", deps);


return new ActionForward("/update.jsp");


}

// 真正注册
public ActionForward regist(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
EmpForm empForm = (EmpForm) form;
Emp empPojo = new Emp();
// form内容给pojo
DozerBeanMapper beanMapper = new DozerBeanMapper();
beanMapper.map(empForm, empPojo); // 前面-->后面
//BeanUtils.copyProperties(empPojo, empForm); //后面-->前面
// 手工处理部门、爱好,玉照
empPojo.setDep(iempService.findDepById(empForm.getDepid()));
String[] loves = empForm.getEmpLoves();
if (loves != null) {
empPojo.setEmpLove(StringUtils.join(loves, ";"));
}
// 玉照


FormFile formFile = empForm.getEmpFacePics();

if (formFile != null) {
String filename = formFile.getFileName(); // 文件名字
byte[] filedata = formFile.getFileData(); // 文件内容
// 虚转实
String realPath = request.getSession().getServletContext()
.getRealPath("/upload");
FileOutputStream fileOutputStream = new FileOutputStream(realPath
+ "/" + filename); // 输出流,写文件
fileOutputStream.write(filedata); // 把数组内容写文件
fileOutputStream.close();// 关闭流

empPojo.setEmpFacePic(filename); //文件名写数据库
}

//注册
iempService.registEmp(empPojo);

return fenye(mapping, form, request, response);



}

// 真正修改
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
EmpForm empForm = (EmpForm) form;

//修改要查找
Emp empPojo = iempService.findEmpById(empForm.getEmpId());
// form内容给pojo
DozerBeanMapper beanMapper = new DozerBeanMapper();
beanMapper.map(empForm, empPojo); // 前面-->后面
//BeanUtils.copyProperties(empPojo, empForm); //后面-->前面
// 手工处理部门、爱好,玉照
empPojo.setDep(iempService.findDepById(empForm.getDepid()));
String[] loves = empForm.getEmpLoves();
if (loves != null) {
empPojo.setEmpLove(StringUtils.join(loves, ";"));
}
// 玉照


FormFile formFile = empForm.getEmpFacePics();

if (formFile != null && formFile.toString().length()>0) {
String filename = formFile.getFileName(); // 文件名字

byte[] filedata = formFile.getFileData(); // 文件内容
// 虚转实
String realPath = request.getSession().getServletContext()
.getRealPath("/upload");
FileOutputStream fileOutputStream = new FileOutputStream(realPath
+ "/" + filename); // 输出流,写文件
fileOutputStream.write(filedata); // 把数组内容写文件
fileOutputStream.close();// 关闭流

empPojo.setEmpFacePic(filename); //文件名写数据库
}

//修改
iempService.updateEmp(empPojo);

return fenye(mapping, form, request, response);



}

// 注册,先查部门,然转到注册页面
public ActionForward getDep2Regist(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
List deps = iempService.getAllDeps();
request.setAttribute("deps", deps);
return new ActionForward("/regist.jsp");

}

// 分页
public ActionForward fenye(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 求第几页
String pageString = request.getParameter("page");
int page = 1;
if (pageString != null)
page = Integer.parseInt(pageString);

// 调用分页
int size = 3;
Map map = iempService.fenye(page, size);

request.setAttribute("map", map);

return new ActionForward("/show.jsp");
}

}



server:
package com.service;

import java.util.List;
import java.util.Map;

import com.dao.DepDAO;
import com.dao.EmpDAO;
import com.pojos.Dep;
import com.pojos.Emp;

public class EmpServiceImpl implements IEmpService {
private DepDAO depDAO;
private EmpDAO empDAO;

public void setDepDAO(DepDAO depDAO) {
this.depDAO = depDAO;
}

public void setEmpDAO(EmpDAO empDAO) {
this.empDAO = empDAO;
}

//注册
/* (non-Javadoc)
* @see com.service.IEmpService#registEmp(com.pojos.Emp)
*/
public void registEmp(Emp emp){
empDAO.save(emp);
}

//删除
/* (non-Javadoc)
* @see com.service.IEmpService#delEmpById(int)
*/
public void delEmpById(int id){
empDAO.delete(empDAO.findById(id));
}

//修改
/* (non-Javadoc)
* @see com.service.IEmpService#updateEmp(com.pojos.Emp)
*/
public void updateEmp(Emp emp){
empDAO.merge(emp);
}

//分页
/* (non-Javadoc)
* @see com.service.IEmpService#fenye(int, int)
*/
public Map fenye(int page,int size){

return empDAO.fenye(page,size);
}

public List getAllDeps() {
return depDAO.findAll();
}

public Dep findDepById(int depid) {
return depDAO.findById(depid);
}

public Emp findEmpById(int id) {
// TODO Auto-generated method stub
return empDAO.findById(id);
}


}


dao:
package com.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pojos.Emp;

/**
* A data access object (DAO) providing persistence and search support for Emp entities.
* Transaction control of the save(), update() and delete() operations
can directly support Spring container-managed transactions or they can be augmented to handle user-managed Spring transactions.
Each of these methods provides additional information for how to configure it for the desired type of transaction control.
* @see com.pojos.Emp
* @author MyEclipse Persistence Tools
*/

public class EmpDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(EmpDAO.class);
//property constants
public static final String EMP_NAME = "empName";
public static final String EMP_SEX = "empSex";
public static final String EMP_LOVE = "empLove";
public static final String EMP_FACE_PIC = "empFacePic";


protected void initDao() {
//do nothing
}

public void save(Emp transientInstance) {
log.debug("saving Emp instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Emp persistentInstance) {
log.debug("deleting Emp instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Emp findById( java.lang.Integer id) {
log.debug("getting Emp instance with id: " + id);
try {
Emp instance = (Emp) getHibernateTemplate()
.get("com.pojos.Emp", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(Emp instance) {
log.debug("finding Emp instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Emp instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Emp as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List findByEmpName(Object empName
) {
return findByProperty(EMP_NAME, empName
);
}

public List findByEmpSex(Object empSex
) {
return findByProperty(EMP_SEX, empSex
);
}

public List findByEmpLove(Object empLove
) {
return findByProperty(EMP_LOVE, empLove
);
}

public List findByEmpFacePic(Object empFacePic
) {
return findByProperty(EMP_FACE_PIC, empFacePic
);
}


public List findAll() {
log.debug("finding all Emp instances");
try {
String queryString = "from Emp";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public Emp merge(Emp detachedInstance) {
log.debug("merging Emp instance");
try {
Emp result = (Emp) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Emp instance) {
log.debug("attaching dirty Emp instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Emp instance) {
log.debug("attaching clean Emp instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static EmpDAO getFromApplicationContext(ApplicationContext ctx) {
return (EmpDAO) ctx.getBean("EmpDAO");
}

public Map fenye(final Integer page, final Integer size) {
//总条数
List listSum = getHibernateTemplate().find("select count(*) from Emp");
int sum = Integer.parseInt(listSum.get(0).toString());
//总页数
int count =(int) Math.ceil(1.0*sum/size);
//越界处理
final int page2 = page<1 ? 1: (page>count ? count :page);
//查
final String hql = "from Emp";
List list = getHibernateTemplate().executeFind(new HibernateCallback(){

public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (page!=null && size!=null)
query.setFirstResult((page2-1)*size).setMaxResults(size);

return query.list();
}});

//保存map
Map map = new HashMap();
map.put("page", page2);
map.put("size", size);
map.put("count", count);
map.put("list", list);


return map;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值