导入依赖
org.apache.commons
commons-collections4
4.4
工具类
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
public class CsvExportUtil {
private static final String CSV_COLUMN_SEPARATOR = ",";
private static final String CSV_ROW_SEPARATOR = "rn";
public static void doExport(List
在Mybatis查询出List
select auth_name,
book_create_time,
book_name,
book_id,
book_price,
publish_name,
classify_name,
book_count,
book_sell
from book_auth_publish
where book_is_delete = 0;
测试
import com.lixianhe.dao.BookMapper;
import com.lixianhe.utils.CsvExportUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
@RestController
@Slf4j
public class TestController {
@Autowired
private BookMapper bookMapper;
@GetMapping("/c/virtualMachine/export")
public void export(HttpServletResponse response) {
// 返回的数据必须是List