0
下面,我CONFIGS嘗試CrudRepository - H2 - TableNotFound
的pom.xml
`<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>`
控制器
@RestController
public class SendSmsController {
@Autowired
private DataBaseService dataBaseService;
@Autowired
private SendSmsService sendSmsService;
@RequestMapping(value="/sendSms", method=RequestMethod.POST)
public ResponseEntity<Sms> sendSms(@RequestBody Sms sms,@RequestParam(required=false) String expirationDate) {
if(sms.getBody().length() > 160){
return new ResponseEntity<Sms>(HttpStatus.UNPROCESSABLE_ENTITY);
}
dataBaseService.saveSms(sms);
return sendSmsService.sendRequest(sms);
}
}
DataBaseService
@Data
@Service
public class DataBaseService {
@Autowired
private SmsRepository smsRepository;
public void saveSms(Sms sms) {
//I try smsRepository.save(sms) too
smsRepository.save(new Sms(sms.getTo(),sms.getTo(),sms.getBody()));
}
}
SmsRepository
public interface SmsRepository extends CrudRepository<Sms, Long> {
}
短信類
@Data
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Sms {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@JsonProperty
private String from;
@JsonProperty
private String to;
@JsonProperty
private String body;
public Sms(String from, String to, String body){
this.from = from;
this.to = to;
this.body = body;
};
public Sms(){
}
}
配置
@Configuration
public class Config {
@Bean
public SendSmsService sendSmsService(){
return new SendSmsService();
}
@Bean
public DataBaseService dataBaseService(){
return new DataBaseService();
}
當代碼到達 「smsRepository.save(SMS)」 的發生此問題。我按照這個教程https://spring.io/guides/gs/accessing-data-jpa/,但我想我缺少配置。感謝您的任何幫助!
再次錯誤:org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into sms (id, body, from, to) values (null, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
Caused by: org.h2.jdbc.JdbcSQLException: Table "SMS" not found; SQL statement:
insert into sms (id, body, from, to) values (null, ?, ?, ?) [42102-187]
,你可以在你的問題中添加Spring配置XML後? – vels4j
@ vels4j完成!編輯 – user2997534