首先,你好。我有员工、地点、部门和工作类别。每个类都有自己的 JPA Repository 接口。我有一个服务类,我想在这个类中一一创建对象,用控制器调用这个方法并写入数据库。但我收到以下错误。我该如何修复这个错误?我哪里做错了,如果您能帮忙,我将不胜感激。
项目结构在这里
服务
@Service
public class MainService {
@Autowired
LocationsRepository locationsRepository;
@Autowired
JobsRepository jobsRepository;
@Autowired
EmployeesRepository employeesRepository;
@Autowired
DepartmentsRepository departmentsRepository;
public String generateManual() {
try {
Locations locations = new Locations();
locations.setCountry("Turkey");
locations.setCity("Istanbul");
locationsRepository.save(locations);
Jobs jobs = new Jobs();
jobs.setSalary(4000);
jobs.setTitle("Software Developer");
jobsRepository.save(jobs);
Employees employees = new Employees();
employees.setFirstName("Mutlu");
employees.setLastName("Eren");
employees.setJobs(jobs);
employeesRepository.save(employees);
Departments departments = new Departments();
departments.setName("IT");
departments.setLocations(locations);
List<Employees> empList = new ArrayList<>();
empList.add(employees);
departments.setEmployees(empList);
departmentsRepository.save(departments);
return "SUCCESS.";
}catch(Exception e) {
e.printStackTrace();
return "FAILED.";
}
}
}
控制器
@RestController
public class MainController {
@Autowired
MainService mainService;
@GetMapping("/generate")
public void generate() {
mainService.generateManual();
}
}
资源
spring.datasource.url=jdbc:mysql://localhost:3306/companydb?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=Turkey
spring.datasource.username=root
spring.datasource.password=
尚方宝剑之说
富国沪深
相关分类