Przeglądaj źródła

0125 springboot配置

Qing 1 rok temu
rodzic
commit
94167fcdab
26 zmienionych plików z 188 dodań i 6 usunięć
  1. 6 0
      springboot-demo/application.properties
  2. 1 0
      springboot-demo/config/application.properties
  3. 20 2
      springboot-demo/src/main/java/com/sf/DemoApplication.java
  4. 19 0
      springboot-demo/src/main/java/com/sf/config/FoodConfig.java
  5. 16 0
      springboot-demo/src/main/java/com/sf/config/VegetablesConfig.java
  6. 1 0
      springboot-demo/src/main/resources/application-dev.properties
  7. 1 0
      springboot-demo/src/main/resources/application-prod.properties
  8. 1 0
      springboot-demo/src/main/resources/application-test.properties
  9. 20 4
      springboot-demo/src/main/resources/application.properties
  10. 24 0
      springboot-demo/src/main/resources/application.yml
  11. 0 0
      springboot-demo/src/main/resources/banner/FromGuoKeYu.txt
  12. 0 0
      springboot-demo/src/main/resources/banner/FromLiHongYuan.txt
  13. 0 0
      springboot-demo/src/main/resources/banner/FromLuBoZhi.txt
  14. 0 0
      springboot-demo/src/main/resources/banner/FromTangZhenLiang.txt
  15. 0 0
      springboot-demo/src/main/resources/banner/FromWangHongMing.txt
  16. 0 0
      springboot-demo/src/main/resources/banner/FromWangHongMing1.txt
  17. 0 0
      springboot-demo/src/main/resources/banner/FromYinXingBo.txt
  18. 0 0
      springboot-demo/src/main/resources/banner/FromZhangJiaXiang.txt
  19. 0 0
      springboot-demo/src/main/resources/banner/FromZhaoYuLong.txt
  20. 0 0
      springboot-demo/src/main/resources/banner/banner.txt
  21. 1 0
      springboot-demo/src/main/resources/config/application.properties
  22. 0 0
      springboot-demo/src/main/resources/mapper/AuthorMapper.xml
  23. 3 0
      springboot-demo/src/main/resources/vegetables.properties
  24. 17 0
      springboot-demo/src/test/java/com/sf/MybatisPlusTests.java
  25. 41 0
      springboot-demo/src/test/java/com/sf/PropertiesTests.java
  26. 17 0
      springboot-demo/src/test/java/com/sf/YamlTests.java

+ 6 - 0
springboot-demo/application.properties

@@ -0,0 +1,6 @@
+#server.port=18084
+
+# \u6709\u56DB\u4E2A\u914D\u7F6E\u6587\u4EF6
+# \u4F18\u5148\u7EA7  \u6839\u8DEF\u5F84/config  resources/config   resources/   \u6839\u8DEF\u5F84/
+#          A\u300118081     B\u300118082           C\u300118083    D\u300118084
+# ADBC

+ 1 - 0
springboot-demo/config/application.properties

@@ -0,0 +1 @@
+#server.port=18081

+ 20 - 2
springboot-demo/src/main/java/com/sf/DemoApplication.java

@@ -1,8 +1,14 @@
 package com.sf;
 package com.sf;
 
 
+import com.sf.config.FoodConfig;
+import com.sf.config.VegetablesConfig;
 import org.mybatis.spring.annotation.MapperScan;
 import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.ComponentScan;
 
 
 // @SpringBootApplication是springboot提供的注解
 // @SpringBootApplication是springboot提供的注解
@@ -11,8 +17,10 @@ import org.springframework.context.annotation.ComponentScan;
 @SpringBootApplication
 @SpringBootApplication
 //@ComponentScan("com.hello")
 //@ComponentScan("com.hello")
 //@ComponentScan("com.sf")
 //@ComponentScan("com.sf")
-@MapperScan("com.sf.mapper")   // 如果不希望每次都在Mapper类上增加@Mapper 可以使用包扫描注解
-public class DemoApplication {
+//@MapperScan("com.sf.mapper")   // 如果不希望每次都在Mapper类上增加@Mapper 可以使用包扫描注解
+@EnableConfigurationProperties({FoodConfig.class})  //打开对配置文件的自定义使用 接收的类是FoodConfig
+//public class DemoApplication implements ApplicationRunner {
+public class DemoApplication implements CommandLineRunner {
 
 
     public static void main(String[] args) {
     public static void main(String[] args) {
         // SpringApplication类执行了run方法  传递了DemoApplication.class和args两个参数
         // SpringApplication类执行了run方法  传递了DemoApplication.class和args两个参数
@@ -24,4 +32,14 @@ public class DemoApplication {
         SpringApplication.run(DemoApplication.class, args);
         SpringApplication.run(DemoApplication.class, args);
     }
     }
 
 
+    @Override
+    public void run(String... args) throws Exception {
+        System.out.println("项目启动啦!!!!!");
+    }
+
+//    @Override
+//    public void run(ApplicationArguments args) throws Exception {
+//        System.out.println("项目启动啦!!!!!");
+//    }
+
 }
 }

+ 19 - 0
springboot-demo/src/main/java/com/sf/config/FoodConfig.java

@@ -0,0 +1,19 @@
+package com.sf.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @ConfigurationProperties 是springboot提供的注解
+ * 声明这是一个配置文件对应的类
+ *
+ * 如果只使用此注解 会报错
+ * Not registered via @EnableConfigurationProperties, marked as Spring component, or scanned via @ConfigurationPropertiesScan
+ */
+@Data
+@ConfigurationProperties(prefix = "food")
+public class FoodConfig {
+    
+    private String meat;
+    private String rice;
+}

+ 16 - 0
springboot-demo/src/main/java/com/sf/config/VegetablesConfig.java

@@ -0,0 +1,16 @@
+package com.sf.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "vegetables")
+@PropertySource("classpath:vegetables.properties")
+public class VegetablesConfig {
+    private String potato;
+    private String eggplant;
+    private String greenpeper;
+}

+ 1 - 0
springboot-demo/src/main/resources/application-dev.properties

@@ -0,0 +1 @@
+server.port=19090

+ 1 - 0
springboot-demo/src/main/resources/application-prod.properties

@@ -0,0 +1 @@
+server.port=11010

+ 1 - 0
springboot-demo/src/main/resources/application-test.properties

@@ -0,0 +1 @@
+server.port=12020

+ 20 - 4
springboot-demo/src/main/resources/application.properties

@@ -1,8 +1,10 @@
 server.port=18080
 server.port=18080
+#server.servlet.context-path=/springboot_demo
 
 
 #spring.thymeleaf.prefix=classpath:/test/
 #spring.thymeleaf.prefix=classpath:/test/
-#spring.thymeleaf.prefix=classpath:/templates
-#spring.thymeleaf.suffix=.html
+spring.thymeleaf.prefix=classpath:/templates/
+spring.thymeleaf.suffix=.html
+spring.thymeleaf.mode=HTML
 #spring.thymeleaf.encoding=utf-8
 #spring.thymeleaf.encoding=utf-8
 #spring.thymeleaf.cache=true
 #spring.thymeleaf.cache=true
 
 
@@ -14,5 +16,19 @@ spring.datasource.password=root123456
 mybatis.config-location=classpath:mybatis-config.xml
 mybatis.config-location=classpath:mybatis-config.xml
 mybatis.mapper-locations=classpath*:mapper/**/*.xml
 mybatis.mapper-locations=classpath*:mapper/**/*.xml
 
 
-spring.banner.location=classpath:bannerZhaoYuLong.txt
-spring.main.banner-mode=off
+#spring.banner.location=classpath:banner/FromTangZhenLiang.txt
+#spring.main.banner-mode=off
+
+#\u591A\u73AF\u5883application  dev  prod test sit\u7070\u5EA6\u6D4B\u8BD5
+
+#\u81EA\u5B9A\u4E49\u914D\u7F6E
+food.meat=\u70E4\u8089
+food.rice=\u62CC\u996D
+# \u7EC3\u4E60\uFF1A\u4E2D\u5348\u5403\u4EC0\u4E48 \u9700\u8981\u4E24\u4E2A\u4EE5\u4E0A\u7684\u53C2\u6570
+# \u6BD4\u5982\uFF1A\u77F3\u9505\u62CC\u996D\u3001\u5C0F\u7092\u83DC\u3001\u714E\u997C\u679C\u5B50...
+
+lines=\
+  11111111111111111111111111111\
+  22222222222222222222222222222\
+  33333333333333333333333333333\
+  44444444444444444444444444444

+ 24 - 0
springboot-demo/src/main/resources/application.yml

@@ -0,0 +1,24 @@
+food:
+    meat: 烤肉
+    rice: 拌饭
+
+mybatis:
+    config-location: classpath:mybatis-config.xml
+    mapper-locations: classpath*:mapper/**/*.xml
+
+spring:
+    datasource:
+        password: root123456
+        url: jdbc:mysql://localhost:3306/novels?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
+        username: root
+---
+spring:
+    banner:
+        location: classpath:banner/FromWangHongMing.txt
+
+#lines: |
+#    我是第一行
+#    我是第二行
+#      我是第三行
+#        我是第四行
+#    我是第五行

+ 0 - 0
springboot-demo/src/main/resources/bannerGuoKeYu.txt → springboot-demo/src/main/resources/banner/FromGuoKeYu.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerLiHongYuan.txt → springboot-demo/src/main/resources/banner/FromLiHongYuan.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerLuBoZhi.txt → springboot-demo/src/main/resources/banner/FromLuBoZhi.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerTangZhenLiang.txt → springboot-demo/src/main/resources/banner/FromTangZhenLiang.txt


+ 0 - 0
springboot-demo/src/main/resources/bannnerWangHongMing.txt → springboot-demo/src/main/resources/banner/FromWangHongMing.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerWangHongMing1.txt → springboot-demo/src/main/resources/banner/FromWangHongMing1.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerYinXingBo.txt → springboot-demo/src/main/resources/banner/FromYinXingBo.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerZhangJiaXiang.txt → springboot-demo/src/main/resources/banner/FromZhangJiaXiang.txt


+ 0 - 0
springboot-demo/src/main/resources/bannerZhaoYuLong.txt → springboot-demo/src/main/resources/banner/FromZhaoYuLong.txt


+ 0 - 0
springboot-demo/src/main/resources/banner.txt → springboot-demo/src/main/resources/banner/banner.txt


+ 1 - 0
springboot-demo/src/main/resources/config/application.properties

@@ -0,0 +1 @@
+#server.port=18082

+ 0 - 0
springboot-demo/src/main/resources/mapper/authorMapper.xml → springboot-demo/src/main/resources/mapper/AuthorMapper.xml


+ 3 - 0
springboot-demo/src/main/resources/vegetables.properties

@@ -0,0 +1,3 @@
+vegetables.eggplant=茄子
+vegetables.greenpeper=青椒
+vegetables.potato=土豆

+ 17 - 0
springboot-demo/src/test/java/com/sf/MybatisPlusTests.java

@@ -1,5 +1,6 @@
 package com.sf;
 package com.sf;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -125,4 +126,20 @@ public class MybatisPlusTests {
         List<Fiction> records = selectPage.getRecords();
         List<Fiction> records = selectPage.getRecords();
         records.forEach(System.out::println);
         records.forEach(System.out::println);
     }
     }
+
+    @Test
+    public void testLambda() {
+//        QueryWrapper chapterWrapper = new QueryWrapper();
+//        chapterWrapper.eq("fiction_id", chapter.getFictionId());
+//        chapterWrapper.eq("sort", pre);
+//        Chapter preChapter = chapterMapper.selectOne(chapterWrapper);
+
+        LambdaQueryWrapper<Chapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+        // fictionId -> fiction_id
+        //  :: 可以理解为  谁的什么方法   Chapter实体类的getFictionId方法
+        lambdaQueryWrapper.eq(Chapter::getFictionId, 6);
+        lambdaQueryWrapper.eq(Chapter::getSort,10);
+        Chapter selected = chapterMapper.selectOne(lambdaQueryWrapper);
+        System.out.println(selected);
+    }
 }
 }

+ 41 - 0
springboot-demo/src/test/java/com/sf/PropertiesTests.java

@@ -0,0 +1,41 @@
+package com.sf;
+
+import com.sf.config.FoodConfig;
+import com.sf.config.VegetablesConfig;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+public class PropertiesTests {
+
+    @Value("${food.rice}")
+    private String rice;
+
+    @Value("${food.meat}")
+    private String meat;
+
+    @Test
+    public void test() {
+        System.out.println(meat + "+" + rice);
+    }
+
+    @Autowired
+    private FoodConfig foodConfig;
+
+    @Test
+    public void testFood(){
+        System.out.println(foodConfig.getMeat());
+        System.out.println(foodConfig.getRice());
+    }
+
+    @Autowired
+    private VegetablesConfig vegetablesConfig;
+    @Test
+    public void testVege(){
+        System.out.println(vegetablesConfig.getPotato());
+        System.out.println(vegetablesConfig.getEggplant());
+        System.out.println(vegetablesConfig.getGreenpeper());
+    }
+}

+ 17 - 0
springboot-demo/src/test/java/com/sf/YamlTests.java

@@ -0,0 +1,17 @@
+package com.sf;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+public class YamlTests {
+
+    @Value("${lines}")
+    private String lines;
+
+    @Test
+    public void test(){
+        System.out.println(lines);
+    }
+}