作一個簡單下載功能的記錄

給同事作一個下載的功能,將數據庫關聯查詢後生產csv文件供下載,期間遇到的小問題,記錄下。java

1,數據庫的關聯查詢nginx

  查詢出來的數據結構是:一個表TABLE_A的部分數據,和TABLE_A的每條數據在TABLE_B表中的個數sql

  mapper.xml文件 sql語句(下面的方式可能會引起一些問題,下面講)數據庫

  <resultMap id="aAndCount" type="com.model.AandCount">tomcat

    <result column="follow_count" property="followCount" jdbcType="INTEGER">服務器

    <association column="a.id" property="dxzf" javaType="com.model.Dxzf">數據結構

      <id column="id" jdbcType="INTEGER" property="id"/>app

      <result column="name" jdbcType="VARCHAR" property="name"/>server

    </association>xml

  </resultMap>

  <select id="queryAandCount" resultMap="aAndCount">

    select count(a.id) follow_count , a.id id , a.name name

    from TABLE_A a

    inner join TABLE_B b on TABLE_A.id = TBALE_B.aId and TABLE_A.status = 1

    group by aId 

    order by follow_count desc

    limit 100

  </select>

  而後在aMapper.java中

  List<AandCount> queryAandCount();

  當用service調用這個mapper.queryAandCount()的時候,返回的數據與預想的不同.

  具體是:follow_count 字段沒有重複的數據,即 只有惟一性的數據 14,13,12,11,... 而把sql放到客戶端執行,真實數據是 14,13,13,12,12,11...  

  懷疑是把 follow_count 作了惟一性處理。

  因此修改了 mapper.xml文件 sql語句 以下(由於 a表的id是返回的數據中惟一性的,將其添加到第一個返回值中)

     <resultMap id="aAndCount" type="com.model.AandCount">

    <result column="a_id" property="aId" jdbcType="INTEGER">

    <result column="follow_count" property="followCount" jdbcType="INTEGER">

    <association column="a.id" property="dxzf" javaType="com.model.Dxzf">

      <id column="id" jdbcType="INTEGER" property="id"/>

      <result column="name" jdbcType="VARCHAR" property="name"/>

    </association>

  </resultMap>

  <select id="queryAandCount" resultMap="aAndCount">

    select a.id a_id , count(a.id) follow_count , a.id id , a.name name

    from TABLE_A a

    inner join TABLE_B b on TABLE_A.id = TBALE_B.aId and TABLE_A.status = 1

    group by aId 

    order by follow_count desc

    limit 100

  </select>

  暫時這麼解決了問題。  

2,文件下載的實現

  在nginx配置文件中添加了下載路徑

  server{

    listen 80;

    server_name test.abcd.com;

    location ~ ^/csv # 下載csv文件的路徑

    {

      root  /opt/tomcat-abcd ; # 當訪問到 test.abcd.com/csv 的時候將會查找服務器中的 /opt/tomcat-abcd/csv 裏面的文件.

    }

  }

  Controller.java 文件

  @RequestMapping(value="/downloadCSV")

  public String downloadCSV(Model model){

    List<AandCount>  list = service.queryAandCount();

    String fileName = "./csv/date.csv";

    createCSV(list,fileName);

    // 跳轉到下載連接

    model.addAttribute("redirectUrl",fileName.replaceFirst(".",""));

    return "forward:/home/operationRedirect";

  }

3,csv的生成 createCSV(list,fileName);

  public void createCSV(List<AandCount> list,String fileName){

    XSSFWorkbook workbook = new XSSFWordbook();

    XSSFWordbook sheet = workbook.createSheet("sheetName");

    if(CollectionUtils.isNotEmpay(list)){

      int index = 0;

      foreach(AandCount a : list){

        XSSFRow row = sheet.createRow(i);

        XSSFCell cell = row.createCell(0,Cell.CELL_TYPE_STRING);

        cell.setCellValue(a.getaId());

        ...

      }

    }

    // 將csv寫入服務器指定路徑

    FileOutputStream fos = null;

    try{

      fos = new FileOutputStream(fileName);

      workbook.write(fos);

    }catch(IOExecption e){  

      ...

    }finally{

      if(fos != null){

        try{

          fos.close();

        }catch(IOExecption e){

          ...

        }

      }

    }

  }

相關文章
相關標籤/搜索