like so
package com.example.accessingdatajpa;
import org.aspectj.weaver.ast.And;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.Optional;
@SpringBootApplication
public class AccessingDataJpaApplication {
private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}
@Bean
public CommandLineRunner demo(MedicineRepository repository){
return (args)->{
//BusinessLogic
repository.save(new Medicine(1,"Pandol","Paracetamo , Maize starch","Al-Hikma","Safe for children",
"Safe for pregenent"," 250g for Driving" ));
repository.save(new Medicine(2,"Aspirin","Paracetamo , Maize starch","Al-Hikma"," Not Safe for children",
"Safe for pregenent"," 250g for Driving" ));
//Fetch all Medicine
log.info("Medicine found with findAll():");
log.info("-------------------------------");
for (Medicine medicine : repository.findAll()) {
log.info(medicine.toString());
}
log.info("");
`like so fetch an individual customer by ID `
Optional<Medicine> medicine = repository.findById(1L);
log.info("Medicine found with findById(1L):");
log.info("--------------------------------");
log.info(medicine.toString());
log.info("");
// fetch customers by last name
log.info("Medicine found with findByname('Aspirin'):");
log.info("--------------------------------------------");
repository.findByMedName("Aspirin").forEach(aspirin -> {
log.info(aspirin.toString());
});
log.info("");
};
}
我为medicine类编写了代码,没有错误,但是在运行代码时,它运行了,但是结果显示在customer表中(我删除了main上customer的代码,并将customer表作为注解,但是结果仍然像屏幕上显示的那样)
暂无答案!
目前还没有任何答案,快来回答吧!