有一个红色下划线,下划线为消息:不兼容类型。必需:...在jpa
。findbyid
方法上找到了model.constants:java.util.optional
Constants constants=constantsRepository.findById(1L);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pkid")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
package com.destek.salaryCalculation.repository;
import com.destek.salaryCalculation.model.Constants;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ConstantsRepository extends JpaRepository<Constants, Long>{
}
package com.destek.salaryCalculation.controller;
import com.destek.salaryCalculation.helper.BesCreateExcelHelper;
import com.destek.salaryCalculation.model.Personal;
import com.destek.salaryCalculation.repository.PersonalRepository;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@RestController
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping("/salarycalc")
public class BesCreateController {
@Autowired
private PersonalRepository personalRepository;
@GetMapping("/besexcel/{group}")
public void exportToExcelByGroup(HttpServletResponse response,@PathVariable String group) throws IOException, InvalidFormatException {
response.setContentType("application/octet-stream");
String headerKey = "Content-Disposition";
String headerValue = "attachment; filename=bes.xlsx";
response.setHeader(headerKey, headerValue);
List<Personal> listPersonal = personalRepository.getListPersonelByGroup(group);
System.out.println(listPersonal);
BesCreateExcelHelper excelExporter = new
BesCreateExcelHelper(listPersonal);
excelExporter.export(response);
}
}
和BesCreateHelper java
package com.destek.salaryCalculation.helper;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.destek.salaryCalculation.model.Constants;
import com.destek.salaryCalculation.model.Personal;
import com.destek.salaryCalculation.repository.ConstantsRepository;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
public class BesCreateExcelHelper {
private XSSFWorkbook workbook;
private XSSFSheet sheet;
private List<Personal> listPersonalBes;
@Autowired
private ConstantsRepository constantsRepository;
public BesCreateExcelHelper(List<Personal> listPersonalBes) throws IOException, InvalidFormatException {
FileInputStream inputStream = new FileInputStream(new File("bes.xlsx"));
this.listPersonalBes = listPersonalBes;
//workbook = new XSSFWorkbook();
workbook = (XSSFWorkbook) WorkbookFactory.create(inputStream);
sheet = workbook.getSheetAt(0);
inputStream.close();
}
private void createCell(Row row, int columnCount, Object value, CellStyle style) {
//sheet.autoSizeColumn(columnCount);
Cell cell = row.createCell(columnCount);
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
}else {
cell.setCellValue((String) value);
}
cell.setCellStyle(style);
}
private void writeDataLines() {
int rowCount = sheet.getLastRowNum();
CellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setFontHeight(15);
style.setFont(font);
Optional<Constants> constants2 = constantsRepository.findById(1L);
Constants constants = constants2.orElseThrow(() ->new RuntimeException("No such data found"));;
for (Personal personal : listPersonalBes) {
Row row = sheet.createRow(++rowCount);
int columnCount = 0;
int i = 1;
createCell(row, columnCount++, i++, style);
createCell(row, columnCount++, personal.getName(), style);
createCell(row, columnCount++, personal.getIdentity().toString(), style);
double besAmount=0;
if(personal.getBes()==1){
int numberDayOfMonth=constants.getDayOfWork();
int sanitaryPermit=personal.getSanitaryPermit();
double grossWages=0;
if(sanitaryPermit<numberDayOfMonth) {
grossWages = constants.getDailyWage() * (numberDayOfMonth - sanitaryPermit);
}
float socialHelp=constants.getSocialHelp();
float mealBonus=constants.getMealBonus();
float tisSupport=constants.getTisSupport();
double deservedVehicleHelp=0;
if(numberDayOfMonth>0){
int dayOfWork=numberDayOfMonth-sanitaryPermit-personal.getAnnualPermit();
// TODO: 11.01.2021 busTicketHelp tekrar hesaplatılacak.
double busTicketHelp=3.25*numberDayOfMonth;
deservedVehicleHelp=busTicketHelp*dayOfWork/numberDayOfMonth;
}
double mealPrice=(constants.getDayOfWork()-(personal.getSanitaryPermit()- Math.floor(personal.getSanitaryPermit()/7)*2)-personal.getAnnualPermit())*constants.getMealBonus();
double extraWorkHourPrice=Math.ceil(personal.getExtraWorkHour()*constants.getDailyWage()/5);
double extraWorkHourPriceSpecial=Math.ceil(personal.getExtraWorkHourSpecial()*constants.getDailyWage()/7.5*1.5);
double mealException=constants.getMinimumWage()/30*6/100*mealPrice;
double sgkPrimBaseAmount=grossWages+socialHelp+mealBonus+tisSupport+deservedVehicleHelp+mealPrice+extraWorkHourPrice+extraWorkHourPriceSpecial+mealException;
besAmount=sgkPrimBaseAmount*0.03;
}
createCell(row, columnCount++, String.valueOf(besAmount), style);
}
}
public void export(HttpServletResponse response) throws IOException {
//writeHeaderLine();
writeDataLines();
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
// workbook.close();
outputStream.close();
}
}
正如@anishb所述,您需要从可选的解析值,但我不会使用
... = constants.get();
因为这是危险的constants.get()
将抛出NosuchelementException
如果值不存在
您应该使用:
Constants value = constants.orElse(null);
Constants value = constants.orElseThrow(() -> new CustomException());
BesCreateExcelHelper将类似于:
@Component
public class BesCreateExcelHelper {
...
public void init(List<Personal> listPersonalBes) { // notice this was the constructor logic
.......
}
}
然后将BesCreateExcelHelper
注入到BesCreateController
中,并将..new BesCreateController()
行替换为BesCreateExcelHelper.init(listPersonal)
#2您可以保持besCreateExcelHelper
原样,但删除常量提取行并从外部传递常量值,当然,这里的外部意味着其他一些Spring组件,就像您在ExportToExcelByGroup
中所做的那样,因此您只需在控制器中执行ConstantsRepository.findById(1L);
然后将其作为第二个构造函数参数传递给Helper类
...
constant = ...constantsRepository.findById(1L);
excelExporter = new
BesCreateExcelHelper(listPersonal, constant);
我更喜欢第一选择
我正在学习java,我写了这个程序,它在我的大学电脑上编译成功,但在我的家庭电脑上没有编译。有谁能帮助我吗?
我正在使用JPA,列类型为: 我使用PostgreSql作为我的数据库,它支持bpchar类型,现在我尝试为我的测试添加H2,但问题是,当我运行测试时,我得到以下结果: 是否有其他方法可以实现这一目标?
我定义jackoson序列化器并将其添加到java类中,如下所示: 编译器出现以下错误: 注释的定义为: 如果我从ReportFilterDeserializer中删除泛型attibute,它将通过编译。我不明白编辑为什么抱怨。
我按照这个教程:https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer 现在我在这一点上: 我的问题是这条线... 显示一个错误:不兼容的类型。需要android。应用程序。FragmentManager发现:android。支持v4.app。碎片管理器。 我看到了一些帖子,但它们不适合我。 我用AppC
我正在尝试使用reverfit和RxJava在我正在使用的应用程序中的自定义视图中进行API调用,但是当我尝试订阅来自我的reverfit API调用的Observable时,遇到了一个不兼容类型错误。 我的改装界面: } 和位于自定义视图的onFinishInflate()中的改装调用: 当我构建项目时,在自定义视图中以可观察的 query=...开头的行中遇到了不兼容类型错误: 错误:(60,