Internship: following the previous code optimization and understanding of some involved contents

ahyo 2022-06-24 07:54:31 阅读数:523

internshipfollowingpreviouscodeoptimization

swagger( API frame ) The involvement of :
Use custom annotations in your code to generate interface documents , This is very important in projects where the front and back ends are separated . The advantage of this is When developing interfaces, you can use swagger Define the interface document , At the same time, it is also convenient for future maintenance

From this, I also really get to know the real work of the back end , Provide the interface , From this, I understand that word The meaning of the document —— No, swagger Before , We can use word,excel And other functions to write interface definition documents . It is very necessary to write documents in actual projects , Improve Java The service side and Web Docking efficiency of front end and mobile end .

Following the last code Optimization carried out For example, add annotations on methods And its determination data type is assigned as null Or for 0 wait .
EntityProperty.java:

package com.boot.demo.service;
import java.util.List;
/** * Entity properties */
public class EntityProperty {

private String packageName;
private String className;
private List<FieldProperty> filedPropertyList;// Field attribute set 
public EntityProperty() {

}
public EntityProperty(String packageName, String className) {

this.packageName = packageName;
this.className = className;
}
public EntityProperty(String packageName, String className, List<FieldProperty> filedPropertyList) {

this.packageName = packageName;
this.className = className;
this.filedPropertyList = filedPropertyList;
}
public String getPackageName() {

return packageName;
}
public void setPackageName(String packageName) {

this.packageName = packageName;
}
public String getClassName() {

return className;
}
public void setClassName(String className) {

this.className = className;
}
public List<FieldProperty> getFiledPropertyList() {

return filedPropertyList;
}
public void setFiledPropertyList(List<FieldProperty> filedPropertyList) {

this.filedPropertyList = filedPropertyList;
}
@Override
public String toString() {

return "EntityProperty{" +
"packageName='" + packageName + '\'' +
", className='" + className + '\'' +
", filedPropertyList=" + filedPropertyList +
'}';
}
}

FieldProperty.java:

package com.boot.demo.service;
/** * Field properties */
public class FieldProperty {

/** * Field name settings */
private String name;
/** * Field type settings */
private String type;
/** * Notes in Chinese */
private String annotationName;
public FieldProperty()
{

}
public FieldProperty(String name, String type) {

this.name = name;
this.type = type;
}
public FieldProperty(String name, String type,String annotationName) {

this.name = name;
this.type = type;
this.annotationName=annotationName;
}
public String getName() {

return name;
}
public void setName(String name) {

this.name = name;
}
public String getType() {

return type;
}
public void setType(String type) {

this.type = type;
}
public String getannotationName() {

return annotationName;
}
public void setannotationName(String annotationName) {

this.annotationName = annotationName;
}
@Override
public String toString() {

return "FieldInfo{" +
"name='" + name + '\'' +
", type=" + type +
", annotationName='" + annotationName + '\'' +
'}';
}
}

ParseConfig.java:

package com.boot.demo.service;
/** * Parsing configuration */
public class ParseConfig {

/** * Whether to use Lombok */
private boolean useLombok=true;
/** * Whether to implement Serializable serialize */
private boolean serializable=true;
/** * Whether to display header comments */
private boolean showHeader = true;
/** * Header comment content */
private String header = " Budget adjustment report data ";
// Budget adjustment analysis 
public ParseConfig() {

}
public boolean isUseLombok() {

return useLombok;
}
public boolean isSerializable() {

return serializable;
}
public boolean isShowHeader() {

return showHeader;
}
public String getHeader() {

return header;
}
}

WordPOI.java:

package com.boot.demo.service;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/ * Resolution configuration supports customization , Specific in ParseConfig.java Related configuration */
public class WordPOI {

public WordPOI(){

}
/** * Generate entity objects * @param entityProperty Entity properties * @param isDoc true Said is ".doc" File format ,false Said is “.docx” File format * @param outPath The output path * @param parseConfig Parsing configuration * @throws IOException */
public void generateEntity(EntityProperty entityProperty, boolean isDoc, String outPath,ParseConfig parseConfig) throws IOException{

String className = entityProperty.getClassName();
String javaName = className + ".java";
StringBuffer buffer = new StringBuffer();
buffer.append("package "+ entityProperty.getPackageName()+";\n\n");
// Whether to use Lobmok
boolean useLombok = parseConfig.isUseLombok();
// Whether to import annotations 
boolean importAnnotation= true;
// Whether to import BigDecimal
boolean importBigDecimal = true;
List<FieldProperty> filedPropertyList = entityProperty.getFiledPropertyList();
if(filedPropertyList !=null ){

StringBuffer bufferField = new StringBuffer();
int index = 0;
int size = filedPropertyList.size();
for(FieldProperty filedProperty : filedPropertyList){
// Traversal data type 
bufferField.append("\[email protected]" + filedProperty.getannotationName() +"(value = \" \")"+ "\n");
if (filedProperty.getName().equals("year")||filedProperty.getName().equals("time"))
bufferField.append("\tprivate " + filedProperty.getType() + " " + filedProperty.getName() +"= LocalDate.now().getYear()"+";\n\n");
else if (filedProperty.getType().equals("BigDecimal")&&filedProperty.getName().contains("Ratio")) {

filedProperty.setType("double");
bufferField.append("\tprivate " + filedProperty.getType() + " " + filedProperty.getName() + "= 0" + ";\n\n");
}
else if(filedProperty.getType().equals("BigDecimal"))
bufferField.append("\tprivate " + filedProperty.getType() + " " + filedProperty.getName() +"= new BigDecimal(\"0\")"+";\n\n");
else
bufferField.append("\tprivate " + filedProperty.getType() + " " + filedProperty.getName() +"= null"+";\n\n");
String fieldType = filedProperty.getType();
if(fieldType!=null)
{

// Traverse 
if(fieldType.startsWith("BigDecimal")){

importBigDecimal = true;
}
}
index++;
}
if(parseConfig.isSerializable()){
// Import Serializable
buffer.append("import java.io.Serializable;\n");
}
if(importAnnotation){

buffer.append("import io.swagger.annotations.ApiModelProperty;\n");
buffer.append("import lombok.experimental.Accessors;\n");
buffer.append("import java.time.LocalDate;\n");
}
if(importBigDecimal){

buffer.append("import java.math.BigDecimal;\n");
}
if(useLombok){
// Import Lombok
buffer.append("import lombok.Data;\n");
}
buffer.append("\n");
// Whether to display the header note description 
if(parseConfig.isShowHeader()){

String header = parseConfig.getHeader();
if(header!=null && header.length()>0){

buffer.append("/**\n");
buffer.append(" * ")
.append(header.replace("\n","\n * "));
buffer.append("\n */\n");
}
}
if(useLombok){
// add to Lombok Of data annotation 
buffer.append("@Data\n");
buffer.append("@Accessors(chain = true)\n");
}
// Splicing class 
buffer.append("public class " + className);
if(genericity){

buffer.append("<T>");
}
// serialize 
if(parseConfig.isSerializable()){

buffer.append(" implements Serializable");
}
buffer.append(" {\n\n");
// Add fields 
buffer.append(bufferField).append("\n");
}
}
buffer.append("\n}\n\n");
// Do output work 
File dirFile = new File(outPath);
if(dirFile!=null && !dirFile.exists()){

dirFile.mkdirs();
}
// Write content to file 
File file = new File(outPath,javaName);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),parseConfig.getCharsetName()));
//doc After parsing the document in format, there will be \u0007 Characters should be uniformly filtered 
writer.write(isDoc ? buffer.toString().replace("\u0007","") : buffer.toString());
writer.flush();
writer.close();
}
}

WordPoiImpl.java:

package com.boot.demo.output;
import com.boot.demo.service.EntityProperty;
import com.boot.demo.service.FieldProperty;
import com.boot.demo.service.ParseConfig;
import com.boot.demo.service.WordPOI;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/* For the two word file Write specific code to achieve Create the ystz_dept.docx file A duplicate field appears But creating ystz.docx There will be no */
public class WordPoiImpl {

public static void main(String[] args) throws IOException {

WordPOI wordPOI=new WordPOI();
String BigDecimal="BigDecimal";
List<String> list = new ArrayList<>();
List<FieldProperty> fieldProperties=new ArrayList<>();
ParseConfig parseConfig=new ParseConfig();
try {

// OPCPackage opcPackage = POIXMLDocument.openPackage("C:\\Users\\Redic\\IdeaProjects\\demo\\src\\main\\resources\\word\\ystz.docx");
OPCPackage opcPackage = POIXMLDocument.openPackage("C:\\Users\\Redic\\IdeaProjects\\demo\\src\\main\\resources\\word\\ystz_dept.docx");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
String text2007 = extractor.getText();
String str1=text2007.replaceAll("\\{\\{","}}");
String subSentences[] = str1.split("\\}\\}");
String allStr="";
for (String sub : subSentences) {

sub=sub.replaceAll("[^a-z^A-Z]", "");
allStr+=sub+" ";
}
String subNew[]=allStr.split(" ");
for (String no:subNew) {

if(!no.isEmpty())
list.add(no);
}
for (int i = 0; i < list.size(); i++) {

for (int j = i+1; j <list.size() ; j++) {

if (list.get(i).equals(list.get(j)))
list.remove(j);
}
}
// Iterator it = list.iterator(); Test if Remove 
// while (it.hasNext()) {

// System.out.println(it.next());
// }
} catch (Exception e) {

e.printStackTrace();
}
for (int i = 0; i < list.size(); i++)
{

if(list.get(i).equals("year")||list.get(i).equals("time")) {

FieldProperty fieldProperty1 = new FieldProperty(list.get(i), "int", "ApiModelProperty");
fieldProperties.add(fieldProperty1);
}
else if(list.get(i).equals("quarter")||list.get(i).equals("dept"))
fieldProperties.add(new FieldProperty(list.get(1),"String","ApiModelProperty"));
else
fieldProperties.add(new FieldProperty(list.get(i),BigDecimal,"ApiModelProperty"));
}
// EntityProperty entityProperty=new EntityProperty("com.boot.demo.pojo","BudgetAdjustDeptReportModel",fieldProperties);
EntityProperty entityProperty=new EntityProperty("com.boot.demo.pojo","BudgetAdjustDeptAnalyseModel",fieldProperties);
wordPOI.generateEntity(entityProperty,false,"C:\\Users\\Redic\\IdeaProjects\\demo\\src\\main\\java\\com\\boot\\demo\\pojo",parseConfig);
}
}
copyright:author[ahyo],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240420596044.html