ssm整合导入导出

Beab

public class Department {
        private int id;
        private String dname;


public class Employee {
            private int id;
            private String name;
            private String position;
            private int salary;
            private Department department;



Dao层


public class Employee {
            private int id;
            private String name;
            private String position;
            private int salary;
            private Department department;



Mapper动态代理




<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.han.dao.BmYgMapper">

<select id="selectYg" parameterType="String" resultMap="yg">
    select * from t_employee e,t_department d where e.did=d.id
    <if test="name!=null and name!=''">
    and e.name like '%${name}%'
    </if>
    
</select>
<select id="selectbfYg" parameterType="int" resultMap="yg">
    select * from t_employee e,t_department d where e.did=d.id
    
    <foreach collection="ids" item="id" open="and e.id in (" separator="," close=")">
    #{id}
    </foreach>
    
</select>


<resultMap type="com.han.pojo.Employee" id="yg">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="position" property="position"/>
<result column="salary" property="salary"/>
<association property="department" javaType="com.han.pojo.Department">
<id column="id" property="id"/>
<result column="dname" property="dname"/>
</association>
</resultMap>
</mapper>


Service层



public interface ServiceDao {
            public List<Employee> selectYg(String name);
            public List<Employee> selectbfYg(int[] ids);




import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ServiceDaoimpl implements ServiceDao{

    @Autowired
    BmYgMapper bmYgMapper;
    
    @Override
    public List<Employee> selectYg(String name) {
        // TODO Auto-generated method stub
        return bmYgMapper.selectYg(name);
    }

    @Override
    public List<Employee> selectbfYg(int[] ids) {
        // TODO Auto-generated method stub
        return bmYgMapper.selectbfYg(ids);
    }

}

Controller




package com.han.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.han.pojo.Employee;
import com.han.service.ServiceDao;

@Controller
@RequestMapping("bmyg")
public class BmYgController {
        
    
    @Autowired
    ServiceDao ser;
    
    
    @RequestMapping("selectYg")
    public String selectYg(Model model,String name){
    
        System.out.println(name);
        List<Employee> list = ser.selectYg(name);
        model.addAttribute("list", list);
        return "ok";
        
        
        
        
    }
    
    @RequestMapping("daochu")
    @ResponseBody
    public String daochu(Model model,String[] idss ) throws IOException{
        BufferedWriter bufferedWriter=null;
        //将String类型的数组转成int类型
        int[] ids=new int[idss.length];
        for (int i = 0; i < idss.length; i++) {
            ids[i]=Integer.parseInt(idss[i]);
        }
        List<Employee> list = ser.selectbfYg(ids);
        //创建一个File
        File file=new File("D://employees.txt");
        //判断文件是否存在
        if(!file.exists()){
            //创建文件
            file.mkdir();
            
        }
        //创建一个FileOutputStream(文件输出流) 向指定的文件输出数据
        FileOutputStream outputStream = new FileOutputStream(file);
        //将字节流转换为字符流          如果不指定字符编码格式 默认为   GBK
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8");
        //缓冲字符流
        bufferedWriter = new BufferedWriter(writer);
        for (Employee employee : list) {
            //写入
            bufferedWriter.write(employee.getId()+"#"+employee.getName()+"#"+employee.getPosition()+"#"+employee.getSalary()+"#"+employee.getDepartment().getDname());
            //换行
            bufferedWriter.newLine();
        }
        
        //刷新
        bufferedWriter.flush();
        //关闭
        bufferedWriter.close();
        outputStream.close();
        
        return "ok";
        
        
        
        
    }
}






<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ok.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
  <script type="text/javascript">
  $(function(){
        alert("aaaaa");
              $("#chu").click(function(){
              
                  var a=new Array;
                  $("input:checkbox:checked").each(function(){
                          
                      a.push($(this).val());    
                      
                  
                  
                  
                  });
                      location.href="${pageContext.request.contextPath }/bmyg/daochu?idss="+a;
                      
              
              
              });
              
            });    
              
              
  </script>
  </head>
 
 
  <body>
    <table border="1">
     <tr><td colspan="5"><form action="${pageContext.request.contextPath }/bmyg/selectYg">姓名<input type="text" name="name"><input type="submit" value="查询"></form> <input type="button" value="导出" id="chu"></td> </tr>
    <tr><td>选择</td> <td>序号</td> <td>姓名</td> <td>职位</td> <td>薪资</td> <td>部门</td></tr>
    <c:forEach items="${list }" var="list">
    <tr><td><input type="checkbox" name="xuan" value="${list.id }"></td> <td>${list.id }</td> <td>${list.name }</td> <td>${list.position }</td> <td>${list.salary }</td> <td>${list.department.dname }</td></tr>
    </c:forEach>
    
    </table>
  </body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值