• 19.10.30笔记-文件下载


    #19.10.30笔记-文件下载

    1,文件下载的流程:上传文件到服务器上,然后从服务器上输出到浏览器上供用户下载,如果是批量下载的话,则多一个步骤,上传服务器,然后打包,下载。

    1.1 上午遇到的问题:需要批量下载文件,这就需要先上传每个文件到服务器,然后统一打包下载。总体的思路有问题,也体会到了函数的魅力:自己的做法: 1. 将上传服务器的过程写入到导出每个文件的过程中了,这就导致如果批量导出,需要导出单个文件的内容,但是导出每个文件里面都有request和response对象,然后批量导出的话也有request和response对象,这就导致下载框出不来;解决方案:将上传服务器的过程封装成工具类中的一个方法,然后调用上传服务器的代码,解决了多个request和response对象和下载框无法出来的现象。现在待解决的问题:下载到浏览器的代码在controller层中冗余量太多,也需要封装成方法。在controller层的时候只能调用包,尽量少一些大量逻辑判断代码,整洁controller层。

    1. 如果采用ajax的话并且需要传入数组,则不能使用下载框,具体写法可以写成这样image-20191030204536841后台接收的话:image-20191030204621635

    10.31笔记

    1. 后台

      1. 首先是mapper中的映射关系,之前一直是手写mapper,比如如果两个表中有外键,需要从一个表获取到另一个表中的数据,就需要级联查询,如果是三个表就需要三个表的级联查询
      @Select("select distinct username from user where user_id in( select user_id from application where app_id in (select app_id from problem where pro_state='1' and pro_res=0 ))")
      List<String> getAllPassUsername();
      

      ​ 三个表的级联查询,目的就是为了查询出来user表中的username。

      还有一个问题:如果你不采用映射关系,则会出现取值为null,比如sql语句 select * from problem where pro_id=1;因为mybatis没有办法将数据源和你的java的实体类中的数据一一映射,有两个解决方法,一种就是在注解中把所有需要的字段手动写出来,比如:

      @Select("select pro_id as proId,pro_title as proTitle,pro_description as proDescription,pro_solution as proSolution,pro_result as proResult,pro_level as proLevel," +        "pro_state as proState,pro_company as proCompany,pro_tag as proTag,pro_res as proRes,app_id as appId,pro_solove_com as proSoloveCom,pro_abstract as proAbstract" +        ",pro_industry as proIndustry,pro_keyword as proKeyword,pro_function as proFunction,"+        " pro_backgroud as proBackgroud,pro_theory as proTheory from problem where pro_res=0 and pro_state='0'")
      List<Problem> selectByCategoryAndFail();
      

      另一种方法就是采用resultMap的方法进行映射:

      @Select("SELECT * FROM problem WHERE app_id = #{appId}")
      @Results(id="problemMap",value = {        
          @Result(id = true,column = "pro_id", property = "proId", jdbcType = JdbcType.INTEGER, javaType = Integer.class),        
          @Result(column = "pro_title", property = "proTitle", jdbcType = JdbcType.VARCHAR, javaType = String.class),        
          @Result(column = "pro_description", property = "proDescription", jdbcType = JdbcType.INTEGER, javaType = Integer.class),        
          @Result(column = "pro_solution", property = "proSolution", jdbcType = JdbcType.VARCHAR, javaType = String.class),        
          @Result(column = "pro_level", property = "proLevel", jdbcType = JdbcType.VARCHAR, javaType = String.class),       
          @Result(column = "pro_state", property = "proState", jdbcType = JdbcType.VARCHAR, javaType = String.class),        
          @Result(column = "pro_company", property = "proCompany", jdbcType = JdbcType.VARCHAR, javaType = String.class),})
      List<Problem> selectByAppId(Integer appId);
      

    这样做的好处还有一个,如果出现别的表的时候,直接来一对一或者一对多的关系映射,直接就映射成功了,但是这种经常使用在两个表有关系的时候,三个表不做思考,应该也是可以的。通过嵌套进去

    比如:

    @Select("SELECT * FROM application")
        @Results(id="listAll",value={
                @Result(id = true,column = "app_id", property = "appId", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
                @Result(column = "app_nowlevel", property = "appNowlevel", jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "app_applylevel", property = "appApplylevel", jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "app_scholar",property = "appScholar",jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "app_cellphone", property = "appCellphone",jdbcType = JdbcType.VARCHAR,javaType =String.class),
                @Result(column = "app_imag", property = "appImag",jdbcType = JdbcType.VARCHAR,javaType =String.class),
                @Result(column = "app_experience", property = "appExperience", jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "app_advisory", property = "appAdvisory", jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "app_problem", property = "appProblem", jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "app_problem_num", property = "appProblemNum", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
                @Result(column = "app_generalpro_num", property = "appGeneralproNum", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
                @Result(column = "app_importantpro_num", property = "appImportantproNum", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
                @Result(column = "app_keypro_num", property = "appKeyproNum", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
                @Result(column = "app_patent", property = "appPatent", jdbcType = JdbcType.VARCHAR, javaType = String.class),
                @Result(column = "user_id",property = "user",one = @One(select = "cn.tj.mapper.UserMapper.selectByUserId"))
        })
        List<Application> listAll();
    
    1. 前台

      1. 如果想从前台获取到后台的值:

        <table id="informationList" class="table table-sm table-striped table-bordered text-center">    <thead>    <tr>        <th>姓名</th>        <th>原级别</th>        <th>申请级别</th>        <th>联系电话</th>        <th>qq号</th>        <th>审核状态</th>        <th>操作</th>    </tr>    </thead>    <tbody>    <!--开始遍历结果集-->    <!--开始遍历结果集-->    <#if applicationList??>    <#list applicationList as info>    <tr>        <td>            <label class="checkbox-inline">                <input type="checkbox" class="checkbox_select" name="failItems" id="items" value="" >${info.user.userName}            </label>        </td>        <!--标题,a属性-->        <td>            <span>                <a href="/case/displayCase/">${info.appNowlevel}</a>            </span>        </td>        <!--难题级别-->        <td>${info.appApplylevel}</td>        <!--所属行业-->        <!--来源企业-->        <td>            ${info.appCellphone}        </td>        <!--关键字-->        <td>            ${info.user.userQq}        </td>        <!--实现功能-->        <td>            通过        </td>        <!--操作-->        <td>            <a href="/case/exportApproveCase/">导出<i class="fa fa-file-word-o" aria-hidden="true"></i></a>        </td>    </tr>    </#list></#if></tbody></table>
        

    11.3笔记

    1. datatable中如果不想对前几列进行排序,使用以下代码:

      $('#informationList').DataTable({//对0,1,2列不排序"columnDefs": [ {"orderable": false,"targets": [0,1,2]}],//如果需要重载页面则需要销毁页面,以便于重新加载destroy:true,//自带的搜索框,如果不需要则可以设置false,默认为truesearching:true,//表格分页paging: true,//汉化language: {"sProcessing": "处理中...","sLengthMenu": "显示 _MENU_ 项结果","sZeroRecords": "没有匹配结果","sInfo": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项","sInfoEmpty": "显示第 0 至 0 项结果,共 0 项","sInfoFiltered": "(由 _MAX_ 项结果过滤)","sInfoPostFix": "","sSearch": "简单搜索:","sUrl": "","sEmptyTable": "表中数据为空","sLoadingRecords": "载入中...","sInfoThousands": ",","oPaginate": {"sFirst": "首页","sPrevious": "上页","sNext": "下页","sLast": "末页"},"oAria": {"sSortAscending": ": 以升序排列此列","sSortDescending": ": 以降序排列此列"}}});

    2. image-20191112202047253

    11.16:对代码进行优化,抽离出下载的方法,代码更加简洁。

    ModelAndView重定向:

    modelAndView.setViewName("redirect:/manage/messageManagement"); redirect的内容是请求的地址,不是html的名字

    博客网站 https://yamon.top 个人网站 https://yamon.top/resume GitHub网站 https://github.com/yamonc 欢迎前来访问
  • 相关阅读:
    P1106 删数问题 / U83355 删数问题【升级版】
    P1955 [NOI2015] 程序自动分析
    P4447 [AHOI2018初中组]分组
    P1308 [NOIP2011 普及组] 统计单词数
    Django | 页面数据的缓存与使用
    Python 虚拟环境 | Mac/Linux下如何避坑安装配置Virtualenv
    python虚拟环境 | virtualenv 的简单使用 (图文)
    机器学习 | 浅谈K-近邻算法
    特征缩放 | 归一化和标准化 (下)
    简析方差、标准差与数值离散程度
  • 原文地址:https://www.cnblogs.com/chenyameng/p/12369565.html
Copyright © 2020-2023  润新知