diff --git a/lostfoundcode/action/GoodsAction.java b/lostfoundcode/action/GoodsAction.java new file mode 100644 index 0000000..daf6ffa --- /dev/null +++ b/lostfoundcode/action/GoodsAction.java @@ -0,0 +1,354 @@ +package action; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; + +import model.Classify; +import model.PageBean; +import model.Goods; +import model.User; +//import model.UserAdmin; + +import org.apache.struts2.ServletActionContext; +import org.aspectj.util.FileUtil; + +import service.IUserService; +import util.AddJson; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.ModelDriven; + +public class GoodsAction extends ActionSupport implements ModelDriven{ + + IUserService iUserService; + public void setiUserService(IUserService iUserService) { + this.iUserService = iUserService; + } + + + Goods good=new Goods(); + @Override + public Goods getModel() { + return good; + } + HttpServletRequest req=ServletActionContext.getRequest(); + AddJson json=new AddJson(); + private int currPage=1;//当前页 + + public void setCurrPage(int currPage) { + this.currPage = currPage; + } + private File proPictureFile; + private String proPictureFileFileName; + private String proPictureContentType; + private final int BUFFER_SIZE=16*1024; + private Integer classifyId; + + public Integer getClassifyId() { + return classifyId; + } + public void setClassifyId(Integer classifyId) { + this.classifyId = classifyId; + } + public File getProPictureFile() { + return proPictureFile; + } + public void setProPictureFile(File proPictureFile) { + this.proPictureFile = proPictureFile; + } + + public String getProPictureContentType() { + return proPictureContentType; + } + public void setProPictureContentType(String proPictureContentType) { + this.proPictureContentType = proPictureContentType; + } + public String getProPictureFileFileName() { + return proPictureFileFileName; + } + public void setProPictureFileFileName(String proPictureFileFileName) { + this.proPictureFileFileName = proPictureFileFileName; + } + /** + * 发布失物招领或寻物启事信息 + */ + public String addLostGood() throws Exception{ + String newFileName=new Date().getTime()+getExtention(proPictureFileFileName); + String path=ServletActionContext.getServletContext().getRealPath("/upload"); + File picFile=new File(path); + if(!picFile.exists()){ + picFile.mkdir(); + } + FileUtil.copyFile(proPictureFile, new File(picFile,newFileName)); + //copy(proPictureFile,picFile); + good.setPicture(newFileName); + good.setCreateTime(new Date()); + Classify c=this.iUserService.getClassifyById(good.getClassifyId()); + User u=this.iUserService.getUserById(good.getCreatorId()); + good.setClassify(c); + good.setUser(u); + good.setGoodsstatus(0); + this.iUserService.saveGood(good); + this.addActionMessage("发布成功!"); + return "uploadSuccess"; + + } + public String addFoundGood() throws Exception{ + String newFileName=new Date().getTime()+getExtention(proPictureFileFileName); + String path=ServletActionContext.getServletContext().getRealPath("/upload"); + File picFile=new File(path); + if(!picFile.exists()){ + picFile.mkdir(); + } + FileUtil.copyFile(proPictureFile, new File(picFile,newFileName)); + //copy(proPictureFile,picFile); + good.setPicture(newFileName); + good.setCreateTime(new Date()); + Classify c=this.iUserService.getClassifyById(good.getClassifyId()); + User u=this.iUserService.getUserById(good.getCreatorId()); + good.setClassify(c); + good.setUser(u); + good.setGoodsstatus(1); + this.iUserService.saveGood(good); + this.addActionMessage("发布成功!"); + return "uploadSuccess"; + + } + + /** + * 上传文件(io流实现) + * @param src + * @param dst + * @throws Exception + */ + private void copy(File src, File dst)throws Exception { + InputStream in=null; + OutputStream out=null; + try { + in=new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE); + out=new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE); + byte[] buffer=new byte[BUFFER_SIZE]; + while(in.read(buffer)>0){ + out.write(buffer); + } + } catch (Exception e) { + throw e; + }finally{ + try { + in.close(); + } catch (Exception e2) { + + } + try { + out.close(); + } catch (Exception e2) { + + } + } + + } + /** + * 截图文件后缀名称 + * @param filename + * @return + */ + private String getExtention(String filename) { + int pos=filename.lastIndexOf("."); + return filename.substring(pos); + } + /** + * 查询分类列表 + * @throws IOException + */ + public void searchClassifyList() throws IOException{ + List list=this.iUserService.searchClassifyList(); + this.json.toJsonArray(list); + } + + /** + * 查询招领或寻物信息列表 + */ + public void searchLostGoodList() throws Exception{ + //String keyword=req.getParameter("keyword"); + String cid=req.getParameter("cid"); + //String conditon=req.getParameter("condition"); + Map map=new HashMap(); + /*if(keyword!=null&&keyword!=""){ + map.put("keyword", keyword); + }*/ + if(cid!=null&&cid!=""){ + map.put("cid", cid); + } + /*if(conditon!=null&&conditon!=""){ + map.put("conditon", conditon); + }*/ + map.put("currPage", currPage+""); + PageBean proList=this.iUserService.searchLostGoodList(map); + this.json.toJsonObj(proList); + } + + public void searchFoundGoodList() throws Exception{ + String cid=req.getParameter("cid"); + Map map=new HashMap(); + if(cid!=null&&cid!=""){ + map.put("cid", cid); + } + map.put("currPage", currPage+""); + PageBean proList=this.iUserService.searchFoundGoodList(map); + this.json.toJsonObj(proList); + } + + /** + * 查询招领或寻物信息详情 + */ + public void getLostGoodDetail() throws Exception{ + String id=req.getParameter("id"); + Goods good=this.iUserService.getLostGoodDetail(id); + this.json.toJson(good); + } + public void getFoundGoodDetail() throws Exception{ + String id=req.getParameter("id"); + Goods good=this.iUserService.getFoundGoodDetail(id); + this.json.toJson(good); + } + + + /** + * 分页查询我发布的列失物招领信息 + * @throws Exception + */ + public void searchMyLostProductByPage() throws Exception{ + User user=(User) req.getSession().getAttribute("User"); + if(user==null){ + throw new Exception("用户帐户为空,请重新登录!"); + }else{ + Map map=new HashMap(); + map.put("currPage", currPage+""); + map.put("userId", user.getId()+""); + PageBean myproList=this.iUserService.searchMyLostGoodByPage(map); + this.json.toJson(myproList); + } + } + public void searchMyProductByPage() throws Exception{ + User user=(User) req.getSession().getAttribute("User"); + if(user==null){ + throw new Exception("用户帐户为空,请重新登录!"); + }else{ + Map map=new HashMap(); + map.put("currPage", currPage+""); + map.put("userId", user.getId()+""); + PageBean myproList=this.iUserService.searchMyFoundGoodByPage(map); + this.json.toJson(myproList); + } + } + + private String repContent; + private String content; + + public String getRepContent() { + return repContent; + } + public void setRepContent(String repContent) { + this.repContent = repContent; + } + public String getContent() { + return content; + } + public void setContent(String content) { + this.content = content; + } + + + /** + * + *

Description: 删除我的商品

+ * @throws Exception + */ + public void deleteMyGoodById() throws Exception{ + User user=(User) req.getSession().getAttribute("User"); + if(user==null){ + throw new Exception("用户帐户为空,请重新登录!"); + }else{ + int pid=Integer.parseInt(req.getParameter("id")); + this.iUserService.delectGoodById(pid); + } + } + + + + /** + * 分页消息 + * @throws Exception + * @param flag 0系统消息,1用户消息 + */ +/* public String searchMessageByPage() throws Exception{ + User user=(User) req.getSession().getAttribute("User"); + String flag=""; + if(user==null){ + throw new Exception("用户帐户为空,请重新登录!"); + }else{ + flag=req.getParameter("flag");//0系统消息,1用户消息 + Map map=new HashMap(); + map.put("currPage", currPage+""); + map.put("userId", user.getId()+""); + map.put("flag", flag); + PageBean message=this.iUserService.searchMessageByPage(map); + if(message!=null){ + ActionContext.getContext().getValueStack().push(message); + } + } + if(flag=="0"||"0".equals(flag)){ + return "sysmessage"; + }else{ + return "usermessage"; + } + } + private String flag; + + public String getFlag() { + return flag; + } + public void setFlag(String flag) { + this.flag = flag; + } + /** + * 删除消息 + * @return + * @throws Exception + */ +/* public String deleteMessage() throws Exception{ + User user=(User) req.getSession().getAttribute("User"); + //String flag=req.getParameter("flag"); + if(user==null){ + throw new Exception("用户帐户为空,请重新登录!"); + }else{ + String id=req.getParameter("id"); + this.iUserService.deleteMessage(Integer.parseInt(id)); + } + if(flag=="0"||"0".equals(flag)){ + return "sysmsgDelete"; + }else{ + return "usermsgDelete"; + } + }*/ + + + +} diff --git a/lostfoundcode/action/UserAction.java b/lostfoundcode/action/UserAction.java new file mode 100644 index 0000000..7127339 --- /dev/null +++ b/lostfoundcode/action/UserAction.java @@ -0,0 +1,162 @@ +package action; + +import javax.servlet.http.HttpServletRequest; +import org.apache.struts2.ServletActionContext; +import service.ILoginService; +import util.AddJson; +import model.User; + +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.ModelDriven; + +public class UserAction extends ActionSupport implements ModelDriven{ + + User user=new User(); + @Override + public User getModel() { + return user; + } + + ILoginService iLoginService; + public void setiLoginService(ILoginService iLoginService) { + this.iLoginService = iLoginService; + } + + + HttpServletRequest req=ServletActionContext.getRequest(); + + /** + * 用户登录 + * @return + */ + public String UserLogin(){ + //HttpServletRequest req=ServletActionContext.getRequest(); + if(user==null){ + this.addActionError("请输入账号和密码!"); + return "login"; + } + + User u=this.iLoginService.findByUser(user); + if(u==null){ + this.addActionError("用户名或密码错误!"); + return "login"; + }else{ + req.getSession().setAttribute("User", u); + return "index"; + } + } + + /** + * 用户注册 + */ + public String userRegister(){ + //通过用户名先查询该用户是否已经注册 + User u=this.iLoginService.findUserByName(user.getUsername()); + if(u!=null){ + this.addActionError("该用户已被注册!"); + return "registerFail"; + } + //user.setHeadPic("images/logo-s.jpg"); + this.iLoginService.saveUser(user); + return "registerSuccess"; + } + + /** + * 通过账号找回密码步骤1 + */ + public String forgetPwd(){ + //HttpServletRequest req=ServletActionContext.getRequest(); + String userName=req.getParameter("userName"); + User u=this.iLoginService.findUserByName(userName); + if(u==null){ + this.addFieldError(userName, "用户名错误!"); + return "getPwdFail"; + } + if(u.getQusetion()==null){ + this.addActionError("抱歉!你没有设置问题和密码!"); + return "getPwdFail"; + } + req.getSession().setAttribute("User", u); + return "getPwdSuccess"; + + } + /** + * 提交问题2 + */ + public String checkAnswer(){ + //HttpServletRequest req=ServletActionContext.getRequest(); + String answertext=req.getParameter("answer"); + User u=this.iLoginService.findUserByName(user.getUsername()); + if(u!=null){ + String answer=u.getAnswer(); + if(!answer.equals(answertext)){ + this.addActionError("抱歉!回答错误!"); + return "answerFalse"; + } + } + req.getSession().setAttribute("User", u); + return "answerTrue"; + + } + + /** + * 重新设置密码3 + */ + public String updatePwd(){ + this.iLoginService.updatePed(user); + return "updatePwdSuccess"; + + } + + /** + * 查询用户账号信息 + * @throws Exception + */ + public void searchPersonalInfo() throws Exception{ + //HttpServletRequest req=ServletActionContext.getRequest(); + int uid=Integer.parseInt(req.getParameter("uid")); + if(uid==0){ + throw new Exception("用户不存在!"); + } + user=this.iLoginService.searchUserInfoById(uid); + AddJson json=new AddJson(); + json.toJson(user); + //return "findUserInfoSuccess"; + + } + + /** + * 修改用户信息 + */ + public void updateUserInfoById(){ + int uid=Integer.parseInt(req.getParameter("id")); + String uname=req.getParameter("userName"); + String pass=req.getParameter("password"); + String phone=req.getParameter("phone"); + String qusetion=req.getParameter("qusetion"); + String answer=req.getParameter("answer"); + int sex=Integer.parseInt(req.getParameter("sex")); + User uu=new User(); + uu.setAnswer(answer); + uu.setPassword(pass); + uu.setPhone(phone); + uu.setId(uid); + uu.setUsername(uname); + uu.setQusetion(qusetion); + uu.setSex(sex); + this.iLoginService.updateUser(uu); + + } + + /** + * 用户退出的登录 + */ + public String userExitLogin(){ + req.getSession().removeAttribute("User"); + return "UserexitSuccess"; + } + + + + +} diff --git a/lostfoundcode/dao/ILoginDao.java b/lostfoundcode/dao/ILoginDao.java new file mode 100644 index 0000000..4132442 --- /dev/null +++ b/lostfoundcode/dao/ILoginDao.java @@ -0,0 +1,53 @@ +package dao; + +//import model.Admin; +import model.User; + +public interface ILoginDao { + + /** + * 通过用户名和密码查询用户 + * @param user + * @return + */ + User findByUser(User user); + + /** + * 通过账号和密码查询管理员 + * @param admin + * @return + */ + //Admin findByAdmin(Admin admin); + + /** + * 通过用户名查询用户 + * @param userName + * @return + */ + User findUserByName(String userName); + + /** + * 保存用户信息 + * @param user + */ + void saveUser(User user); + + /** + * 修改用户密码 + * @param user + */ + void updatePed(User user); + + /** + * 通过用户id查询用户信息 + * @param uid + * @return + */ + User searchUserInfoById(int uid); + + /** + * 修改用户信息 + * @param user + */ + void updateUser(User user); +} diff --git a/lostfoundcode/dao/IUserDao.java b/lostfoundcode/dao/IUserDao.java new file mode 100644 index 0000000..284e192 --- /dev/null +++ b/lostfoundcode/dao/IUserDao.java @@ -0,0 +1,111 @@ +package dao; + +import java.util.List; +import java.util.Map; + +import model.Classify; +import model.Goods; +import model.User; +//import model.UserAdmin; + +public interface IUserDao { + + /** + * 发布供求信息 + * @param product + */ + void saveGood(Goods good); + + /** + * 查询分类列表 + * @return + */ + List searchClassifyList(); + + /** + * 查询商品 + * @param map + * @return + */ + List searchLostGoodList(Map map); + List searchFoundGoodList(Map map); + /** + * 查询商品数量 + * @param map + * @return + */ + int searchLostGoodCount(Map map); + int searchFoundGoodCount(Map map); + /** + * 查询商品详情 + * @param id + * @return + */ + Goods getLostGoodDetail(String id); + Goods getFoundGoodDetail(String id); + + /** + * 通过分类id获取分类信息 + * @param classifyId + */ + Classify getClassifyById(Integer classifyId); + + User getUserById(Integer creatorId); + + /*void updateProduct(Product product);*/ + + + /** + *

Description: 查询我发布的商品列表数量

+ * @param parseInt + * @return + */ + int searchMyLostGoodCount(int parseInt); + int searchMyFoundGoodCount(int parseInt); + /** + *

Description: 查询我发布的商品列表信息

+ * @param map + * @return + */ + List getMyLostGoodList(Map map); + List getMyFoundGoodList(Map map); + + + /** + *

Description: 删除我发布的商品

+ * @param pid + */ + void delectGoodById(int pid); + + + + + /** + *

Description: 保存用户消息

+ * @param map + * @return + */ +// void saveUserMessage(UserAdmin uaa); + + /** + *

Description: 保存用户消息数量

+ * @param map + * @return + */ +// int searchMessageCount(int uid, String flag); + + /** + *

Description: 保存用户消息

+ * @param map + * @return + */ +// List getMessageList(Map map); + + /** + * 通过id删除消息 + */ + void deleteMessage(int id); + + + +} diff --git a/lostfoundcode/dao/impl/LoginDao.java b/lostfoundcode/dao/impl/LoginDao.java new file mode 100644 index 0000000..3472772 --- /dev/null +++ b/lostfoundcode/dao/impl/LoginDao.java @@ -0,0 +1,96 @@ +package dao.impl; + +import java.util.List; +import org.springframework.orm.hibernate3.support.HibernateDaoSupport; +//import model.Admin; +import model.User; +import dao.ILoginDao; + +public class LoginDao extends HibernateDaoSupport implements ILoginDao { + + /** + * 通过账号和密码查询用户 + */ + @Override + public User findByUser(User user) { + String hql="from User u where u.userName=? and u.password=?"; + + @SuppressWarnings("unchecked") + + List list=this.getHibernateTemplate().find(hql,user.getUsername(),user.getPassword()); + if(list.size()>0){ + return list.get(0); + }else{ + return null; + } + } + + /** + * 通过账号和密码查询管理员 + */ +/* @Override + public Admin findByAdmin(Admin admin) { + String hql="from Admin a where a.name=? and a.password=?"; + @SuppressWarnings("unchecked") + List list=this.getHibernateTemplate().find(hql, admin.getName(),admin.getPassword()); + if(list.size()>0){ + return list.get(0); + }else{ + return null; + } + }*/ + + /** + * 通过用户名查询用户 + */ + @Override + public User findUserByName(String userName) { + String hql="from User where userName=?"; + @SuppressWarnings("unchecked") + List list=this.getHibernateTemplate().find(hql,userName); + if(list.size()>0){ + return list.get(0); + } + return null; + } + + /** + * 保存用户信息 + */ + @Override + public void saveUser(User user) { + this.getHibernateTemplate().save(user); + } + + /** + * 修改用户密码 + */ + @Override + public void updatePed(User user) { + User u=this.getHibernateTemplate().get(User.class, user.getId()); + u.setPassword(user.getPassword()); + this.getHibernateTemplate().update(u); + + } + + /** + * 通过用户id查询用户信息 + */ + @Override + public User searchUserInfoById(int uid) { + User u=this.getHibernateTemplate().get(User.class, uid); + return u; + } + + /** + * 修改用户信息 + */ + @Override + public void updateUser(User user) { + this.getHibernateTemplate().update(user); + + } + + + +} diff --git a/lostfoundcode/dao/impl/UserDao.java b/lostfoundcode/dao/impl/UserDao.java new file mode 100644 index 0000000..0d574f1 --- /dev/null +++ b/lostfoundcode/dao/impl/UserDao.java @@ -0,0 +1,362 @@ +package dao.impl; + +import java.util.List; +import java.util.Map; + +import model.Classify; +import model.Goods; +import model.User; +//import model.UserAdmin; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.criterion.DetachedCriteria; +import org.hibernate.criterion.MatchMode; +import org.hibernate.criterion.Order; +import org.hibernate.criterion.Restrictions; +import org.springframework.orm.hibernate3.support.HibernateDaoSupport; + +import dao.IUserDao; + +public class UserDao extends HibernateDaoSupport implements IUserDao { + + /** + * 发布供求信息 + */ + @Override + public void saveGood(Goods good) { + this.getHibernateTemplate().saveOrUpdate (good); + + } + + /** + * 查询分类列表 + */ + @SuppressWarnings("unchecked") + @Override + public List searchClassifyList() { + String hql="from Classify"; + return this.getHibernateTemplate().find(hql); + } + + /** + * 查询商品 + */ + @SuppressWarnings("unchecked") + @Override + public List searchLostGoodList(Map map) { + String cid=map.get("cid"); + DetachedCriteria dc=DetachedCriteria.forClass(Goods.class); + //dc.addOrder(Order.desc("createTime")); + //dc.add(Restrictions.ne("proHassum", 0)); + /*if(proName!=null){ + proName="%"+map.get("keyword")+"%"; + dc.add(Restrictions.like("proName",proName, MatchMode.ANYWHERE)); + }*/ + if(cid!=null){ + dc.add(Restrictions.eq("classify.cid",Integer.parseInt(cid))); + } + dc.add(Restrictions.eq("goodsStatus",0)); + /*if(condition!=null){ + int con=Integer.parseInt(condition); + switch(con){ + case 5: + dc.add(Restrictions.eq("type",1)); + break; + case 4: + dc.add(Restrictions.eq("type",0)); + break; + case 1: + dc.addOrder(Order.desc("proClicknum")); + break; + case 2: + dc.addOrder(Order.asc("proPrice")); + break; + case 3: + dc.addOrder(Order.desc("proPrice")); + break; + } + }*/ + + List list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize"))); + return list; + } + + /** + * 查询商品数量 + */ + @SuppressWarnings("unchecked") + @Override + public int searchLostGoodCount(Map map) { + StringBuffer br=new StringBuffer(); + StringBuffer order=new StringBuffer(); + //String proName=map.get("keyword"); + String cid=map.get("cid"); + //String condition=map.get("conditon"); + String count_hql="select count(*) from Product where goodsstatus='0'"; + /*if(proName!=null){ + proName="%"+map.get("keyword")+"%"; + br.append(" and proName like '"+proName+"'"); + }*/ + if(cid!=null){ + br.append(" and classify.cid='"+Integer.parseInt(cid)+"'"); + } + /*if(condition!=null){ + int con=Integer.parseInt(condition); + order.append(" order by createTime DESC"); + switch(con){ + case 1: + order.append(" ,proClicknum DESC"); + break; + case 2: + order.append(" ,proPrice ASC"); + break; + case 3: + order.append(" ,proPrice DESC"); + break; + case 4: + br.append(" and type=0"); + break; + case 5: + br.append(" and type=1"); + break; + + } + }*/ + List list=this.getHibernateTemplate().find(count_hql+br.toString()+order.toString()); + if(list.size()>0){ + return list.get(0).intValue(); + } + return 0; + } + @SuppressWarnings("unchecked") + @Override + public List searchFoundGoodList(Map map) { + String cid=map.get("cid"); + DetachedCriteria dc=DetachedCriteria.forClass(Goods.class); + if(cid!=null){ + dc.add(Restrictions.eq("classify.cid",Integer.parseInt(cid))); + } + dc.add(Restrictions.eq("goodsStatus",0)); + List list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize"))); + return list; + } + + /** + * 查询商品数量 + */ + @SuppressWarnings("unchecked") + @Override + public int searchFoundGoodCount(Map map) { + StringBuffer br=new StringBuffer(); + StringBuffer order=new StringBuffer(); + String cid=map.get("cid"); + String count_hql="select count(*) from Product where goodsstatus='1'"; + if(cid!=null){ + br.append(" and classify.cid='"+Integer.parseInt(cid)+"'"); + } + List list=this.getHibernateTemplate().find(count_hql+br.toString()+order.toString()); + if(list.size()>0){ + return list.get(0).intValue(); + } + return 0; + } + + + /** + * 查询商品详情 + */ + @SuppressWarnings("unchecked") + @Override + public Goods getLostGoodDetail(String id) { + int pid=0; + if(id!=null&&id!=""){ + pid=Integer.parseInt(id); + } + String hql=" from Product where id=?"; + List list=this.getHibernateTemplate().find(hql,pid); + if(list.size()>0){ + Goods p=list.get(0); + return p; + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public Goods getFoundGoodDetail(String id) { + int pid=0; + if(id!=null&&id!=""){ + pid=Integer.parseInt(id); + } + String hql=" from Product where id=?"; + List list=this.getHibernateTemplate().find(hql,pid); + if(list.size()>0){ + Goods p=list.get(0); + return p; + } + return null; + } + + + + @Override + public Classify getClassifyById(Integer classifyId) { + String hql="from Classify where cid=?"; + @SuppressWarnings("unchecked") + List c=this.getHibernateTemplate().find(hql, classifyId); + if(c.size()>0){ + Classify cla=c.get(0); + return cla; + } + return null; + } + + @Override + public User getUserById(Integer creatorId) { + String hql="from User where uid=?"; + @SuppressWarnings("unchecked") + List u=this.getHibernateTemplate().find(hql, creatorId); + if(u.size()>0){ + User user=u.get(0); + return user; + } + return null; + } + + /*@Override + public void updateProduct(Product product) { + this.getHibernateTemplate().update(product); + + }*/ + + + +/** + *

Description: 查询我发布的商品列表数量

+ * @param parseInt + * @return + */ + @SuppressWarnings("unchecked") +@Override +public int searchMyLostGoodCount(int uid) { + String hql="select count(*) from Product where user.uid = ? and goodsstatus='0'"; + List list=this.getHibernateTemplate().find(hql,uid); + if(list.size()>0){ + return list.get(0).intValue(); + } + return 0; +} + @SuppressWarnings("unchecked") +@Override +public int searchMyFoundGoodCount(int uid) { + String hql="select count(*) from Product where user.uid = ? and goodsstatus='1'"; + List list=this.getHibernateTemplate().find(hql,uid); + if(list.size()>0){ + return list.get(0).intValue(); + } + return 0; +} + +/** + *

Description: 查询我发布的商品列表信息

+ * @param map + * @return + */ + @SuppressWarnings("unchecked") +@Override +public List getMyLostGoodList(Map map) { + DetachedCriteria dc=DetachedCriteria.forClass(model.Goods.class); + dc.add(Restrictions.eq("user.uid",Integer.parseInt(map.get("userId")))); + dc.add(Restrictions.eq("goodsstatus",0)); + dc.addOrder(Order.desc("createTime")); + List list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize"))); + return list; +} + + @SuppressWarnings("unchecked") + @Override + public List getMyFoundGoodList(Map map) { + DetachedCriteria dc=DetachedCriteria.forClass(model.Goods.class); + dc.add(Restrictions.eq("user.uid",Integer.parseInt(map.get("userId")))); + dc.add(Restrictions.eq("goodsstatus",1)); + dc.addOrder(Order.desc("createTime")); + List list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize"))); + return list; + } + + +/** + *

Description: 删除我发布的商品

+ * @param pid + */ +@Override +public void delectGoodById(int pid) { + Goods good=this.getHibernateTemplate().get(Goods.class, pid); + if(good!=null){ + this.getHibernateTemplate().delete(good); + } + +} + + +/** + *

Description: 保存用户消息

+ * @param map + * @return + */ +/*@Override +public void saveUserMessage(UserAdmin uaa) { + this.getHibernateTemplate().save(uaa); +} +@SuppressWarnings("unchecked") +@Override +public int searchMessageCount(int uid, String flag) { + StringBuffer sb=new StringBuffer(); + String hql="select count(*) from UserAndAdmin where 1=1 "; + if(flag=="0"||"0".equals(flag)){ + sb.append(" and status='0' and userId="+uid); + }else{ + sb.append(" and status='1' and userId="+uid); + } + List list=this.getHibernateTemplate().find(hql+sb.toString()); + if(list.size()>0){ + return list.get(0).intValue(); + } + return 0; +} +@SuppressWarnings("unchecked") +@Override +public List getMessageList(Map map) { + DetachedCriteria dc=DetachedCriteria.forClass(model.UserAdmin.class); + String flag=map.get("flag"); + if(flag=="0"||"0".equals(flag)){ + dc.add(Restrictions.eq("userId",Integer.parseInt( map.get("userId")))); + dc.add(Restrictions.eq("status",0)); + }else{ + dc.add(Restrictions.eq("userId",Integer.parseInt( map.get("userId")))); + dc.add(Restrictions.eq("status",1)); + } + List list=this.getHibernateTemplate().findByCriteria(dc,Integer.parseInt(map.get("begin")),Integer.parseInt(map.get("pageSize"))); + return list; +}*/ + +/** + * 通过id删除消息 + */ +@Override +public void deleteMessage(int id) { + String hql="delete from UserAndAdmin where id="+id; + SessionFactory factory=this.getHibernateTemplate().getSessionFactory(); + Session session=factory.openSession(); + Query query=session.createQuery(hql); + query.executeUpdate(); + session.close(); + +} + + + + +} diff --git a/lostfoundcode/model/Classify.hbm.xml b/lostfoundcode/model/Classify.hbm.xml new file mode 100644 index 0000000..ace98b3 --- /dev/null +++ b/lostfoundcode/model/Classify.hbm.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lostfoundcode/model/Classify.java b/lostfoundcode/model/Classify.java new file mode 100644 index 0000000..53fd5a3 --- /dev/null +++ b/lostfoundcode/model/Classify.java @@ -0,0 +1,57 @@ +package model; + +import java.util.HashSet; +import java.util.Set; + +/** + * TClassify entity. @author MyEclipse Persistence Tools + */ + +public class Classify implements java.io.Serializable { + + // Fields + + private Integer cid; + private String cname; + private Integer csort; + + // Constructors + + /** default constructor */ + public Classify() { + } + + /** full constructor */ + public Classify(String cname, Integer csort) { + this.cname = cname; + this.csort = csort; + } + + // Property accessors + + public Integer getCid() { + return this.cid; + } + + public void setCid(Integer cid) { + this.cid = cid; + } + + public String getCname() { + return this.cname; + } + + public void setCname(String cname) { + this.cname = cname; + } + + public Integer getCsort() { + return this.csort; + } + + public void setCsort(Integer csort) { + this.csort = csort; + } + + +} \ No newline at end of file diff --git a/lostfoundcode/model/Goods.hbm.xml b/lostfoundcode/model/Goods.hbm.xml new file mode 100644 index 0000000..c4c6c11 --- /dev/null +++ b/lostfoundcode/model/Goods.hbm.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lostfoundcode/model/Goods.java b/lostfoundcode/model/Goods.java new file mode 100644 index 0000000..6e4cd43 --- /dev/null +++ b/lostfoundcode/model/Goods.java @@ -0,0 +1,168 @@ +package model; + +import java.sql.Timestamp; +import java.util.Date; + +/** + * TGoods entity. @author MyEclipse Persistence Tools + */ + +public class Goods implements java.io.Serializable { + + // Fields + + private Integer id; + private User user; + private Classify classify; + private String goodsdescribe; + private String goodsname; + private Integer goodsstatus;//区分招领和寻物 + private Date goodstime;//什么时候捡到的、丢的 + private String area; + private String contact;//联系人姓名 + private String qq; + private String picture; + private Date createTime; + private Integer creatorId;//发布者id + private Integer classifyId;//分类id + + // Constructors + + /** default constructor */ + public Goods() { + } + + /** minimal constructor */ + /*public Goods(Integer id, User TUser, Integer goodsstatus) { + this.id = id; + this.User = TUser; + this.goodsstatus = goodsstatus; + } + + /** full constructor */ + /*public Goods(Integer id, User TUser, Classify TClassify, + String goodsdescribe, String goodsname, Integer goodsstatus, + Timestamp goodstime, String area, String contact, String qq) { + this.id = id; + this.TUser = TUser; + this.TClassify = TClassify; + this.goodsdescribe = goodsdescribe; + this.goodsname = goodsname; + this.goodsstatus = goodsstatus; + this.goodstime = goodstime; + this.area = area; + this.contact = contact; + this.qq = qq; + }*/ + + // Property accessors + + public Integer getId() { + return this.id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getPicture() { + return picture; + } + public void setPicture(String picture) { + this.picture = picture; + } + + + public String getGoodsdescribe() { + return this.goodsdescribe; + } + + public void setGoodsdescribe(String goodsdescribe) { + this.goodsdescribe = goodsdescribe; + } + + public String getGoodsname() { + return this.goodsname; + } + + public void setGoodsname(String goodsname) { + this.goodsname = goodsname; + } + + public Integer getGoodsstatus() { + return this.goodsstatus; + } + + public void setGoodsstatus(Integer goodsstatus) { + this.goodsstatus = goodsstatus; + } + + public Date getGoodstime() { + return this.goodstime; + } + + public void setGoodstime(Timestamp goodstime) { + this.goodstime = goodstime; + } + + public String getArea() { + return this.area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getContact() { + return this.contact; + } + + public void setContact(String contact) { + this.contact = contact; + } + + public String getQq() { + return this.qq; + } + + public void setQq(String qq) { + this.qq = qq; + } + + public Integer getCreatorId() { + return creatorId; + } + + public void setCreatorId(Integer creatorId) { + this.creatorId = creatorId; + } + + public Integer getClassifyId() { + return classifyId; + } + + public void setClassifyId(Integer classifyId) { + this.classifyId = classifyId; + } + + public Classify getClassify() { + return classify; + } + public void setClassify(Classify classify) { + this.classify = classify; + } + public User getUser() { + return user; + } + public void setUser(User user) { + this.user = user; + } + + public Date getCreateTime() { + return createTime; + } + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + +} \ No newline at end of file diff --git a/lostfoundcode/model/HibernateSessionFactory.java b/lostfoundcode/model/HibernateSessionFactory.java new file mode 100644 index 0000000..76764a7 --- /dev/null +++ b/lostfoundcode/model/HibernateSessionFactory.java @@ -0,0 +1,118 @@ +package model; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; + +/** + * Configures and provides access to Hibernate sessions, tied to the + * current thread of execution. Follows the Thread Local Session + * pattern, see {@link http://hibernate.org/42.html }. + */ +public class HibernateSessionFactory { + + /** + * Location of hibernate.cfg.xml file. + * Location should be on the classpath as Hibernate uses + * #resourceAsStream style lookup for its configuration file. + * The default classpath location of the hibernate config file is + * in the default package. Use #setConfigFile() to update + * the location of the configuration file for the current session. + */ + private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; + private static final ThreadLocal threadLocal = new ThreadLocal(); + private static Configuration configuration = new Configuration(); + private static org.hibernate.SessionFactory sessionFactory; + private static String configFile = CONFIG_FILE_LOCATION; + + static { + try { + configuration.configure(configFile); + sessionFactory = configuration.buildSessionFactory(); + } catch (Exception e) { + System.err + .println("%%%% Error Creating SessionFactory %%%%"); + e.printStackTrace(); + } + } + private HibernateSessionFactory() { + } + + /** + * Returns the ThreadLocal Session instance. Lazy initialize + * the SessionFactory if needed. + * + * @return Session + * @throws HibernateException + */ + public static Session getSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + + if (session == null || !session.isOpen()) { + if (sessionFactory == null) { + rebuildSessionFactory(); + } + session = (sessionFactory != null) ? sessionFactory.openSession() + : null; + threadLocal.set(session); + } + + return session; + } + + /** + * Rebuild hibernate session factory + * + */ + public static void rebuildSessionFactory() { + try { + configuration.configure(configFile); + sessionFactory = configuration.buildSessionFactory(); + } catch (Exception e) { + System.err + .println("%%%% Error Creating SessionFactory %%%%"); + e.printStackTrace(); + } + } + + /** + * Close the single hibernate session instance. + * + * @throws HibernateException + */ + public static void closeSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + threadLocal.set(null); + + if (session != null) { + session.close(); + } + } + + /** + * return session factory + * + */ + public static org.hibernate.SessionFactory getSessionFactory() { + return sessionFactory; + } + + /** + * return session factory + * + * session factory will be rebuilded in the next call + */ + public static void setConfigFile(String configFile) { + HibernateSessionFactory.configFile = configFile; + sessionFactory = null; + } + + /** + * return hibernate configuration + * + */ + public static Configuration getConfiguration() { + return configuration; + } + +} \ No newline at end of file diff --git a/lostfoundcode/model/PageBean.java b/lostfoundcode/model/PageBean.java new file mode 100644 index 0000000..9ab22ce --- /dev/null +++ b/lostfoundcode/model/PageBean.java @@ -0,0 +1,47 @@ +package model; + +import java.util.List; + +public class PageBean { + private int currPage;//当前页 + private int pageSize;//每页记录数 + private int totalCount;//总记录数 + private int totalPage;//总页数 + List list;//每页显示的数据 + + public PageBean() { + } + public int getCurrPage() { + return currPage; + } + public void setCurrPage(int currPage) { + this.currPage = currPage; + } + public int getPageSize() { + return pageSize; + } + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + public int getTotalCount() { + return totalCount; + } + public void setTotalCount(int totalCount) { + this.totalCount = totalCount; + } + public int getTotalPage() { + return totalPage; + } + public void setTotalPage(int totalPage) { + this.totalPage = totalPage; + } + public List getList() { + return list; + } + public void setList(List list) { + this.list = list; + } + + + +} diff --git a/lostfoundcode/model/User.hbm.xml b/lostfoundcode/model/User.hbm.xml new file mode 100644 index 0000000..88df4b7 --- /dev/null +++ b/lostfoundcode/model/User.hbm.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lostfoundcode/model/User.java b/lostfoundcode/model/User.java new file mode 100644 index 0000000..704f8b6 --- /dev/null +++ b/lostfoundcode/model/User.java @@ -0,0 +1,95 @@ +package model; + +import java.util.HashSet; +import java.util.Set; + +/** + * TUser entity. @author MyEclipse Persistence Tools + */ + +public class User implements java.io.Serializable { + + // Fields + + private Integer id; + private String username; + private Integer sex; + private String phone; + private String password; + private String qusetion; + private String answer; + + // Constructors + + /** default constructor */ + public User() { + } + + /** full constructor */ + public User(String username, String password,String qusetion, String answer) { + this.username = username; + this.password = password; + this.qusetion = qusetion; + this.answer = answer; + } + + // Property accessors + + public Integer getId() { + return this.id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Integer getSex() { + return this.sex; + } + + public void setSex(Integer sex) { + this.sex = sex; + } + + public String getPhone() { + return this.phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getQusetion() { + return this.qusetion; + } + + public void setQusetion(String qusetion) { + this.qusetion = qusetion; + } + + public String getAnswer() { + return this.answer; + } + + public void setAnswer(String answer) { + this.answer = answer; + } + + +} \ No newline at end of file diff --git a/lostfoundcode/service/ILoginService.java b/lostfoundcode/service/ILoginService.java new file mode 100644 index 0000000..cdefa27 --- /dev/null +++ b/lostfoundcode/service/ILoginService.java @@ -0,0 +1,53 @@ +package service; + +//import model.Admin; +import model.User; + +public interface ILoginService { + + /** + * 通过用户名和密码查询用户 + * @param user + * @return + */ + User findByUser(User user); + + /** + * 通过管理员帐户和密码查询管理员 + * @param admin + * @return + */ + //Admin findByAdmin(Admin admin); + + /** + * 通过用户名查询用户 + * @param userName + * @return + */ + User findUserByName(String userName); + + /** + * 保存用户信息 + * @param user + */ + void saveUser(User user); + + /** + * 修改用户密码 + * @param user + */ + void updatePed(User user); + + /** + * 通过用户id查询用户信息 + * @param uid + * @return + */ + User searchUserInfoById(int uid); + + /** + * 修改用户信息 + * @param user + */ + void updateUser(User user); +} diff --git a/lostfoundcode/service/IUserService.java b/lostfoundcode/service/IUserService.java new file mode 100644 index 0000000..c35e4fd --- /dev/null +++ b/lostfoundcode/service/IUserService.java @@ -0,0 +1,95 @@ +package service; + +import java.util.List; +import java.util.Map; + +import model.Classify; +import model.PageBean; +import model.Goods; +import model.User; +//import model.UserAdmin; + +public interface IUserService { + + /** + * 发布供求信息 + * @param product + */ + void saveGood(Goods good); + + /** + * 查询分类列表 + * @return + */ + List searchClassifyList(); + + /** + * 查询商品 + * @param map + * @return + */ + PageBean searchLostGoodList(Map map); + PageBean searchFoundGoodList(Map map); + + /** + * 查询商品详情 + * @param id + * @return + */ + Goods getLostGoodDetail(String id); + Goods getFoundGoodDetail(String id); + + /** + * 通过分类id获取分类信息 + * @param classifyId + */ + Classify getClassifyById(Integer classifyId); + + User getUserById(Integer creatorId); + + /*void updateProduct(Product product);*/ + + + + /** + *

Description: 查询我发布的商品列表信息

+ * @param map + * @return + */ + PageBean searchMyLostGoodByPage(Map map); + PageBean searchMyFoundGoodByPage(Map map); + + + /** + *

Description: 删除我发布的商品

+ * @param pid + */ + void delectGoodById(int pid); + + + /** + * 保存用户消息 + * @param uaa + */ + //void saveUserMessage(UserAdmin uaa); + + /** + * 分页消息 + * @throws Exception + * @param flag 0系统消息,1用户消息 + */ + //PageBean searchMessageByPage(Map map); + + /** + * 通过id删除消息 + * @param parseInt + */ + void deleteMessage(int id); + + + + + + + +} diff --git a/lostfoundcode/service/impl/LoginService.java b/lostfoundcode/service/impl/LoginService.java new file mode 100644 index 0000000..c724a28 --- /dev/null +++ b/lostfoundcode/service/impl/LoginService.java @@ -0,0 +1,79 @@ +package service.impl; + +import org.springframework.transaction.annotation.Transactional; + +import dao.ILoginDao; +//import model.Admin; +import model.User; +import service.ILoginService; +@Transactional +public class LoginService implements ILoginService{ + + private ILoginDao iLoginDao; + + public void setiLoginDao(ILoginDao iLoginDao) { + this.iLoginDao = iLoginDao; + } + + /** + * 通过用户帐户和密码查询用户 + */ + @Override + public User findByUser(User user) { + return iLoginDao.findByUser(user); + } + + /** + * 通过管理员帐户和密码查询管理员 + */ +/* @Override + public Admin findByAdmin(Admin admin) { + return iLoginDao.findByAdmin(admin); + }*/ + + /** + * 通过用户名查询用户 + */ + @Override + public User findUserByName(String userName) { + return this.iLoginDao.findUserByName(userName); + } + + /** + * 保存用户信息 + */ + @Override + public void saveUser(User user) { + this.iLoginDao.saveUser(user); + } + + /** + * 修改用户密码 + */ + @Override + public void updatePed(User user) { + this.iLoginDao.updatePed(user); + + } + + /** + * 通过用户id查询用户信息 + */ + @Override + public User searchUserInfoById(int uid) { + + return this.iLoginDao.searchUserInfoById(uid); + } + + /** + * 修改用户信息 + */ + @Override + public void updateUser(User user) { + this.iLoginDao.updateUser(user); + + } + + + +} diff --git a/lostfoundcode/service/impl/UserService.java b/lostfoundcode/service/impl/UserService.java new file mode 100644 index 0000000..defabbc --- /dev/null +++ b/lostfoundcode/service/impl/UserService.java @@ -0,0 +1,264 @@ +package service.impl; + +import java.util.List; +import java.util.Map; + +import model.Classify; +import model.PageBean; +import model.Goods; +import model.User; +//import model.UserAdmin; + +import org.springframework.transaction.annotation.Transactional; + +import service.IUserService; +import dao.IUserDao; +@Transactional +public class UserService implements IUserService { + + private IUserDao iUserDao; + + public void setiUserDao(IUserDao iUserDao) { + this.iUserDao = iUserDao; + } + Integer currPage=1; + /** + * 发布供求信息 + */ + @Override + public void saveGood(Goods good) { + this.iUserDao.saveGood(good); + + } + + /** + * 查询分类列表 + */ + @Override + public List searchClassifyList() { + + return this.iUserDao.searchClassifyList(); + } + + /** + * 查询商品 + */ + @Override + public PageBean searchLostGoodList(Map map) { + PageBean pageBean=new PageBean(); + //封装当前页 + currPage=Integer.parseInt(map.get("currPage")); + pageBean.setCurrPage(currPage); + //封装每页记录数 + int pageSize=12; + pageBean.setPageSize(pageSize); + //封装总记录数 + int totalCount=this.iUserDao.searchLostGoodCount(map); + pageBean.setTotalCount(totalCount); + //封装总页数 + double tc=totalCount; + Double num=Math.ceil(tc/pageSize); + if(num==0){ + num=(double) 1; + } + pageBean.setTotalPage(num.intValue()); + //封装每页显示的数据 + int begin=(currPage-1)*pageSize; + map.put("begin", begin+""); + map.put("pageSize", pageSize+""); + List list=this.iUserDao.searchLostGoodList(map); + pageBean.setList(list); + return pageBean; + + } + + @Override + public PageBean searchFoundGoodList(Map map) { + PageBean pageBean=new PageBean(); + //封装当前页 + currPage=Integer.parseInt(map.get("currPage")); + pageBean.setCurrPage(currPage); + //封装每页记录数 + int pageSize=12; + pageBean.setPageSize(pageSize); + //封装总记录数 + int totalCount=this.iUserDao.searchFoundGoodCount(map); + pageBean.setTotalCount(totalCount); + //封装总页数 + double tc=totalCount; + Double num=Math.ceil(tc/pageSize); + if(num==0){ + num=(double) 1; + } + pageBean.setTotalPage(num.intValue()); + //封装每页显示的数据 + int begin=(currPage-1)*pageSize; + map.put("begin", begin+""); + map.put("pageSize", pageSize+""); + List list=this.iUserDao.searchFoundGoodList(map); + pageBean.setList(list); + return pageBean; + + } + + /** + * 查询商品详情 + */ + @Override + public Goods getLostGoodDetail(String id) { + + return this.iUserDao.getLostGoodDetail(id); + } + @Override + public Goods getFoundGoodDetail(String id) { + + return this.iUserDao.getFoundGoodDetail(id); + } + + + @Override + public Classify getClassifyById(Integer classifyId) { + return this.iUserDao.getClassifyById(classifyId); + + } + + @Override + public User getUserById(Integer creatorId) { + + return this.iUserDao.getUserById(creatorId); + } + + /*@Override + public void updateProduct(Product product) { + this.iUserDao.updateProduct(product); + + }*/ + + /** + *

Description: 查询我发布的商品列表信息

+ * @param map + * @return + */ + @Override + public PageBean searchMyLostGoodByPage(Map map) { + PageBean pageBean=new PageBean(); + //封装当前页 + currPage=Integer.parseInt(map.get("currPage")); + pageBean.setCurrPage(currPage); + //封装每页记录数 + int pageSize=5; + pageBean.setPageSize(pageSize); + //封装总记录数 + int totalCount=this.iUserDao.searchMyLostGoodCount(Integer.parseInt(map.get("userId"))); + pageBean.setTotalCount(totalCount); + //封装总页数 + double tc=totalCount; + Double num=Math.ceil(tc/pageSize); + if(num==0){ + num=(double) 1; + } + pageBean.setTotalPage(num.intValue()); + //封装每页显示的数据 + int begin=(currPage-1)*pageSize; + map.put("begin", begin+""); + map.put("pageSize", pageSize+""); + List list=this.iUserDao.getMyLostGoodList(map); + pageBean.setList(list); + return pageBean; + } + + public PageBean searchMyFoundGoodByPage(Map map) { + PageBean pageBean=new PageBean(); + //封装当前页 + currPage=Integer.parseInt(map.get("currPage")); + pageBean.setCurrPage(currPage); + //封装每页记录数 + int pageSize=5; + pageBean.setPageSize(pageSize); + //封装总记录数 + int totalCount=this.iUserDao.searchMyFoundGoodCount(Integer.parseInt(map.get("userId"))); + pageBean.setTotalCount(totalCount); + //封装总页数 + double tc=totalCount; + Double num=Math.ceil(tc/pageSize); + if(num==0){ + num=(double) 1; + } + pageBean.setTotalPage(num.intValue()); + //封装每页显示的数据 + int begin=(currPage-1)*pageSize; + map.put("begin", begin+""); + map.put("pageSize", pageSize+""); + List list=this.iUserDao.getMyFoundGoodList(map); + pageBean.setList(list); + return pageBean; + } + + /** + *

Description: 删除我发布的商品

+ * @param pid + */ + @Override + public void delectGoodById(int pid) { + this.iUserDao.delectGoodById(pid); + + } + + + /** + * 保存用户消息 + * @param uaa + */ +/* @Override + public void saveUserMessage(UserAdmin uaa) { + this.iUserDao.saveUserMessage(uaa); + } + + /** + * 分页消息 + * @throws Exception + * @param flag 0系统消息,1用户消息 + */ +/* @Override + public PageBean searchMessageByPage(Map map) { + PageBean pageBean=new PageBean(); + //封装当前页 + currPage=Integer.parseInt(map.get("currPage")); + pageBean.setCurrPage(currPage); + //封装每页记录数 + int pageSize=10; + pageBean.setPageSize(pageSize); + //封装总记录数 + int totalCount=this.iUserDao.searchMessageCount(Integer.parseInt(map.get("userId")),map.get("flag")); + pageBean.setTotalCount(totalCount); + //封装总页数 + double tc=totalCount; + Double num=Math.ceil(tc/pageSize); + if(num==0){ + num=(double) 1; + } + pageBean.setTotalPage(num.intValue()); + //封装每页显示的数据 + int begin=(currPage-1)*pageSize; + map.put("begin", begin+""); + map.put("pageSize", pageSize+""); + List list=this.iUserDao.getMessageList(map); + pageBean.setList(list); + return pageBean; + }*/ + + /** + * 通过id删除消息 + */ + @Override + public void deleteMessage(int id) { + this.iUserDao.deleteMessage(id); + + } + + + + + + +} diff --git a/pagebean/.classpath b/pagebean/.classpath new file mode 100644 index 0000000..32040b3 --- /dev/null +++ b/pagebean/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/pagebean/.myhibernatedata b/pagebean/.myhibernatedata new file mode 100644 index 0000000..405b3a4 --- /dev/null +++ b/pagebean/.myhibernatedata @@ -0,0 +1,25 @@ +#MyEclipse Hibernate Properties +#Sat Jan 04 23:52:24 CST 2020 +sessionFactoryName=hib.HibernateSessionFactory +genBasicCompId=false +profile= +daoSFId= +version=3.3 +jndiPath= +detectM2M=false +reStrategyClass= +detectO2O=false +springDaoFile= +useJavaTypes=true +keyGenerator= +libInstallFolder=WebRoot/WEB-INF/lib +addLibs2Project=true +genVersionTag=false +sessionFactoryId= +basePersistenceClass= +genAnnotations=false +reSettingsFile= +configFile=/pagebean/src/hibernate.cfg.xml +createConfigFile=true +addLibs2Classpath=true +baseDaoClass=BaseHibernateDAO diff --git a/pagebean/.mymetadata b/pagebean/.mymetadata new file mode 100644 index 0000000..4a30efa --- /dev/null +++ b/pagebean/.mymetadata @@ -0,0 +1,13 @@ + + + + + + + diff --git a/pagebean/.project b/pagebean/.project new file mode 100644 index 0000000..311d8e4 --- /dev/null +++ b/pagebean/.project @@ -0,0 +1,64 @@ + + + pagebean + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder + + + + + org.eclipse.jdt.core.javabuilder + + + + + com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator + + + + + com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator + + + + + org.eclipse.wst.validation.validationbuilder + + + + + com.genuitec.eclipse.hibernate.HibernateBuilder + + + + + com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder + + + + + + com.genuitec.eclipse.ast.deploy.core.deploymentnature + com.genuitec.eclipse.hibernate.hibernatenature + org.eclipse.jem.workbench.JavaEMFNature + com.genuitec.eclipse.j2eedt.core.webnature + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.common.modulecore.ModuleCoreNature + + diff --git a/pagebean/.settings/.jsdtscope b/pagebean/.settings/.jsdtscope new file mode 100644 index 0000000..252e54f --- /dev/null +++ b/pagebean/.settings/.jsdtscope @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/pagebean/.settings/org.eclipse.jdt.core.prefs b/pagebean/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..eda871f --- /dev/null +++ b/pagebean/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Sat Jan 04 23:57:23 CST 2020 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/pagebean/.settings/org.eclipse.wst.common.component b/pagebean/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..343dd38 --- /dev/null +++ b/pagebean/.settings/org.eclipse.wst.common.component @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/pagebean/.settings/org.eclipse.wst.common.project.facet.core.xml b/pagebean/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..ef27789 --- /dev/null +++ b/pagebean/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/pagebean/.settings/org.eclipse.wst.jsdt.ui.superType.container b/pagebean/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..3bd5d0a --- /dev/null +++ b/pagebean/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/pagebean/.settings/org.eclipse.wst.jsdt.ui.superType.name b/pagebean/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..05bd71b --- /dev/null +++ b/pagebean/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/pagebean/WebRoot/META-INF/MANIFEST.MF b/pagebean/WebRoot/META-INF/MANIFEST.MF new file mode 100644 index 0000000..254272e --- /dev/null +++ b/pagebean/WebRoot/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/pagebean/WebRoot/WEB-INF/classes/action/ProductAction.class b/pagebean/WebRoot/WEB-INF/classes/action/ProductAction.class new file mode 100644 index 0000000..2f912e6 Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/action/ProductAction.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/AbstractProducts.class b/pagebean/WebRoot/WEB-INF/classes/dao/AbstractProducts.class new file mode 100644 index 0000000..0bcfc05 Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/AbstractProducts.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/AbstractProductsId.class b/pagebean/WebRoot/WEB-INF/classes/dao/AbstractProductsId.class new file mode 100644 index 0000000..8b88738 Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/AbstractProductsId.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/BaseHibernateDAO.class b/pagebean/WebRoot/WEB-INF/classes/dao/BaseHibernateDAO.class new file mode 100644 index 0000000..2ad133b Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/BaseHibernateDAO.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/HibernateSessionFactory.class b/pagebean/WebRoot/WEB-INF/classes/dao/HibernateSessionFactory.class new file mode 100644 index 0000000..8bfb0cc Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/HibernateSessionFactory.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/IBaseHibernateDAO.class b/pagebean/WebRoot/WEB-INF/classes/dao/IBaseHibernateDAO.class new file mode 100644 index 0000000..b8a3b7a Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/IBaseHibernateDAO.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/Products.class b/pagebean/WebRoot/WEB-INF/classes/dao/Products.class new file mode 100644 index 0000000..ba9ba7e Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/Products.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/Products.hbm.xml b/pagebean/WebRoot/WEB-INF/classes/dao/Products.hbm.xml new file mode 100644 index 0000000..acce55c --- /dev/null +++ b/pagebean/WebRoot/WEB-INF/classes/dao/Products.hbm.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/ProductsDAO.class b/pagebean/WebRoot/WEB-INF/classes/dao/ProductsDAO.class new file mode 100644 index 0000000..793c8ab Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/ProductsDAO.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/dao/ProductsId.class b/pagebean/WebRoot/WEB-INF/classes/dao/ProductsId.class new file mode 100644 index 0000000..f144d59 Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/dao/ProductsId.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/hibernate.cfg.xml b/pagebean/WebRoot/WEB-INF/classes/hibernate.cfg.xml new file mode 100644 index 0000000..d970cc4 --- /dev/null +++ b/pagebean/WebRoot/WEB-INF/classes/hibernate.cfg.xml @@ -0,0 +1,26 @@ + + + + + + + + + org.hibernate.dialect.MySQLDialect + + + jdbc:mysql://localhost:3306/myday + + root + mysql123 + + com.mysql.jdbc.Driver + + myday + + + + + \ No newline at end of file diff --git a/pagebean/WebRoot/WEB-INF/classes/model/PageBean.class b/pagebean/WebRoot/WEB-INF/classes/model/PageBean.class new file mode 100644 index 0000000..0e2d83e Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/model/PageBean.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/model/Product.class b/pagebean/WebRoot/WEB-INF/classes/model/Product.class new file mode 100644 index 0000000..daf6188 Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/model/Product.class differ diff --git a/pagebean/WebRoot/WEB-INF/classes/service/ProductService.class b/pagebean/WebRoot/WEB-INF/classes/service/ProductService.class new file mode 100644 index 0000000..93fcefb Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/classes/service/ProductService.class differ diff --git a/pagebean/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.47.jar b/pagebean/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.47.jar new file mode 100644 index 0000000..f3398ed Binary files /dev/null and b/pagebean/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.47.jar differ diff --git a/pagebean/WebRoot/WEB-INF/web.xml b/pagebean/WebRoot/WEB-INF/web.xml new file mode 100644 index 0000000..5b6f684 --- /dev/null +++ b/pagebean/WebRoot/WEB-INF/web.xml @@ -0,0 +1,11 @@ + + + + + index.jsp + + diff --git a/pagebean/WebRoot/index.jsp b/pagebean/WebRoot/index.jsp new file mode 100644 index 0000000..19ce291 --- /dev/null +++ b/pagebean/WebRoot/index.jsp @@ -0,0 +1,26 @@ +<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'index.jsp' starting page + + + + + + + + + + This is my JSP page.
+ + diff --git a/pagebean/WebRoot/page.jsp b/pagebean/WebRoot/page.jsp new file mode 100644 index 0000000..80f868c --- /dev/null +++ b/pagebean/WebRoot/page.jsp @@ -0,0 +1,42 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + +Insert title here + + + <%-- 构建分页导航 --%> + 共有${requestScope.pageBean.totalRecord}条信息,共${requestScope.pageBean.totalPage }页,当前为${requestScope.pageBean.pageNum}页 +
+ + + + 首页 + 上一页 + + + + + + + + ${i} + + + ${i} + + + + + + 下一页 + 末页 + + + <%--尾页 --%> + 尾页 + + \ No newline at end of file diff --git a/pagebean/src/action/ProductAction.java b/pagebean/src/action/ProductAction.java new file mode 100644 index 0000000..b612e89 --- /dev/null +++ b/pagebean/src/action/ProductAction.java @@ -0,0 +1,31 @@ +package action; +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import model.PageBean; + +import service.ProductService; + + +public class ProductAction { + + + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + int pageNum = Integer.valueOf(request.getParameter("pageNum")); + int pageSize=10; + ProductService ps = new ProductService(); + //ȡһPageBean + PageBean pb = ps.showAllProductWithPage(pageNum, pageSize); + + //pbrequest + request.setAttribute("pageBean",pb); + request.getRequestDispatcher("/page.jsp").forward(request,response); + } + + +} diff --git a/pagebean/src/dao/AbstractProducts.java b/pagebean/src/dao/AbstractProducts.java new file mode 100644 index 0000000..3ba6793 --- /dev/null +++ b/pagebean/src/dao/AbstractProducts.java @@ -0,0 +1,35 @@ +package dao; + +/** + * AbstractProducts entity provides the base persistence definition of the + * Products entity. @author MyEclipse Persistence Tools + */ + +public abstract class AbstractProducts implements java.io.Serializable { + + // Fields + + private ProductsId id; + + // Constructors + + /** default constructor */ + public AbstractProducts() { + } + + /** full constructor */ + public AbstractProducts(ProductsId id) { + this.id = id; + } + + // Property accessors + + public ProductsId getId() { + return this.id; + } + + public void setId(ProductsId id) { + this.id = id; + } + +} \ No newline at end of file diff --git a/pagebean/src/dao/AbstractProductsId.java b/pagebean/src/dao/AbstractProductsId.java new file mode 100644 index 0000000..8150e8d --- /dev/null +++ b/pagebean/src/dao/AbstractProductsId.java @@ -0,0 +1,173 @@ +package dao; + +/** + * AbstractProductsId entity provides the base persistence definition of the + * ProductsId entity. @author MyEclipse Persistence Tools + */ + +public abstract class AbstractProductsId implements java.io.Serializable { + + // Fields + + private Integer proId; + private Integer userId; + private String proName; + private String contact; + private Float price; + private Float oprice; + private Integer type; + private Integer classifyId; + + // Constructors + + /** default constructor */ + public AbstractProductsId() { + } + + /** minimal constructor */ + public AbstractProductsId(Integer proId, Integer userId) { + this.proId = proId; + this.userId = userId; + } + + /** full constructor */ + public AbstractProductsId(Integer proId, Integer userId, String proName, + String contact, Float price, Float oprice, Integer type, + Integer classifyId) { + this.proId = proId; + this.userId = userId; + this.proName = proName; + this.contact = contact; + this.price = price; + this.oprice = oprice; + this.type = type; + this.classifyId = classifyId; + } + + // Property accessors + + public Integer getProId() { + return this.proId; + } + + public void setProId(Integer proId) { + this.proId = proId; + } + + public Integer getUserId() { + return this.userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getProName() { + return this.proName; + } + + public void setProName(String proName) { + this.proName = proName; + } + + public String getContact() { + return this.contact; + } + + public void setContact(String contact) { + this.contact = contact; + } + + public Float getPrice() { + return this.price; + } + + public void setPrice(Float price) { + this.price = price; + } + + public Float getOprice() { + return this.oprice; + } + + public void setOprice(Float oprice) { + this.oprice = oprice; + } + + public Integer getType() { + return this.type; + } + + public void setType(Integer type) { + this.type = type; + } + + public Integer getClassifyId() { + return this.classifyId; + } + + public void setClassifyId(Integer classifyId) { + this.classifyId = classifyId; + } + + public boolean equals(Object other) { + if ((this == other)) + return true; + if ((other == null)) + return false; + if (!(other instanceof AbstractProductsId)) + return false; + AbstractProductsId castOther = (AbstractProductsId) other; + + return ((this.getProId() == castOther.getProId()) || (this.getProId() != null + && castOther.getProId() != null && this.getProId().equals( + castOther.getProId()))) + && ((this.getUserId() == castOther.getUserId()) || (this + .getUserId() != null && castOther.getUserId() != null && this + .getUserId().equals(castOther.getUserId()))) + && ((this.getProName() == castOther.getProName()) || (this + .getProName() != null && castOther.getProName() != null && this + .getProName().equals(castOther.getProName()))) + && ((this.getContact() == castOther.getContact()) || (this + .getContact() != null && castOther.getContact() != null && this + .getContact().equals(castOther.getContact()))) + && ((this.getPrice() == castOther.getPrice()) || (this + .getPrice() != null && castOther.getPrice() != null && this + .getPrice().equals(castOther.getPrice()))) + && ((this.getOprice() == castOther.getOprice()) || (this + .getOprice() != null && castOther.getOprice() != null && this + .getOprice().equals(castOther.getOprice()))) + && ((this.getType() == castOther.getType()) || (this.getType() != null + && castOther.getType() != null && this.getType() + .equals(castOther.getType()))) + && ((this.getClassifyId() == castOther.getClassifyId()) || (this + .getClassifyId() != null + && castOther.getClassifyId() != null && this + .getClassifyId().equals(castOther.getClassifyId()))); + } + + public int hashCode() { + int result = 17; + + result = 37 * result + + (getProId() == null ? 0 : this.getProId().hashCode()); + result = 37 * result + + (getUserId() == null ? 0 : this.getUserId().hashCode()); + result = 37 * result + + (getProName() == null ? 0 : this.getProName().hashCode()); + result = 37 * result + + (getContact() == null ? 0 : this.getContact().hashCode()); + result = 37 * result + + (getPrice() == null ? 0 : this.getPrice().hashCode()); + result = 37 * result + + (getOprice() == null ? 0 : this.getOprice().hashCode()); + result = 37 * result + + (getType() == null ? 0 : this.getType().hashCode()); + result = 37 + * result + + (getClassifyId() == null ? 0 : this.getClassifyId() + .hashCode()); + return result; + } + +} \ No newline at end of file diff --git a/pagebean/src/dao/BaseHibernateDAO.java b/pagebean/src/dao/BaseHibernateDAO.java new file mode 100644 index 0000000..40bc925 --- /dev/null +++ b/pagebean/src/dao/BaseHibernateDAO.java @@ -0,0 +1,16 @@ +package dao; + +import org.hibernate.Session; + + +/** + * Data access object (DAO) for domain model + * @author MyEclipse Persistence Tools + */ +public class BaseHibernateDAO implements IBaseHibernateDAO { + + public Session getSession() { + return HibernateSessionFactory.getSession(); + } + +} \ No newline at end of file diff --git a/pagebean/src/dao/HibernateSessionFactory.java b/pagebean/src/dao/HibernateSessionFactory.java new file mode 100644 index 0000000..a2d103a --- /dev/null +++ b/pagebean/src/dao/HibernateSessionFactory.java @@ -0,0 +1,118 @@ +package dao; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; + +/** + * Configures and provides access to Hibernate sessions, tied to the + * current thread of execution. Follows the Thread Local Session + * pattern, see {@link http://hibernate.org/42.html }. + */ +public class HibernateSessionFactory { + + /** + * Location of hibernate.cfg.xml file. + * Location should be on the classpath as Hibernate uses + * #resourceAsStream style lookup for its configuration file. + * The default classpath location of the hibernate config file is + * in the default package. Use #setConfigFile() to update + * the location of the configuration file for the current session. + */ + private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; + private static final ThreadLocal threadLocal = new ThreadLocal(); + private static Configuration configuration = new Configuration(); + private static org.hibernate.SessionFactory sessionFactory; + private static String configFile = CONFIG_FILE_LOCATION; + + static { + try { + configuration.configure(configFile); + sessionFactory = configuration.buildSessionFactory(); + } catch (Exception e) { + System.err + .println("%%%% Error Creating SessionFactory %%%%"); + e.printStackTrace(); + } + } + private HibernateSessionFactory() { + } + + /** + * Returns the ThreadLocal Session instance. Lazy initialize + * the SessionFactory if needed. + * + * @return Session + * @throws HibernateException + */ + public static Session getSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + + if (session == null || !session.isOpen()) { + if (sessionFactory == null) { + rebuildSessionFactory(); + } + session = (sessionFactory != null) ? sessionFactory.openSession() + : null; + threadLocal.set(session); + } + + return session; + } + + /** + * Rebuild hibernate session factory + * + */ + public static void rebuildSessionFactory() { + try { + configuration.configure(configFile); + sessionFactory = configuration.buildSessionFactory(); + } catch (Exception e) { + System.err + .println("%%%% Error Creating SessionFactory %%%%"); + e.printStackTrace(); + } + } + + /** + * Close the single hibernate session instance. + * + * @throws HibernateException + */ + public static void closeSession() throws HibernateException { + Session session = (Session) threadLocal.get(); + threadLocal.set(null); + + if (session != null) { + session.close(); + } + } + + /** + * return session factory + * + */ + public static org.hibernate.SessionFactory getSessionFactory() { + return sessionFactory; + } + + /** + * return session factory + * + * session factory will be rebuilded in the next call + */ + public static void setConfigFile(String configFile) { + HibernateSessionFactory.configFile = configFile; + sessionFactory = null; + } + + /** + * return hibernate configuration + * + */ + public static Configuration getConfiguration() { + return configuration; + } + +} \ No newline at end of file diff --git a/pagebean/src/dao/IBaseHibernateDAO.java b/pagebean/src/dao/IBaseHibernateDAO.java new file mode 100644 index 0000000..ccc03cf --- /dev/null +++ b/pagebean/src/dao/IBaseHibernateDAO.java @@ -0,0 +1,12 @@ +package dao; + +import org.hibernate.Session; + + +/** + * Data access interface for domain model + * @author MyEclipse Persistence Tools + */ +public interface IBaseHibernateDAO { + public Session getSession(); +} \ No newline at end of file diff --git a/pagebean/src/dao/Products.hbm.xml b/pagebean/src/dao/Products.hbm.xml new file mode 100644 index 0000000..acce55c --- /dev/null +++ b/pagebean/src/dao/Products.hbm.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pagebean/src/dao/Products.java b/pagebean/src/dao/Products.java new file mode 100644 index 0000000..394c9bf --- /dev/null +++ b/pagebean/src/dao/Products.java @@ -0,0 +1,19 @@ +package dao; + +/** + * Products entity. @author MyEclipse Persistence Tools + */ +public class Products extends AbstractProducts implements java.io.Serializable { + + // Constructors + + /** default constructor */ + public Products() { + } + + /** full constructor */ + public Products(ProductsId id) { + super(id); + } + +} diff --git a/pagebean/src/dao/ProductsDAO.java b/pagebean/src/dao/ProductsDAO.java new file mode 100644 index 0000000..60f9d29 --- /dev/null +++ b/pagebean/src/dao/ProductsDAO.java @@ -0,0 +1,135 @@ +package dao; + +import java.util.List; +import org.hibernate.LockMode; +import org.hibernate.Query; +import org.hibernate.criterion.Example; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A data access object (DAO) providing persistence and search support for + * Products 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 hib.Products + * @author MyEclipse Persistence Tools + */ + +public class ProductsDAO extends BaseHibernateDAO { + private static final Logger log = LoggerFactory + .getLogger(ProductsDAO.class); + + // property constants + + public void save(Products transientInstance) { + log.debug("saving Products instance"); + try { + getSession().save(transientInstance); + log.debug("save successful"); + } catch (RuntimeException re) { + log.error("save failed", re); + throw re; + } + } + + public void delete(Products persistentInstance) { + log.debug("deleting Products instance"); + try { + getSession().delete(persistentInstance); + log.debug("delete successful"); + } catch (RuntimeException re) { + log.error("delete failed", re); + throw re; + } + } + + public Products findById(dao.ProductsId id) { + log.debug("getting Products instance with id: " + id); + try { + Products instance = (Products) getSession().get("hib.Products", id); + return instance; + } catch (RuntimeException re) { + log.error("get failed", re); + throw re; + } + } + + public List findByExample(Products instance) { + log.debug("finding Products instance by example"); + try { + List results = getSession().createCriteria("hib.Products") + .add(Example.create(instance)).list(); + 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 Products instance with property: " + propertyName + + ", value: " + value); + try { + String queryString = "from Products as model where model." + + propertyName + "= ?"; + Query queryObject = getSession().createQuery(queryString); + queryObject.setParameter(0, value); + return queryObject.list(); + } catch (RuntimeException re) { + log.error("find by property name failed", re); + throw re; + } + } + + public List findAll() { + log.debug("finding all Products instances"); + try { + String queryString = "from Products"; + Query queryObject = getSession().createQuery(queryString); + return queryObject.list(); + } catch (RuntimeException re) { + log.error("find all failed", re); + throw re; + } + } + + public Products merge(Products detachedInstance) { + log.debug("merging Products instance"); + try { + Products result = (Products) getSession().merge(detachedInstance); + log.debug("merge successful"); + return result; + } catch (RuntimeException re) { + log.error("merge failed", re); + throw re; + } + } + + public void attachDirty(Products instance) { + log.debug("attaching dirty Products instance"); + try { + getSession().saveOrUpdate(instance); + log.debug("attach successful"); + } catch (RuntimeException re) { + log.error("attach failed", re); + throw re; + } + } + + public void attachClean(Products instance) { + log.debug("attaching clean Products instance"); + try { + getSession().lock(instance, LockMode.NONE); + log.debug("attach successful"); + } catch (RuntimeException re) { + log.error("attach failed", re); + throw re; + } + } +} \ No newline at end of file diff --git a/pagebean/src/dao/ProductsId.java b/pagebean/src/dao/ProductsId.java new file mode 100644 index 0000000..bdf7b52 --- /dev/null +++ b/pagebean/src/dao/ProductsId.java @@ -0,0 +1,27 @@ +package dao; + +/** + * ProductsId entity. @author MyEclipse Persistence Tools + */ +public class ProductsId extends AbstractProductsId implements + java.io.Serializable { + + // Constructors + + /** default constructor */ + public ProductsId() { + } + + /** minimal constructor */ + public ProductsId(Integer proId, Integer userId) { + super(proId, userId); + } + + /** full constructor */ + public ProductsId(Integer proId, Integer userId, String proName, + String contact, Float price, Float oprice, Integer type, + Integer classifyId) { + super(proId, userId, proName, contact, price, oprice, type, classifyId); + } + +} diff --git a/pagebean/src/hibernate.cfg.xml b/pagebean/src/hibernate.cfg.xml new file mode 100644 index 0000000..d970cc4 --- /dev/null +++ b/pagebean/src/hibernate.cfg.xml @@ -0,0 +1,26 @@ + + + + + + + + + org.hibernate.dialect.MySQLDialect + + + jdbc:mysql://localhost:3306/myday + + root + mysql123 + + com.mysql.jdbc.Driver + + myday + + + + + \ No newline at end of file diff --git a/pagebean/src/model/PageBean.java b/pagebean/src/model/PageBean.java new file mode 100644 index 0000000..d3d4c7d --- /dev/null +++ b/pagebean/src/model/PageBean.java @@ -0,0 +1,129 @@ +package model; +import java.util.List; + + +public class PageBean { + private int pageNum; //ǰҳ,DZߴ + private int pageSize; //ÿҳʾ + private int totalRecord; //ܵļ¼ѯݿõ + + public int getPageNum() { + return pageNum; + } + + public void setPageNum(int pageNum) { + this.pageNum = pageNum; + } + + public int getPageSize() { + return pageSize; + } + + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + + public int getTotalRecord() { + return totalRecord; + } + + public void setTotalRecord(int totalRecord) { + this.totalRecord = totalRecord; + } + + public int getTotalPage() { + return totalPage; + } + + public void setTotalPage(int totalPage) { + this.totalPage = totalPage; + } + + public int getStartIndex() { + return startIndex; + } + + public void setStartIndex(int startIndex) { + this.startIndex = startIndex; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public int getStart() { + return start; + } + + public void setStart(int start) { + this.start = start; + } + + public int getEnd() { + return end; + } + + public void setEnd(int end) { + this.end = end; + } + + //Ҫ + private int totalPage; //ҳͨtotalRecordpageSizeԵ + //ʼҲݿҪӵڼݿʼãstartIndexpageSize + //֪limitݣܻÿҳҪʾ + private int startIndex; + + //ÿҳҪʾݷlist + private List list; + + //ҳʾҳ,ҳʾ12345ҳstartΪ1endΪ5Ҳ + private int start; + private int end; + + //ͨpageNumpageSizetotalRecordtatalPagestartIndex + //췽нpageNumpageSizetotalRecord + public PageBean(int pageNum,int pageSize,int totalRecord) { + this.pageNum = pageNum; + this.pageSize = pageSize; + this.totalRecord = totalRecord; + + //totalPage ҳ + if(totalRecord%pageSize==0){ + //˵ÿҳʾpageSizeݣûжһҳҪʾpageSizeݵ + this.totalPage = totalRecord / pageSize; + }else{ + //Ҫڼһҳʾݡ + this.totalPage = totalRecord / pageSize +1; + } + //ʼ + this.startIndex = (pageNum-1)*pageSize ; + //ʾ5ҳ + this.start = 1; + this.end = 5; + //ʾҳ㷨 + + if(totalPage <=5){ + //ҳС5ôendΪҳֵˡ + this.end = this.totalPage; + }else{ + //ҳ5ôҪݵǰǵڼҳжstartendΪˣ + this.start = pageNum - 2; + this.end = pageNum + 2; + + if(start <= 0){ + //統ǰҳǵ1ҳߵ2ҳôͲ + this.start = 1; + this.end = 5; + } + if(end > this.totalPage){ + //統ǰҳǵ2ҳһҳҲͬ + this.end = totalPage; + this.start = end - 5; + } + } + } +} diff --git a/pagebean/src/model/Product.java b/pagebean/src/model/Product.java new file mode 100644 index 0000000..45d18af --- /dev/null +++ b/pagebean/src/model/Product.java @@ -0,0 +1,56 @@ +package model; + + +public class Product { + private int proId; + private int userId; + private String proName; //Ʒ + private String contact; //ϵʽ + private float price; //ת + private float oprice; //ԭ + private int classifyId; // + private int type; + + public int getProId() { + return proId; + } + public void setProId(int proId) { + this.proId = proId; + } + public int getUserId() { + return userId; + } + public void setUserId(int userId) { + this.userId = userId; + } + public String getProName() { + return proName; + } + public void setProName(String proName) { + this.proName = proName; + } + public String getContact() { + return contact; + } + public void setContact(String contact) { + this.contact = contact; + } + public float getPrice() { + return price; + } + public void setPrice(float price) { + this.price = price; + } + public float getOprice() { + return oprice; + } + public void setOprice(float oprice) { + this.oprice = oprice; + } + public int getType() { + return type; + } + public void setType(int type) { + this.type = type; + } +} diff --git a/pagebean/src/service/ProductService.java b/pagebean/src/service/ProductService.java new file mode 100644 index 0000000..538a0d5 --- /dev/null +++ b/pagebean/src/service/ProductService.java @@ -0,0 +1,27 @@ +package service; +import dao.ProductsDAO; + +import java.util.List; + +import model.PageBean; +import model.Product; + + +public class ProductService { + + + @SuppressWarnings("unchecked") + public PageBean showAllProductWithPage(int pageNum,int pageSize){ + ProductsDAO pro=new ProductsDAO(); + List allProduct = pro.findAll(); + int totalRecord = allProduct.size(); + + PageBean pb= new PageBean(pageNum,pageSize,totalRecord); + + int startIndex = pb.getStartIndex(); + +// pb.setList(ProductsDAO.findAll(startIndex,pageSize)); + pb.setList(pro.findAll()); + return pb; + } +}