Java后臺-3.新建項目

新建maven的project
image.png

填寫項目名稱和選擇存放位置,然后finish就創(chuàng)建完成了。
image.png
創(chuàng)建包名

image.png

config:存放配置項,如安全配置、錯誤碼配置等。
controller:控制層,其實就是api層,定義外部使用的接口。
dao:數(shù)據(jù)庫交互層,用于獲取和處理MySQL的數(shù)據(jù)。
pojo:實體類層,存放各種各樣的實體類
response:定義返回結(jié)果的結(jié)構(gòu),便于統(tǒng)一使用。
services:有接口與接口對應(yīng)的實現(xiàn)類,其中的impl就是真正實現(xiàn)邏輯的地方。
utils:工具類

為什么需要controller+services+servicesImpl這種結(jié)構(gòu)?
其實主要是為了擴展和修改。其實就是把實現(xiàn)層和接口層分離出來。
如果只是controller+services,具體的邏輯全都在services層中編寫,那萬一需要更換數(shù)據(jù)庫;或者擴展為另一種實現(xiàn)邏輯,但是舊邏輯又暫時不能刪;那就只能一個個service改內(nèi)部邏輯了。
但如果是controller+services+servicesImpl,那就只需要增加一個impl實現(xiàn)類,對接其他數(shù)據(jù)庫也好,新邏輯也罷,都可以簡單完成。

創(chuàng)建啟動類

先配置好pom.xml文件,增加以下配置項:

    <!--spring-boot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>BeautifulBoxSystem</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging><!--增加打包模式,使用jar包打包方式-->

    <!--配置名稱、url、有效年份-->
    <name>beautiful-box-mall</name>
    <url>https://www.beautifulbox.net</url>
    <inceptionYear>2021-Now</inceptionYear>

    <!--增加配置:java版本、編碼等-->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <!--依賴管理-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--添加依賴-->
    <dependencies>
        <!--spring-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--數(shù)據(jù)庫-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</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>
        </dependency>
        <!-- lombok log、自動get、set等-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <!-- RESTful APIs swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- 集成security 密碼加密-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--集成圖靈驗證碼-->
        <dependency>
            <groupId>com.github.whvcse</groupId>
            <artifactId>easy-captcha</artifactId>
            <version>1.6.2</version>
        </dependency>
        <!--mail相關(guān)-->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
        <!--Redis 非關(guān)系型數(shù)據(jù)庫-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--JWT token token生成器-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.6.0</version>
        </dependency>
    </dependencies>

    <!--構(gòu)建配置-->
    <build>
        <finalName>beautiful-box-1.0.0</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>net.beautifulbox.mall.MallApp</mainClass>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
        <repository>
            <id>org.jboss.repository.releases</id>
            <name>JBoss Maven Release Repository</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

創(chuàng)建自己的App啟動類

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

自定義訪問端口,需要創(chuàng)建application.yml
image.png
server:
  port: 2021
spring:
  application:
    name: beautiful_box_system
  datasource:
    url: jdbc:mysql://192.168.1.6:3306/beautiful_box_system?characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    username: 你數(shù)據(jù)庫的用戶名
    password: 數(shù)據(jù)庫密碼
  jpa:
    show-sql: true
  redis:
    host: 192.168.1.6 # Redis服務(wù)器地址
    port: 6379 # Redis服務(wù)器連接端口
    password: 000000 # Redis服務(wù)器連接密碼

大致上的配置算是配玩了,后面需要用到的,再加上。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容