Maven과 Spring Boot를 사용한 소규모 서비스

8270 단어
Maven in 5 Minutes
mvn archetype:generate -DgroupId=com.parkingservices.app -DartifactId=parking-services -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false


IntelliJ로 열고 src 폴더 삭제



pom.xml에서 몇 가지를 변경해야 합니다.

<?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.parkingservices.app</groupId>
  <artifactId>parking-services</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>

  <modules>
    <module>vehicle</module>
  </modules>

  <name>parking-services</name>
  <url>https://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <spring.boot.maven.plugin.version>2.5.7</spring.boot.maven.plugin.version>
    <spring.boot.dependencies.version>2.5.7</spring.boot.dependencies.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring.boot.dependencies.version}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>${spring.boot.maven.plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>


이제 새 모듈을 추가해야 합니다.

프로젝트명 우클릭 > New > Module









배너 스프링 부트 만들기
Online Spring Boot Banner Generator

이제 응용 프로그램을 실행



폼 차량




<?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">
    <parent>
        <groupId>com.parkingservices.app</groupId>
        <artifactId>parking-services</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>vehicle</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

</project>


  • VehicleApplication.java

  • @SpringBootApplication
    public class VehicleApplication {
        public static void main(String[] args) {
            SpringApplication.run(VehicleApplication.class);
        }
    }
    


  • 차량.자바

  • @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @Entity
    public class Vehicle {
        @Id
        @SequenceGenerator(name = "vehicle_id_sequence", sequenceName = "vehicle_id_sequence")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "vehicle_id_sequence")
        private Integer id;
        private String plate;
        @Column(name = "parking_entry_date", nullable = false)
        private LocalDateTime vehicleEntryDate;
    
        @PrePersist
        public void prePersist() {
            vehicleEntryDate = LocalDateTime.now();
        }
    }
    


  • VehicleController.java

  • @Slf4j
    @RestController
    @RequestMapping("api/v1/vehicles")
    public record VehicleController(VehicleService vehicleService) {
        @PostMapping
        public void registerVehicle(@RequestBody VehicleRegistrationRequest vehicleRegistrationRequest) {
            log.info("New register vehicle {}", vehicleRegistrationRequest);
            vehicleService.registerVehicle(vehicleRegistrationRequest);
        }
    }
    


  • 차량 요청.자바

  • public record VehicleRegistrationRequest(String plate) {
    }
    


  • 차량 저장소.자바

  • public interface VehicleRepository extends JpaRepository<Vehicle, Integer> {
    }
    


  • VehicleService.java

  • @Service
    public record VehicleService(VehicleRepository vehicleRepository) {
        public void registerVehicle(VehicleRegistrationRequest request) {
            Vehicle vehicle = Vehicle.builder()
                    .plate(request.plate())
                    .build();
            vehicleRepository.save(vehicle);
        }
    }
    

    좋은 웹페이지 즐겨찾기