无法插入数据库

时间:2019-12-23 11:54:26

标签: hibernate spring-boot jpa

在以下应用程序中,我无法在数据库中插入数据,您能找出想要插入一些数据时在此处实际将数据插入null的问题吗?

Handler class

@RestController
public class ISAController {

  @Autowired
  private ISAService isaService;

  @PostMapping("/createISA")
  public ISAModel createISA(ISAModel isaModel) {
      return isaService.createISA(isaModel);
  }
}

//repository interface

public interface ISARepository extends CrudRepository<ISAModel, Long>{

}

//service class

public interface ISAService {


    public ISAModel createISA(ISAModel isaModel);
    ISAModel updateISA(ISAModel isaModel);
    ISAModel deleteISA(ISAModel isaModel);
}

public interface ISAService {

    Optional<ISAModel> getAllISAInfoById(Long id);
    public ISAModel createISA(ISAModel isaModel);
    ISAModel updateISA(ISAModel isaModel);
    ISAModel deleteISA(ISAModel isaModel);
}

@Service
public class ISAServiceImpl implements ISAService{

    @Autowired
    private ISARepository iSARepository;

    @Override
    public Optional<ISAModel> getAllISAInfoById(Long id) {

        return iSARepository.findById(id);
    }
    @Override
    public ISAModel createISA(@RequestBody ISAModel isaModel) {
        return iSARepository.save(isaModel);
    }

    @Override
    public ISAModel updateISA(ISAModel isaModel) {

        return null;
    }

    @Override
    public ISAModel deleteISA(ISAModel isaModel) {

        return null;
    }
}
//model class
@Entity
class ISAModel{

 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String salary;
}

//properties file
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/world?useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

//main class like normal spring boot class with @EnableJpaAuditing annotation
上面的应用程序中的

数据不会进入数据库并显示存储在其中的空数据,除了id和在此处插入的id(例如1然后2),依此类推... 我现在已经编辑了上面的代码。

1 个答案:

答案 0 :(得分:0)

@RequestBody注释必须在RestController上,而不在服务上:

 @PostMapping("/createISA")
 public ISAModel createISA(@RequestBody ISAModel isaModel) {