您的位置:首页 > 其它

一个不知名的项目--Day04

2017-08-20 23:16 169 查看

day04

后台开发–添加商品

表结构



sevlset

Product product = VoUtils.ParseVo(Product.class, request.getParameterMap());
try {
Part img = request.getPart("pimg");
String pprice = request.getParameter("pprice");
String pnum = request.getParameter("pnum");
ProdService instence = BasicFactory.factory.getInstence(ProdService.class);
instence.checkedProduct(product, pprice, pnum);
if (img.getSize()<=0) {
throw new MsgException("图片没有上传");
}
if (!img.getContentType().split("/")[0].equals("image")){
throw new MsgException("你上传的不是图片");
}
product.setPimgurl(instence.saveImge(img,getServletContext().getRealPath("")));
instence.saveProduct(product);
} catch (MsgException e) {
e.printStackTrace();
request.setAttribute("msg", e.getMessage());
request.setAttribute("produt",product);
request.getRequestDispatcher("/back/product_add.jsp").forward(request, response);
return;
}
response.getWriter().write("<h1>添加商品成功<h1>");
response.setHeader("refresh","3;url="+request.getContextPath()+"back/product_add.jsp");


业务层

public String saveImge(Part img, String realPath) throws MsgException {
StringBuilder path=new StringBuilder("/WEB-INF/productImg");

String str=Integer.toHexString( UUID.randomUUID().toString().hashCode());
for (int i=0;i<str.length();i++){
path.append("/").append(str.charAt(i));
}
path.insert(0,realPath);
File file=new File(path.toString());
file.mkdirs();
path.append("/").append(UUID.randomUUID().toString()).append(".")
.append(img.getContentType().split("/")[1]);
try {
img.write(path.toString());
} catch (IOException e) {
e.printStackTrace();
throw new MsgException("图片保存失败");
}
return path.delete(0,realPath.length()).toString();
}


持久层

public void saveProduct(Product product) {
String sql="insert into tb_product (pid,pname,pprice,pnum,pimgurl,pinfo,pcategory) " +
" values(?,?,?,?,?,?,?)";
DaoUtils.upDate(sql,product.getPid(),product.getPname(),product.getPprice(),
product.getPnum(),product.getPimgurl(), product.getPinfo(), product.getPcategory());
}


数据填充程序

{

String repath="C:\\Users\\honey\\Desktop\\xxxx\\web";
String[] cate={"手机数码","电脑平板","家用电器","汽车用品","食品饮料","图书杂志","服装服饰","理财产品"} ;
for (int ii=0;ii<50;ii++) {
BufferedImage bi =new BufferedImage(600,600,BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.setColor(new Color(255,255,255));
g2d.fillRect(0,0,600,600);
g2d.setFont(new Font("微软雅黑",Font.BOLD,50));
g2d.setColor(new Color(RandomVerifyUtils.RandomInt(0,234),RandomVerifyUtils.RandomInt(0,234),RandomVerifyUtils.RandomInt(0,234)));
String name = "测试商品" + Integer.toHexString(UUID.randomUUID().toString().hashCode());
StringBuilder path = new StringBuilder("/WEB-INF/productImg");
String str1 = Integer.toHexString(UUID.randomUUID().toString().hashCode());
for (int i = 0; i < str1.length(); i++) {
path.append("/").append(str1.charAt(i));
}
path.insert(0, repath);
File file = new File(path.toString());
file.mkdirs();
path.append(UUID.randomUUID().toString()).append(".jpg");
System.out.println("name = " + name);
g2d.drawString(name, 50, 300);
try {
ImageIO.write(bi, "JPEG", new FileOutputStream(path.toString()));
String sql = "insert into tb_product (pid,pname,pprice,pnum,pimgurl,pinfo,pcategory) " +
" values(?,?,?,?,?,?,?)";
DaoUtils.upDate(sql,
UUID.randomUUID().toString(),
name,
RandomVerifyUtils.RandomInt(0, 10000),
RandomVerifyUtils.RandomInt(0, 100000),
path.delete(0, repath.length()).toString(),
Integer.toHexString(UUID.randomUUID().toString().hashCode()),
cate[RandomVerifyUtils.RandomInt(0, cate.length - 1)]);
} catch (IOException e) {
e.printStackTrace();
}
}
}


jsp代码就不放



就放个图吧

商品展示

图片转发

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//以后可以装防盗链
String imgurl = request.getParameter("imgurl");

request.getRequestDispatcher(imgurl).forward(request, response);
}


servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProdService instence = BasicFactory.factory.getInstence(ProdService.class);
String begin = request.getParameter("begin");
int now=0;
if (!VoUtils.isNull(begin)){
now=Integer.parseInt(begin);
}
List<Product> list=instence.getProdList(now*15,15);
int pagesum=instence.getpage();
if (pagesum>15){
pagesum/=15;
}else{
pagesum=1;
}
if ((now+1)>pagesum){
now--;
}
request.setAttribute("prodlist",list);
request.setAttribute("pagesum",pagesum);
request.setAttribute("now",now);
System.out.println("list.size() = " + list.size());
request.getRequestDispatcher("/back/product_list.jsp").forward(request, response);
}


业务层

@Override
public void saveProduct(Product product) {
pd.saveProduct(product);
}

@Override
public List<Product> getProdList(int begin,int end) {
return pd.selectProduct(begin,end);
}

@Override
public int getpage() {
return pd.getCount();
}


持久层

//获取商品列表
public List<Product> selectProduct(int begin,int end) {
String sql="select pid,pname,pprice,pnum,pimgurl,pinfo,pcategory from tb_product limit ?,?";

return DaoUtils.query(sql,new BeanListHandle<Product>(Product.class),begin,end);
}

@Override  //获取数据条目总数
public int getCount() {
String sql="SELECT count(*) from tb_product ";
return DaoUtils.query(sql, new ResultSetHandle<Integer>() {
@Override
public Integer handle(ResultSet rs) throws SQLException {
if (rs.next()){
return  rs.getInt(1);
}
return -1;
}
});




总结

今天出去完了一下,回来被css,html卡了很久一脸头大,商品列表本来不想做分页的,那个页面有点问题,就做了简单的分页

其他也没什么普通的业务逻辑

项目状态



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  电商 逻辑 分页