Ps:一般不会用Maven来创建SpringBoot项目,会选择SpringBoot Initializr构建SpringBoot应用。
Ps:@Entiy 针对于data-jpa使用。
Ps:如果使用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: prod #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: 3313@#! 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() { } }