1. 程式人生 > >Scala學習筆記(9)—— Scala實戰專案(1)- 環境搭建

Scala學習筆記(9)—— Scala實戰專案(1)- 環境搭建

1 專案需求

1.1 資料庫管理(java實現)

default  my-db1  my-db2
id:資料庫編號
name : 資料庫名稱
location : 資料庫存放在 HDFS/S3/OSS 等檔案系統上的目錄
	/user/hive/warehouse
	/user/hive/warehouse/my-db1.db

1.2 表管理(scala語言實現)

my-table1	my-tanle2
id : 表編號
name : 表名稱
tableType : 表型別,內部表/外部表
dbId : 表所屬的資料庫 id
預設儲存路徑 : db 對應的 location / tablename

2 開發環境搭建

  • IDEA + Spring Boot + Data + java
  • IDEA + Spring Boot + Data + Scala
    在這裡插入圖片描述
    在這裡插入圖片描述
    在這裡插入圖片描述
  • 刪除以下的檔案
    在這裡插入圖片描述
    在這裡插入圖片描述
  • 新建一個配置檔案
    在這裡插入圖片描述
    在這裡插入圖片描述
  • 執行主類 SpringBootScalaApplication.java
    訪問 http://localhost:7777/scala-boot/
    在這裡插入圖片描述
  • 新建 controller 包,新建 HelloBoot.java 檔案
    在這裡插入圖片描述
package controller;

import org.springframework.web.bind.annotation.
RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloBoot { @RequestMapping(value = "/sayHello", method = RequestMethod.GET) public String sayHelli() { return "Hello Boot ...."
; } }
  • 瀏覽器訪問 http://localhost:7777/scala-boot/sayHello

2.1 POM 檔案新增依賴

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.18.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.scala.shizhan</groupId>
    <artifactId>spring-boot-scala</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-scala</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <scala.version>2.11.8</scala.version>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--新增 Scala 依賴-->
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <!--新增Data 的依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--新增 Scala 的plugin-->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>compile-scala</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>test-compile-scala</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <recompileMode>incremental</recompileMode>
                    <scalaVersion>${scala.version}</scalaVersion>
                    <args>
                        <arg>-deprecation</arg>
                    </args>
                    <jvmArgs>
                        <jvmArg>-Xms64m</jvmArg>
                        <jvmArg>-Xmx1024m</jvmArg>
                    </jvmArgs>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>


2.2 新建 Scala 目錄

在這裡插入圖片描述

package com.scala.shizhan.controller

import org.springframework.web.bind.annotation.{RequestMapping, RequestMethod, RestController}

@RestController
class ScalaHelloBoot {

    @RequestMapping(value = Array("/sayScalaHello"), method = Array(RequestMethod.GET))
    def sayScalaHello(): String = {
        "Hello Scala Boot ....."
    }
}


  • 瀏覽器訪問 http://localhost:7777/scala-boot/sayScalaHello
    在這裡插入圖片描述

2.3 新建資料庫

mysql> create database scalaboot;
Query OK, 1 row affected (0.00 sec)

mysql> use scalaboot;
Database changed
mysql> show tables;
Empty set

mysql> 
  • 修改配置檔案
    在這裡插入圖片描述