SSM整合 上传下载之添加商品

Posted liuoop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合 上传下载之添加商品相关的知识,希望对你有一定的参考价值。

上传下载细节:

 导入xml配置文件!!

技术图片

 

 

 

一、建表:

 1 DROP TABLE IF EXISTS `t_house`;
 2 CREATE TABLE `t_house` (
 3   `h_id` int(11) NOT NULL AUTO_INCREMENT,
 4   `h_name` varchar(64) NOT NULL,
 5   `h_rect` double(10,2) NOT NULL,
 6   `h_date` date NOT NULL,
 7   `h_loc1` varchar(255) NOT NULL,
 8   `h_loc2` varchar(255) NOT NULL,
 9   `h_img` varchar(120) NOT NULL,
10   `h_userId` int(11) NOT NULL,
11   PRIMARY KEY (`h_id`)
12 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

 

二、首先注入maven依赖:

pom.xml

 1 <dependency>
 2       <groupId>org.projectlombok</groupId>
 3       <artifactId>lombok</artifactId>
 4       <version>1.18.4</version>
 5       <scope>provided</scope>
 6     </dependency>
 7     <!--文件上传下载 -->
 8     <dependency>
 9       <groupId>commons-io</groupId>
10       <artifactId>commons-io</artifactId>
11       <version>2.4</version>
12     </dependency>
13     <!--用来帮助进行IO功能开发 -->
14     <dependency>
15       <groupId>commons-fileupload</groupId>
16       <artifactId>commons-fileupload</artifactId>
17       <version>1.3</version>
18     </dependency>

 

三、添加配置:

springmvc.xml

1 <!-- 多部分文件上传 M kb byte-->
2     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3         <property name="maxUploadSize" value="104857600"/>
4         <property name="defaultEncoding" value="UTF-8"></property>
5     </bean>

 

四、编辑插入语句:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.etc.dao.HouseDAO">
 4     <insert id="add">
 5  INSERT INTO `t_house` (
 6     `h_name`,
 7     `h_rect`,
 8     `h_date`,
 9     `h_loc1`,
10     `h_loc2`,
11     `h_img`,
12     `h_userId`
13 )
14 VALUES
15     (
16         #hName,
17         #hRect,
18         #hDate,
19         #hLoc1,
20         #hLoc2,
21         #hImg,
22         #userId
23     );
24 </insert>
25 
26 
27 </mapper>

 

五、建立实体类:

HouseVO :
 1 package com.etc.Vo;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 import org.springframework.format.annotation.DateTimeFormat;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 import java.util.Date;
10 
11 @Data
12 @AllArgsConstructor
13 @NoArgsConstructor
14 public class HouseVO 
15     private String title;
16     private Double floorage;
17     @DateTimeFormat(pattern = "yyyy-MM-dd")
18     private Date houseDate;
19     private String districtId;
20     private String streetId;
21     private MultipartFile hImg;
22 
House :
 1 package com.etc.entity;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 import java.util.Date;
 8 
 9 @Data
10 @AllArgsConstructor
11 @NoArgsConstructor
12 public class House 
13     private Integer hId;
14     private String hName;
15     private Double hRect;
16     private Date hDate;
17     private String hLoc1;
18     private String hLoc2;
19     private String hImg;
20     private int userId;
21 
HouseConverter :
 1 package com.etc.converter;
 2 
 3 import com.etc.Vo.HouseVO;
 4 import com.etc.entity.House;
 5 
 6 public class HouseConverter 
 7     public static House convert(HouseVO houseVO, String img,int id) 
 8         House house = new House();
 9         house.setHName(houseVO.getTitle());
10         house.setHRect(houseVO.getFloorage());
11         house.setHDate(houseVO.getHouseDate());
12         house.setHLoc1(houseVO.getDistrictId());
13         house.setHLoc2(houseVO.getStreetId());
14         house.setHImg(img);
15         house.setUserId(id);
16         return house;
17     
18 

 

六、编写接口:

1 package com.etc.dao;
2 
3 import com.etc.entity.House;
4 
5 public interface HouseDAO 
6 
7     void add(House house);
8 
9 

 

七、编写service:

 1 package com.etc.servise;
 2 
 3 import com.etc.dao.HouseDAO;
 4 import com.etc.entity.House;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 @Service
 9 public class HouseService 
10 
11     @Autowired
12     private HouseDAO houseDAO;
13 
14     public void add(House house)
15         houseDAO.add(house);
16     
17 
18 
19 

 

八、编写控制层:

 1 package com.etc.controller;
 2 
 3 import com.etc.Vo.HouseVO;
 4 import com.etc.converter.HouseConverter;
 5 import com.etc.entity.House;
 6 import com.etc.entity.User;
 7 import com.etc.servise.HouseService;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Controller;
10 import org.springframework.ui.Model;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 
13 import javax.servlet.http.HttpSession;
14 import java.io.File;
15 import java.io.IOException;
16 
17 @Controller
18 @RequestMapping("/house")
19 public class HouseController 
20 
21     public static final String UPLOAD_DIR="D:\\\\images\\\\";
22 
23     @Autowired
24     private HouseService houseService;
25 
26     @RequestMapping("/add")
27     public String add(HouseVO houseVO, Model model, HttpSession session) throws IOException 
28         //获取session中的user对象
29         User user=(User) session.getAttribute("user");
30         //拿到user对象的id
31         int userId=user.getUserId();
32         //设置文件上传的路径
33         houseVO.getHImg().transferTo(new File(UPLOAD_DIR+houseVO.getHImg().getOriginalFilename()));
34         //建立house对象
35         House house=HouseConverter.convert(houseVO,houseVO.getHImg().getOriginalFilename(),userId);
36         //调用添加方法
37         houseService.add(house);
38         return "list";
39     
40 

 

九、页面展示:

技术图片fabu.jsp

 

以上是关于SSM整合 上传下载之添加商品的主要内容,如果未能解决你的问题,请参考以下文章

Java SSM 项目实战 day02 功能介绍,SSM整合,数据库和IDEA的maven工程搭建,产品信息查询和添加

01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层

ssh整合问题总结--在添加商品模块实现图片(文件)的上传

02 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之Service层

03 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之web层

学习模仿天猫商城毕业设计SSM商城系统笔记