first pushing the cardatabase
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.packt.cardatabase;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.packt.cardatabase.domain.*;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CardatabaseApplication implements CommandLineRunner {
|
||||
private static final Logger logger = LoggerFactory.getLogger(CardatabaseApplication.class);
|
||||
|
||||
@Autowired
|
||||
private CarRepository crepository;
|
||||
@Autowired
|
||||
private OwnerRepository orepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CardatabaseApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String...args) throws Exception {
|
||||
//application 이 완전히 시작하기 전에 CommandLineRunner 를 이용해 예제 데이타를 준비하기 위해 이곳에 코드를 추가
|
||||
|
||||
Owner owner1 = new Owner("John", "Johnson");
|
||||
Owner owner2 = new Owner("Justin", "Choi");
|
||||
orepository.saveAll(Arrays.asList(owner1, owner2));
|
||||
|
||||
crepository.save(new Car("Ford", "Mustang", "Red", "ADF-1211", 2021, 59000, owner1));
|
||||
crepository.save(new Car("Kia", "Niro", "Navy", "BBB-2222", 2018, 39000, owner2));
|
||||
crepository.save(new Car("Toyota", "Camrey", "White", "CCC-3333", 2022, 35000, owner2));
|
||||
|
||||
for (Car car : crepository.findAll()) {
|
||||
logger.info(car.getBrand()+" "+car.getModel());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
116
cardatabase/src/main/java/com/packt/cardatabase/domain/Car.java
Normal file
116
cardatabase/src/main/java/com/packt/cardatabase/domain/Car.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package com.packt.cardatabase.domain;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
|
||||
@Entity
|
||||
public class Car {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private long id;
|
||||
private String brand, model, color, registerNumber;
|
||||
@Column(name="`year`", nullable=true, length=8)
|
||||
private int year;
|
||||
@Column(nullable=true)
|
||||
private float price;
|
||||
|
||||
@ManyToOne(fetch=FetchType.LAZY)
|
||||
@JoinColumn(name="owner")
|
||||
private Owner owner;
|
||||
|
||||
// @ManyToMany(mappedBy="cars")
|
||||
// private Set<Owner> owners = new HashSet<Owner>();
|
||||
//
|
||||
// public Set<Owner> getOwners(){
|
||||
// return owners;
|
||||
// }
|
||||
// public void setOwners(Set<Owner> owners) {
|
||||
// this.owners = owners;
|
||||
// }
|
||||
|
||||
public Car() {}
|
||||
|
||||
public Car(String brand, String model, String color, String registerNumber, int year, float price, Owner owner) {
|
||||
|
||||
super();
|
||||
this.brand = brand;
|
||||
this.model = model;
|
||||
this.color = color;
|
||||
this.year = year ;
|
||||
this.price = price;
|
||||
this.registerNumber = registerNumber;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getRegisterNumber() {
|
||||
return registerNumber;
|
||||
}
|
||||
|
||||
public void setRegisterNumber(String registerNumber) {
|
||||
this.registerNumber = registerNumber;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Owner getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(Owner owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.packt.cardatabase.domain;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.data.repository.*;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||
|
||||
// @RestResource(path="vehicles") -> api 호출경로를 cars 대신 vehicles 로 변경하고자 할때 사용하는 어노테이션
|
||||
@RepositoryRestResource
|
||||
public interface CarRepository extends CrudRepository<Car, Long>, PagingAndSortingRepository<Car, Long>{
|
||||
|
||||
//브랜드로 검색
|
||||
List<Car> findByBrand(@Param("brand") String brand);
|
||||
|
||||
//색상으로 검색
|
||||
List<Car> findByColor(String color);
|
||||
|
||||
List<Car> findByModel(String model);
|
||||
List<Car> findByRegisterNumber(String registerNumber);
|
||||
List<Car> findByYearOrderByModel(int year);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.packt.cardatabase.domain;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.*;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
|
||||
@Entity
|
||||
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
|
||||
public class Owner {
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private long ownerId;
|
||||
private String firstname, lastname;
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
|
||||
private List<Car> cars;
|
||||
|
||||
@ManyToMany(cascade=CascadeType.PERSIST)
|
||||
@JoinTable(name="car_owner",
|
||||
joinColumns = { @JoinColumn(name="ownerid")},
|
||||
inverseJoinColumns = { @JoinColumn(name="`id`")})
|
||||
private List<Car> manycars;
|
||||
|
||||
public Owner() {}
|
||||
|
||||
public Owner(String firstname, String lastname) {
|
||||
super();
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
|
||||
}
|
||||
|
||||
public long getOwnerId() {
|
||||
return ownerId;
|
||||
}
|
||||
|
||||
public void setOwnerId(long ownerId) {
|
||||
this.ownerId = ownerId;
|
||||
}
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public void setFirstname(String firstname) {
|
||||
this.firstname = firstname;
|
||||
}
|
||||
|
||||
public String getLastname() {
|
||||
return lastname;
|
||||
}
|
||||
|
||||
public void setLastname(String lastname) {
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public List<Car> getCars(){
|
||||
return cars;
|
||||
}
|
||||
|
||||
public void setCars(List<Car> cars) {
|
||||
this.cars = cars;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.packt.cardatabase.domain;
|
||||
|
||||
import org.springframework.data.repository.*;
|
||||
|
||||
public interface OwnerRepository extends CrudRepository<Owner, Long>{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.packt.cardatabase.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import com.packt.cardatabase.domain.Car;
|
||||
import com.packt.cardatabase.domain.CarRepository;
|
||||
|
||||
@RestController
|
||||
public class CarController {
|
||||
@Autowired
|
||||
private CarRepository crepository;
|
||||
|
||||
|
||||
@RequestMapping("/cars")
|
||||
public Iterable<Car> getCars(){
|
||||
return crepository.findAll();
|
||||
}
|
||||
}
|
||||
12
cardatabase/src/main/resources/application.properties
Normal file
12
cardatabase/src/main/resources/application.properties
Normal file
@@ -0,0 +1,12 @@
|
||||
logging.level.root = INFO
|
||||
#spring.datasource.url = jdbc:h2:mem:testdb
|
||||
#spring.h2.console.enabled = true
|
||||
#spring.h2.console.path = /h2-console
|
||||
spring.datasource.url = jdbc:mariadb://xigmaz.com:3307/cardb
|
||||
spring.datasource.username = xztest_user
|
||||
spring.datasource.password = 0e1UP7-_dOej8Wez
|
||||
spring.datasource.driver-class-name = org.mariadb.jdbc.Driver
|
||||
spring.jpa.show-sql = true
|
||||
spring.jpa.generate-ddl = true
|
||||
spring.jpa.hibernate.ddl-auto = create-drop
|
||||
spring.data.rest.basePath=/api
|
||||
Reference in New Issue
Block a user