SpringBoot(一) 新建項目

[toc]php

項目需求分析

前言

今年跳槽後工做比較清閒,並且本人對android開發確實以爲興趣乏乏,恰好SpringBoot目前很火,並且能夠和我android的kotlin母語配合食用,因此,開坑一個SpringBoot學習系列。html

目標

  • 開發一個純粹的KotlinApp項目
  • 開發一個SpringBoot項目(先後端不分離,由於我不會寫html)

項目分析

App

仿着餓了麼來,除開支付模塊(由於我沒有帳號),其餘特效,自定義View之類的,估摸着本身學習,少用或者禁用第三方,儘可能杜絕面向百度編程java

後臺

api模塊

提供api給與app調用,儘可能規範化,模塊化。mysql

管理後臺模塊

使用Bootstrap佈局,寫相似jsp的頁面模塊,雖然不優雅,很差看。android

開擼

模塊解釋

後臺的話,打算分紅3個模塊:git

  • Buyer
  • Seller
  • Service

其中 Buyer 模塊爲app提供接口 Seller 模塊提供ModelAndView,也就是管理後臺 Service模塊提供Service,dao,數據操做github

實際操做

新建SpringBoot項目

若是還不會新建項目,能夠查看如何新建SpringBoot項目web

刪除多餘文件

刪除項目目錄下src目錄,即刪除後,項目目錄爲: spring

乾乾淨淨的初始目錄

新建module

分別新建Buyer,Seller,Service 三個module,新建方法爲: sql

新建module
選中該項

修改根項目的pom.xml文件

修改根項目的pom.xml文件,去除掉springBoot的依賴(這些依賴咱們放在Service Module裏面) 這裏只是我我的的見解,也許不對 其實這裏我對這個項目的認識是:

借一張圖說話

沒錯,就是android狗最熟悉的Eclipse(其實我沒用過,不過這個比較好解釋) platform 也就是咱們一開始新建的那個空殼項目,其實至關於Eelipse的workPlace,也就是咱們的工做目錄 Service 其實就是咱們提供數據的Library,咱們每一個功能模塊都會依賴它,在裏面寫Dao,Service Buyer 賣家模塊,其實就是一個提供入口的功能模塊,依賴了Service模塊,調用裏面的Service Seller 買家模塊,解釋同Buyer

好的,我知道仍是解釋得很爛,那咱們看圖。

image.png

其中根目錄的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>
    <modules>
        <module>Buyer</module>
        <module>Service</module>
        <module>Seller</module>
    </modules>
    <groupId>com.ly</groupId>
    <artifactId>platform</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>platform</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
複製代碼

注意這裏根模塊引用了3個module:

<module>Buyer</module>
        <module>Service</module>
        <module>Seller</module>
複製代碼

其中Service的pom.xml文件比較繁雜,由於主要Springboot依賴的文件,通用第三方都會寫在這裏

<?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>
        <artifactId>platform</artifactId>
        <groupId>com.ly</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>service</artifactId>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <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>
        <!--lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
        <!--微信插件-->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!--spring-boot-configuration:spring boot 配置處理器; -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!--freemarker-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
複製代碼

而Buyer以及Seller的pom.xml高度相似:

Buyer

<?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>
		<artifactId>platform</artifactId>
		<groupId>com.ly</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>

	<artifactId>Buyer</artifactId>

	<dependencies>
		<dependency>
			<groupId>com.ly</groupId>
			<artifactId>service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<type>pom</type>
				<version>1.5.2.RELEASE</version>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<properties>
		<start-class>com.ly.platform.PlatformApplication</start-class>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>1.5.3.RELEASE</version>
				<configuration>
					<mainClass>${start-class}</mainClass>
					<layout>ZIP</layout>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
複製代碼

Seller

<?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>
		<artifactId>platform</artifactId>
		<groupId>com.ly</groupId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>

	<artifactId>Seller</artifactId>

	<dependencies>
		<dependency>
			<groupId>com.ly</groupId>
			<artifactId>service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<type>pom</type>
				<version>1.5.2.RELEASE</version>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<properties>
		<start-class>com.ly.platform.PlatformApplication</start-class>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>1.5.3.RELEASE</version>
				<configuration>
					<mainClass>${start-class}</mainClass>
					<layout>ZIP</layout>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
複製代碼

值得注意的是這邊Buyer以及Service模塊都引入了Service

<dependencies>
		<dependency>
			<groupId>com.ly</groupId>
			<artifactId>service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
複製代碼

每一個模塊本身的獨立配置文件

值得注意的是配置文件,咱們一共有3個配置文件 (properties/yml) 分別位於:Buyer,Seller,Service

其中所負責的功能不一樣:

Service模塊的配置文件
負責通用配置,例如配置常量,配置數據庫,Redis等等

Buyer/Seller模塊的配置文件 負責配置各自的端口,日誌文件目錄等等不須要通用的配置

使用方法

Service

按去配置數據庫鏈接,寫dao,service 不表,咱們寫好了以下文件:

Service文件

其中service提供的方法爲:

/** * Banner */
public interface BannerService {
    List<Banner> findAll();
}
複製代碼
Buyer

配置端口爲8888

配置端口

寫好對應的controller

@RestController
@RequestMapping("api/home")
@Api(value = "首頁模塊", description = "管理Banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("banner")
    @ApiOperation("查詢全部的Banner")
    public List<Banner> getAllBanner() {
        return bannerService.findAll();
    }
}
複製代碼
Seller

配置端口爲8889

server.port=8889
複製代碼

寫對應的controller

@RestController
@RequestMapping("web/home")
@Api(value = "首頁模塊", description = "管理Banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("banner")
    @ApiOperation("查詢全部的Banner")
    public List<Banner> getAllBanner() {
        return bannerService.findAll();
    }
}
複製代碼

Tips 計劃Seller是先後端不分離的,因此這裏應該返回的ModelAndView,不過先寫着測試吧

結果測試

這裏使用postman進行接口測試,接口地址分別爲:

localhost:8888/platform/api/home/banner
複製代碼
localhost:8889/platform/web/home/banner
複製代碼

結果爲:

image.png
image.png

結尾

沒啥好結尾的,加油! 附地址: App App

相關文章
相關標籤/搜索