Java Spring Boot Repository.save()方法不起作用-无提交
我扫描两个二维码,尝试从我的Android手机应用程序中获取二维码,并将其保存在repository.save中,保存在我的本地数据库中 我的应用程序将列表发送到后端,但不插入数据库。当我运行localhost/8090时,我没有得到任何信息。 在浏览器中,仅显示以下内容: -查找设备 -设备代码 -设备ID Developer.java DeveloperRepository.java deviceCodeAndID是来自Android应用程序的类,它使用应用程序扫描Java Spring Boot Repository.save()方法不起作用-无提交,java,android-studio,spring-boot,qr-code,Java,Android Studio,Spring Boot,Qr Code,我扫描两个二维码,尝试从我的Android手机应用程序中获取二维码,并将其保存在repository.save中,保存在我的本地数据库中 我的应用程序将列表发送到后端,但不插入数据库。当我运行localhost/8090时,我没有得到任何信息。 在浏览器中,仅显示以下内容: -查找设备 -设备代码 -设备ID Developer.java DeveloperRepository.java deviceCodeAndID是来自Android应用程序的类,它使用应用程序扫描 index.html
index.html
您是否启用了事务管理。即使您使用SpringBoot数据存储库。您需要启用事务管理,否则默认情况下所有内容都将处于读取模式。对于读取模式,不需要进行事务处理。但是,当您执行任何将更改数据库中数据的操作时,您需要执行事务管理。 在应用程序类中使用@EnableTransactionManagement,在DAO或服务类中使用@Transactional
@SpringBootApplication
public class Application {
@Autowired
DeveloperRepository developerRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
这是我的应用程序类很抱歉设置错误,我写错了。问题不在那里!谢谢你的快速回答,但我没有刀或服务类。我不需要两者,还是我需要它们?我尝试了。尝试在应用程序类上添加以下注释@EnableTransactionManagement@EntityScanentity\u包@EnableJpaRepositoriesRepository\u包
@Controller
public class DevelopersController {
@Autowired
DeveloperRepository repository;
@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody
private String addDevices(Developer deviceCodeAndId) {
System.out.println("xyz!");
if (!repository.exists(deviceCodeAndId.getId())) {
repository.save(deviceCodeAndId);
return "successfully added " + deviceCodeAndId.getId();
}
return deviceCodeAndId.getId();
}
@RequestMapping(value = "/showall",method = RequestMethod.GET)
public String index(Model model) {
model.addAttribute("index",repository.findAll());
return "index";
}
}
index.html
@SpringBootApplication
public class Application {
@Autowired
DeveloperRepository developerRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}