我正在创建一个管理一些客户信息的应用程序。但是,如果知道每次重新启动代码时,数据库中的数据都会完全丢失,代码就可以正常工作。我刚输入了springboot代码。
重启控制器.java
package crudapp.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import crudapp.model.Customer;
import crudapp.model.Message;
import crudapp.service.CustomerServices;
@RestController
@RequestMapping("/api/customer")
public class RestAPIController {
@Autowired
CustomerServices customerServices;
@PostMapping("/create")
public ResponseEntity<Message> addNewCustomer(@RequestBody Customer customer) {
try {
Customer returnedCustomer = customerServices.saveCustomer(customer);
return new ResponseEntity<Message>(new Message("Upload Successfully!",
Arrays.asList(returnedCustomer), ""), HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Fail to post a new Customer!",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/retrieveinfos")
public ResponseEntity<Message> retrieveCustomerInfo() {
try {
List<Customer> customerInfos = customerServices.getCustomerInfos();
return new ResponseEntity<Message>(new Message("Get Customers' Infos!",
customerInfos, ""), HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Fail!",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/findone/{id}")
public ResponseEntity<Message> getCustomerById(@PathVariable long id) {
try {
Optional<Customer> optCustomer = customerServices.getCustomerById(id);
if(optCustomer.isPresent()) {
return new ResponseEntity<Message>(new Message("Successfully! Retrieve a customer by id = " + id,
Arrays.asList(optCustomer.get()), ""), HttpStatus.OK);
} else {
return new ResponseEntity<Message>(new Message("Failure -> NOT Found a customer by id = " + id,
null, ""), HttpStatus.NOT_FOUND);
}
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Failure",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/updatebyid/{id}")
public ResponseEntity<Message> updateCustomerById(@RequestBody Customer _customer,
@PathVariable long id) {
try {
if(customerServices.checkExistedCustomer(id)) {
Customer customer = customerServices.getCustomerById(id).get();
//set new values for customer
customer.setFirstname(_customer.getFirstname());
customer.setLastname(_customer.getLastname());
customer.setAddress(customer.getAddress());
customer.setAge(_customer.getAge());
// save the change to database
customerServices.updateCustomer(customer);
return new ResponseEntity<Message>(new Message("Successfully! Updated a Customer "
+ "with id = " + id,
Arrays.asList(customer), ""), HttpStatus.OK);
}else {
return new ResponseEntity<Message>(new Message("Failer! Can NOT Found a Customer "
+ "with id = " + id,
null, ""), HttpStatus.NOT_FOUND);
}
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Failure",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/deletebyid/{id}")
public ResponseEntity<Message> deleteCustomerById(@PathVariable long id) {
try {
// checking the existed of a Customer with id
if(customerServices.checkExistedCustomer(id)) {
customerServices.deleteCustomerById(id);
return new ResponseEntity<Message> (new Message("Successfully! Delete a Customer with id = " + id,
null, ""), HttpStatus.OK);
}else {
return new ResponseEntity<Message>(new Message("Failer! Can NOT Found a Customer "
+ "with id = " + id, null, ""), HttpStatus.NOT_FOUND);
}
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Failure",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
客户.java
package crudapp.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String firstname;
@Column
private String lastname;
@Column
private String address;
@Column
private int age;
public void setId(long id) {
this.id = id;
}
public long getId() {
return this.id;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return this.firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return this.lastname;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return this.address;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
protected Customer() {}
public Customer(String firstname, String lastname, String address, int age) {
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
this.age = age;
}
public String toString() {
return String.format("id=%d, firstname='%s', lastname'%s', address=%s, age=%d",
id, firstname, lastname, address, age);
}
}
customerrepository.java文件
package crudapp.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import crudapp.model.Customer;
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long>{
}
客户服务.java
package crudapp.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import crudapp.model.Customer;
import crudapp.repository.CustomerRepository;
@Service
public class CustomerServices {
@Autowired CustomerRepository repository;
public Customer saveCustomer(Customer customer) {
return repository.save(customer);
}
public List<Customer> getCustomerInfos(){
return repository.findAll();
}
public Optional<Customer> getCustomerById(long id) {
return repository.findById(id);
}
public boolean checkExistedCustomer(long id) {
if(repository.existsById((long) id)) {
return true;
}
return false;
}
public Customer updateCustomer(Customer customer) {
return repository.save(customer);
}
public void deleteCustomerById(long id) {
repository.deleteById(id);
}
}
应用程序属性
spring.datasource.url=jdbc:mysql://localhost:3306/customerdb
spring.datasource.username=root
spring.datasource.password=12345
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
暂无答案!
目前还没有任何答案,快来回答吧!