SSM之文件上传

SSM之文件上传

这是我写SSM项目的时候,写的功能实现!!!
执行顺序:

  1. 前台到controller。
  2. controller ===》 service/serviceImpl。
  3. service/serviceImpl ===》mapper。
  4. mapper ==== 》mapper.xml。

例如添加功能

实体类:没有什么特殊的,get,set,tostring,有参,无参就省略了!!!

public class House {
    private Integer hid;

    private String harea;

    private String hcommunity;

    private String hunitnumber;

    private Integer hfloor;

    private String hroomno;

    private String hacreage;

    private Integer hdirection;

    private Integer hfitment;

    private Integer hisdoubleair;

    private Integer hlimit;

    private String hfacility;

    private Float hprice;

    private Integer hstatus;

    private String himg;

    private String haddress;

    private Date haddtime;

    private Date hupdatetime;
}

工具类

public interface Tool {
    String IMAGE_URL = "E://images";

    static File upLoadImage(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        File dir = new File(Tool.IMAGE_URL,System.currentTimeMillis()+fileName);
        if(!dir.getParentFile().exists()){
            dir.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dir);
        } catch (IOException e) {
            System.out.println("文件上传失败!!!");
        }
        return dir;
    }
}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 显示sql语句 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 命名转换 -->
    </settings>
    <typeAliases>
        <package name="com.zhiyou100.entity"/><!--给实体类起别名 -->
    </typeAliases>
    <!--分页插件:注意的是plugins标签要放在typeAliases后面,否则会报错-->
    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- pageNum<=0 时会查询第一页   reasonable=true -->
            <!-- 指定数据库方言   helperDialect=mysql -->
            <property name="params" value="reasonable=true;helperDialect=mysql"/>
        </plugin>
    </plugins>
    <mappers>
    	<!-- 引入所有的sql映射文件 -->
        <package name="com.zhiyou100.mapper"/>
    </mappers>
</configuration>

spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 创建数据源 -->
    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db_zygy"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!-- 创建sqlsession的bean -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource"  ref="ds" />
        <!-- 引入所有的sql映射文件 -->
        <!-- <property name="mapperLocations" value="classpath:com/zhiyou100/mapper/*.xml"/> -->
        <!-- 引入mybatis的核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis_config.xml"/>
    </bean>
    <!-- 创建事务管理bean -->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"/>
    </bean>
    <!-- 为所有的mapper接口创建实现类对象 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zhiyou100.mapper"/>
    </bean>
</beans>

springMVC-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 指定扫描创建bean -->
    <context:component-scan base-package="com.zhiyou100"/>
    <!-- 注册注解的处理器适配器和处理器映射器 -->
<!--    <mvc:annotation-driven/>-->
    <mvc:annotation-driven />

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 配置多部件表单的解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--上传文件的最大大小,单位为字节 -->
        <property name="maxUploadSize" value="17367648787"/>
        <!-- 上传文件的编码 -->
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    <!-- 配置拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.zhiyou100.inteceptor.Lanjie" />
        </mvc:interceptor>
    </mvc:interceptors>

</beans>


前台页面

     <tr>
        <td>房屋图片</td>
         <td colspan="3" class="control">
             <input type="file" name="imgFile" placeholder="房屋图片">
         </td>
     </tr>

controller控制层

@Controller
@RequestMapping("/house")
public class HouseController {

    @Autowired
    HouseServiceImpl houseServiceImpl;
    @Autowired
    HttpSession session;

    /**
     * 添加房屋
     */
    @RequestMapping("/houseadd.action")
    public String houseAddMethod(House house, @RequestParam("imgFile") MultipartFile file, HttpServletRequest request) {
        System.out.println("房屋添加" + house);
        File dir = Tool.upLoadImage(file);
        house.setHimg(dir.getPath().substring(2));                
        int addMethod = houseServiceImpl.houseAddMethod(house);
        System.out.println("添加行数" + addMethod);
        session.setAttribute("house", house);
        return "redirect:/house/houseall.action";
    }
}

serviceImpl数据层

@Service
public class HouseServiceImpl implements HouseService{
    @Autowired
    HouseMapper houseMapper;

    /**添加房屋*/
    @Override
    public int houseAddMethod(House house) {
        house.setHaddtime(new Date());
        house.setHupdatetime(new Date());
        return houseMapper.insertSelective(house);
    }
}

mapper持久层

public interface HouseMapper {

	/** 添加房屋
     * @return*/
    int insertSelective(House record);
}

mapper.xml

<!--添加房屋-->
  <insert id="insertSelective" parameterType="com.zhiyou100.entity.House" >
    insert into house
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="hid != null and hid != ''" >
        hid,
      </if>
      <if test="harea != null and harea != ''" >
        harea,
      </if>
      <if test="hcommunity != null and hcommunity != ''" >
        hcommunity,
      </if>
      <if test="hunitnumber != null and hunitnumber != ''" >
        hunitNumber,
      </if>
      <if test="hfloor != null and hfloor != ''" >
        hfloor,
      </if>
      <if test="hroomno != null and hroomno != ''" >
        hroomNo,
      </if>
      <if test="hacreage != null and hacreage != ''" >
        hacreage,
      </if>
      <if test="hdirection != null and hdirection != ''" >
        hdirection,
      </if>
      <if test="hfitment != null and hfitment != ''" >
        hfitment,
      </if>
      <if test="hisdoubleair != null and hisdoubleair != ''" >
        hisDoubleAir,
      </if>
      <if test="hlimit != null and hlimit != ''" >
        hlimit,
      </if>
      <if test="hfacility != null and hfacility != ''" >
        hfacility,
      </if>
      <if test="hprice != null and hprice != ''" >
        hprice,
      </if>
      <if test="hstatus != null and hstatus != ''" >
        hstatus,
      </if>
      <if test="himg != null and himg != ''" >
        himg,
      </if>
      <if test="haddress != null and haddress != ''" >
        haddress,
      </if>
      <!--<if test="haddtime != null and haddtime != ''" >
        haddtime,
      </if>
      <if test="hupdatetime != null and hupdatetime != ''" >
        hupdateTime,
      </if>-->
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="hid != null" >
        #{hid,jdbcType=INTEGER},
      </if>
      <if test="harea != null" >
        #{harea,jdbcType=VARCHAR},
      </if>
      <if test="hcommunity != null" >
        #{hcommunity,jdbcType=VARCHAR},
      </if>
      <if test="hunitnumber != null" >
        #{hunitnumber,jdbcType=VARCHAR},
      </if>
      <if test="hfloor != null" >
        #{hfloor,jdbcType=INTEGER},
      </if>
      <if test="hroomno != null" >
        #{hroomno,jdbcType=VARCHAR},
      </if>
      <if test="hacreage != null" >
        #{hacreage,jdbcType=VARCHAR},
      </if>
      <if test="hdirection != null" >
        #{hdirection,jdbcType=INTEGER},
      </if>
      <if test="hfitment != null" >
        #{hfitment,jdbcType=INTEGER},
      </if>
      <if test="hisdoubleair != null" >
        #{hisdoubleair,jdbcType=INTEGER},
      </if>
      <if test="hlimit != null" >
        #{hlimit,jdbcType=INTEGER},
      </if>
      <if test="hfacility != null" >
        #{hfacility,jdbcType=VARCHAR},
      </if>
      <if test="hprice != null" >
        #{hprice,jdbcType=REAL},
      </if>
      <if test="hstatus != null" >
        #{hstatus,jdbcType=INTEGER},
      </if>
      <if test="himg != null" >
        #{himg,jdbcType=VARCHAR},
      </if>
      <if test="haddress != null" >
        #{haddress,jdbcType=VARCHAR},
      </if>
   <!--   <if test="haddtime != null" >
        #{haddtime,jdbcType=TIMESTAMP},
      </if>
      <if test="hupdatetime != null" >
        #{hupdatetime,jdbcType=TIMESTAMP},
      </if>-->
    </trim>
  </insert>

完整的前台页面为

<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2020/8/27
  Time: 14:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>公寓管理系统 - 添加房屋信息</title>
    <link rel="stylesheet" href="<c:url value="/css/main.css"/>">
    <link rel="stylesheet" href="<c:url value="/font-awesome/css/font-awesome.css"/>">
</head>
<body>
    <div class="box">
        <h3>添加房屋信息</h3>
        <form action="<c:url value="/house/houseadd.action"/>" method="post" enctype="multipart/form-data">
            <table class="form-table">
                <tr>
                    <td>地区</td>
                    <td colspan="3" class="control">
                        <input type="text" name="harea" placeholder="填写地区">
                    </td>
                </tr>
                <tr>
                    <td>小区名字</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hcommunity" placeholder="小区名字">
                    </td>
                </tr>
                <tr>
                    <td>单元号</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hunitnumber" placeholder="单元号">
                    </td>
                </tr>
                <tr>
                    <td>楼层</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hfloor" placeholder="楼层">
                    </td>
                </tr>
                <tr>
                    <td>房间号</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hroomno" placeholder="房间号">
                    </td>
                </tr>
                <tr>
                    <td>面积(平米)</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hacreage" placeholder="面积">
                    </td>
                </tr>
                <tr>
                    <td>朝向</td>
                    <td colspan="3" class="control">
                        <select name="hdirection">
                            <option value = "1" selected>东</option>
                            <option value = "2">南</option>
                            <option value = "3">西</option>
                            <option value="4"></option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>装修</td>
                    <td colspan="3" class="control">
                        <select name="hfitment">
                            <option value = "1" selected>毛坯</option>
                            <option value = "2">简装</option>
                            <option value = "3">精装</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>是否双气</td>
                    <td colspan="3" class="control">
                        <select name="hisdoubleair">
                            <option value = "1" selected>双无</option>
                            <option value = "2">暖气</option>
                            <option value = "3">双气</option>
                            <option value="4">双气</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>限住人数</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hlimit" placeholder="限住人数">
                    </td>
                </tr>
                <tr>
                    <td>配套设施</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hfacility" placeholder="配套设施">
                    </td>
                </tr>
                <tr>
                    <td>日租价格(元/月)</td>
                    <td colspan="3" class="control">
                        <input type="text" name="hprice" placeholder="价格">
                    </td>
                </tr>
                <tr>
                    <td>房屋状态</td>
                    <td colspan="3" class="control">
                        <select name="hstatus">
                            <option value="2">未出租</option>
                            <option value="1">已出租</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>房屋图片</td>
                    <td colspan="3" class="control">
                        <input type="file" name="imgFile" placeholder="房屋图片">
                    </td>
                </tr>
                <tr>
                    <td>完整地址信息</td>
                    <td colspan="3" class="control">
                        <input type="text" name="haddress" placeholder="完整地址信息">
                    </td>
                </tr>
            </table>
            <div class="buttons">
                <input class="btn btn-primary va-bottom" type="submit" value="保存">&nbsp;&nbsp;
                <a class="btn btn-default" href="javascript:history.go(-1)">返回</a>
            </div>
        </form>
    </div>
</body>
</html>

好了,内容到此为止哦!有用的话就点个赞哈,收藏会更好,谢谢美女帅哥们啦!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值