1. 程式人生 > >SpringBoot - 基礎篇

SpringBoot - 基礎篇

Ps:一般不會用Maven來建立SpringBoot專案,會選擇SpringBoot Initializr構建SpringBoot應用。

Ps:@Entiy 針對於data-jpa使用。

Ps1:用埠80的好處是可以省略該埠號進行訪問。

Ps2:如果使用debug=true,則SpringBoot自動覆蓋logging.level.root=debug。

Ps:YAML的出現避免了將所有的配置沒有集中性的在一起,導致需要修改某配置的時候,需要一條一條肉眼搜尋。

Ps:Jar包可自動載入同目錄的application配置檔案:意思是如果出現修改一些配置檔案資訊,總不能將package好的jar包解壓取出配置檔案--修改--再package,太麻煩,直接複製出需要的配置檔案資訊--修改好後--貼上到與jar包同目錄下的地方即可,重啟啟動Jar包會優先使用同目錄下的配置檔案覆蓋Jar包裡的同名的配置檔案。

 

<?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.imooc</groupId>
	<artifactId>myspringboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>myspringboot</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.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>
	</dependencies>

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

</project>
// application.yml

spring:
 profiles:
  active: prd
  
#debug: true
##logging.level.root
##logging.file
#logging:
# level:
#  root: info
# file: e:/myspringboot.log
#
#spring:
#  datasource:
#    driver-class-name: com.mysql.jdbc.Driver
#    url: jdbc:mysql://localhost:3306/test
#    username: root
#    password: 123456
#mall:
# config:
#  name: 愛美商城
#  description: 這是一家化妝品特賣網站
#  hot-sales: 20
#  show-advert: true
// application-dev.yml

debug: true

#logging.level.root
#logging.file
logging:
 level:
  root: info
 file: e:/myspringboot.log

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

mall:
 config:
  name: 愛美商城
  description: 這是一家化妝品特賣網站
  hot-sales: 20
  show-advert: true
// application-prd.yml

debug: false

#logging.level.root
#logging.file
logging:
 level:
  root: info
 file: /local/user/app-prd.log

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://155.32.55.88:3307/prd
    username: root1
    password: [email protected]#!

mall:
 config:
  name: 優美商城
  description: 這是一家化妝品特賣網站
  hot-sales: 20
  show-advert: true

server:
  port: 80
package com.imooc.myspringboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @Value("${mall.config.name}")
    private String name;

    @Value("${mall.config.description}")
    private String description;

    @Value("${mall.config.hot-sales}")
    private Integer hotSales;

    @Value("${mall.config.show-advert}")
    private Boolean showAdvert;

    @RequestMapping("/out")
    @ResponseBody
    public String out(){
        return "success";
    }

    @RequestMapping("/info")
    @ResponseBody
    public String info(){
        return String.format("name:%s,description:%s,hot-sales:%s,show-advert:%s",
                name,description,hotSales,showAdvert);
    }
}
package com.imooc.myspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyspringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyspringbootApplication.class, args);
    }
}
package com.imooc.myspringboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyspringbootApplicationTests {

	@Test
	public void contextLoads() {
	}

}