package com.itheima.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.entity.Book;
import com.itheima.entity.JsonEntity;
import com.itheima.entity.SelectParams;
import com.itheima.mapper.BookMapper;
import com.itheima.test.JdbcConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class HelloController {
private final ObjectMapper objectMapper;
private final BookMapper bookMapper;
private final DataSource dataSource;
private final JdbcConfig jdbcConfig;
private final Book book;
private final SelectParams selectParams;
@GetMapping("hello")
public List<Book> hello() {
//将从数据库中查询的数据做成一个文件
selectParams.setAuthorStr("佚名");
selectParams.setGetCreateDateFrom("20210312");
//List<Book> bookList = bookMapper.selectAllBook();
List<Book> bookList = bookMapper.selectBookByParam(selectParams, 1200);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("E:\\temp.txt");
/** ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
for (Book book:
bookList) {
objectOutputStream.writeObject(book);
}*/
for (Book book :
bookList) {
fileOutputStream.write(String.valueOf(book.getId()).getBytes());
System.out.println(String.valueOf(book.getId()).getBytes());
fileOutputStream.write("\t".getBytes());
fileOutputStream.write(book.getBookName().getBytes());
fileOutputStream.write("\t".getBytes());
fileOutputStream.write(book.getAuthor().getBytes());
fileOutputStream.write("\r\n".getBytes());
}
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//从作成的文件中读取数据到实体pojo类中
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("E:\\temp.txt");
int len = 0;
/** while ((len=fileInputStream.read())!=-1){
System.out.println(len);
}*/
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
//System.out.println(new String(bytes,0,len));
String pojoTemp = new String(bytes, 0, len);
String[] split = pojoTemp.split("/t");
/** System.out.println("-----------以换行符分割----------");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}*/
System.out.println(split[0]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
List<Book> bookListJson = new ArrayList<>();
return bookList;
}
@GetMapping("jsonConvert")
public void convertJsonToEntity() {
StringBuilder paramStringBuilder = new StringBuilder();
// 获取request参数
String requestJson = "param01=12,param02=13,param03=14,paramList=[12,13,14],param04=15,param05=16,param06=17";
// “=”置换为“:”
String replaceRequestJson = requestJson.replace("=", ":");
paramStringBuilder.append(replaceRequestJson);
// list取出并删除
int paramListIndexFrom = paramStringBuilder.indexOf("paramList");
int paramListIndexTo = paramStringBuilder.indexOf("]");
String[] paramListStr = paramStringBuilder.substring(paramListIndexFrom, paramListIndexTo+1).split(":");
String paramListKey = paramListStr[0];
String paramListValue = paramListStr[1];
StringBuilder stringBuilderConvert = new StringBuilder();
stringBuilderConvert.append("{");
stringBuilderConvert.append("\"" + paramListKey + "\"" + ":" + paramListValue);
stringBuilderConvert.append(",");
paramStringBuilder.delete(paramListIndexFrom, paramListIndexTo + 2);
String[] split = paramStringBuilder.toString().split(",");
int a = split.length;
int add = 0;
for (String item :
split) {
String[] splitTemp = item.split(":");
String key = splitTemp[0];
String value = splitTemp[1];
stringBuilderConvert.append("\"" + key + "\"" + ":" + "\"" + value + "\"");
add++;
if (add != split.length)
{
stringBuilderConvert.append(",");
}
}
stringBuilderConvert.append("}");
/*
String json = "{\n" +
" \"param01\": \"1\",\n" +
" \"param02\": \"www.runoob.com\",\n" +
" \"param03\": \"1\",\n" +
" \"param04\": \"菜鸟教程\",\n" +
" \"param05\": \"www.runoob.com\",\n" +
" \"param06\": \"1\",\n" +
" \"paramList\": [\n" +
" 1,\n" +
" 2,\n" +
" 3,\n" +
" 4\n" +
" ]\n" +
"}";*/
System.out.println(stringBuilderConvert.toString());
try {
JsonEntity jsonEntity = objectMapper.readValue(stringBuilderConvert.toString(), JsonEntity.class);
System.out.println(jsonEntity);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
package com.itheima.entity;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@RequiredArgsConstructor
@Builder
@Data
public class JsonEntity {
@NotNull
private String param01;
@NotNull
private String param02;
@NotNull
private String param03;
@NotNull
private String param04;
@NotNull
private String param05;
@NotNull
private String param06;
@NotNull
private List<String> paramList;
}