Java 每次我重新启动应用程序时,使用crudepository的Spring引导数据库(apachederby)都会被删除
我刚刚开始使用Spring启动应用程序。我跟在后面 设置: 对于CRUD操作,我使用CRUDepository(Java 每次我重新启动应用程序时,使用crudepository的Spring引导数据库(apachederby)都会被删除,java,spring,maven,spring-mvc,spring-boot,Java,Spring,Maven,Spring Mvc,Spring Boot,我刚刚开始使用Spring启动应用程序。我跟在后面 设置: 对于CRUD操作,我使用CRUDepository(@Repository)接口作为父接口,通过控制器(@RestController)简化API,Apache Derby用作应用程序数据的嵌入式数据库(@Entities)。@RestController和@Repository之间的桥接是使用@Service注释类完成的 问题: 每当我重新启动Spring Boot应用程序时,我就会丢失使用POST@RequestBody存储在嵌入式
@Repository
)接口作为父接口,通过控制器(@RestController
)简化API,Apache Derby用作应用程序数据的嵌入式数据库(@Entities
)。@RestController
和@Repository
之间的桥接是使用@Service
注释类完成的
问题:
每当我重新启动Spring Boot应用程序时,我就会丢失使用POST
@RequestBody
存储在嵌入式数据库中的所有数据。如果我想将一些数据存储到数据库中,我必须再次执行POST
请求。对于我来说,这显然不是一个好的实践,无论是对于发展还是对于生产,我一定是做错了什么
回购协议:
@Repository
public interface DailyRashifalRepository extends
CrudRepository<DailyRashifalEntity, String> {
}
应用程序:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
public class RashifalController {
@Autowired
private RashifalService service;
//Daily Rashifal API
@RequestMapping("rashifal/daily")
public List<DailyRashifalEntity> getDailyRashifalAll() {
return service.getDailyAll(2017, 2, 3);
}
@RequestMapping("rashifal/daily/{id}")
public DailyRashifalEntity getDailyRashifal(@PathVariable String id) {
return service.getDaily(id,2017, 2, 3);
/*DailyRashifalEntity entity = new DailyRashifalEntity("3", "Ramro Din", 2074, 2, 28);
return entity;*/
}
@RequestMapping(method = RequestMethod.POST, value = "rashifal/daily")
public void addRashifalAll(@RequestBody List<DailyRashifalEntity> entityList) {
service.addDailyAll(entityList);
}
@RequestMapping(method = RequestMethod.PUT, value = "rashifal/daily/")
public void updateRashifalDailyAll(@RequestBody List<DailyRashifalEntity> entityList) {
service.updateDailyAll(entityList);
}
@RequestMapping(method = RequestMethod.DELETE, value = "rashifal/daily/delete_all")
public void deleteDailyAll(@RequestBody List<DailyRashifalEntity> entityList) {
service.deleteDailyAll(entityList);
}
@RequestMapping(method = RequestMethod.DELETE, value = "rashifal/daily/delete_all_by_day/{year}/{month}/{day}")
public void deleteDailyAllByDay(@PathVariable("year") int year, @PathVariable("month") int month,
@PathVariable("day") int day) {
service.deleteDailyAllByDay(year, month, day);
}
@RequestMapping(method = RequestMethod.DELETE, value = "rashifal/daily/delete_by_day/{id}/{year}/{month}/{day}")
public void deleteDailyByDay( @PathVariable("id") String id,@PathVariable("year") int year,
@PathVariable("month") int month, @PathVariable("day") int day) {
service.deleteDailyByDay(id,year, month, day);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gurkhatech</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Spring boot api for rashifal REST api</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server.port=8080
spring.thymeleaf.cache=false
服务:
@Service
public class RashifalService {
@Autowired
DailyRashifalRepository dailyRepo;
@Autowired
WeeklyRashifalRepository weeklyRepo;
@Autowired
MonthlyRashifalRepository monthlyRepo;
@Autowired
YearlyRashifalRepository yearlyRepo;
public List<DailyRashifalEntity> getDailyAll(int year, int month, int day) {
List<DailyRashifalEntity> rashiList = new ArrayList<DailyRashifalEntity>();
dailyRepo.findAll().forEach(rashiList::add);
return rashiList;
}
public DailyRashifalEntity getDaily( String id, int year, int month, int day) {
return dailyRepo.findOne(id);
}
public void addDaily(DailyRashifalEntity entity) {
dailyRepo.save(entity);
}
public void addDailyAll(List<DailyRashifalEntity> entityList) {
for(int i=0;i<entityList.size();i++){
dailyRepo.save(entityList.get(i));
}
}
public void updateDaily(DailyRashifalEntity entity) {
dailyRepo.save(entity);
}
.....Other methods
}
我做错了什么?您可以查看所有代码,也可以在中查看代码(请遵循堆栈溢出问题发布) 您需要一个数据库来保存数据,H2和Derby是嵌入式的,需要在启动时将数据编码到db中。要添加一个简单的MySQL数据库,您需要将其添加到pom.xml中:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
在我刚开始工作时,我发现以下参考资料很有用:
许多Youtube视频和代码来自,这就是嵌入式数据库的设计目的。它仅用于测试和演示目的。如果要持久化数据,需要从derby移动到其他数据库。使用springboot,这样做很容易。只需设置一个数据库(postgres或mysql),然后在应用程序属性中添加该连接参数(以及pom中必要的依赖项,并删除derby依赖项)。其余的都会处理好,不需要更改代码中的任何内容。@pvpkiran非常感谢您的富有洞察力的回答,我想问的更多的是您是否有可能提供我一个好的参考?
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Everything after the ? is optional but I have found recommended previously.
spring.datasource.url=jdbc:mysql://serverip:3306/databasename?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
# Username and password
spring.datasource.username=user
spring.datasource.password=password