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动态代理





PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">



















Service层



public interface ServiceDao {
            public List selectYg(String name);
            public List 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 selectYg(String name) {
        // TODO Auto-generated method stub
        return bmYgMapper.selectYg(name);
    }

    @Override
    public List 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 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 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+"/";
%>



 
   
    
    My JSP 'ok.jsp' starting page
    
    
    
        
    
    
    

 
 
 
 
 
   


    
   
   
   

   
    
   
姓名
选择 序号 姓名 职位 薪资 部门
${list.id } ${list.name } ${list.position } ${list.salary } ${list.department.dname }

 



你可能感兴趣的:(ssm整合导入导出)