From ef5956efa0f388a05b731354b2a12fd82f9d5534 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:43:16 +0000 Subject: [PATCH 01/24] Bump shiro-spring from 1.4.0 to 1.7.0 in /15.Spring-Boot-Shiro-Ehcache Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 15.Spring-Boot-Shiro-Ehcache/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15.Spring-Boot-Shiro-Ehcache/pom.xml b/15.Spring-Boot-Shiro-Ehcache/pom.xml index d579d849..c3e901aa 100644 --- a/15.Spring-Boot-Shiro-Ehcache/pom.xml +++ b/15.Spring-Boot-Shiro-Ehcache/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From 7dc67c7f8417b80fb09ef5b93f6548d76f33ed80 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:43:22 +0000 Subject: [PATCH 02/24] Bump shiro-spring in /16.Spring-Boot-Shiro-Thymeleaf-Tag Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml b/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml index 250848ff..aededa31 100644 --- a/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml +++ b/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From 1c1e1b5f1e5174a347fd7f07ca760654860638d5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:43:29 +0000 Subject: [PATCH 03/24] Bump shiro-spring in /13.Spring-Boot-Shiro-Authorization Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 13.Spring-Boot-Shiro-Authorization/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/13.Spring-Boot-Shiro-Authorization/pom.xml b/13.Spring-Boot-Shiro-Authorization/pom.xml index 784930cf..c22e0776 100644 --- a/13.Spring-Boot-Shiro-Authorization/pom.xml +++ b/13.Spring-Boot-Shiro-Authorization/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From 093550e28f2da70031437175aabe7a3219f73508 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:43:35 +0000 Subject: [PATCH 04/24] Bump shiro-spring in /11.Spring-Boot-Shiro-Authentication Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 11.Spring-Boot-Shiro-Authentication/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/11.Spring-Boot-Shiro-Authentication/pom.xml b/11.Spring-Boot-Shiro-Authentication/pom.xml index 1fdb0b0a..8b4b003b 100644 --- a/11.Spring-Boot-Shiro-Authentication/pom.xml +++ b/11.Spring-Boot-Shiro-Authentication/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From 71e487bd6168d77ad0e82f6d09742b226605cb8b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:43:35 +0000 Subject: [PATCH 05/24] Bump shiro-spring from 1.4.0 to 1.7.0 in /17.Spring-Boot-Shiro-Session Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 17.Spring-Boot-Shiro-Session/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/17.Spring-Boot-Shiro-Session/pom.xml b/17.Spring-Boot-Shiro-Session/pom.xml index 65f14ebe..bce962a5 100644 --- a/17.Spring-Boot-Shiro-Session/pom.xml +++ b/17.Spring-Boot-Shiro-Session/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From 797fd828569dc2140fd1e6719b8f50383a9f2f40 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:43:52 +0000 Subject: [PATCH 06/24] Bump shiro-spring in /12.Spring-Boot-Shiro-RememberMe Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 12.Spring-Boot-Shiro-RememberMe/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/12.Spring-Boot-Shiro-RememberMe/pom.xml b/12.Spring-Boot-Shiro-RememberMe/pom.xml index c27b864c..97aed20b 100644 --- a/12.Spring-Boot-Shiro-RememberMe/pom.xml +++ b/12.Spring-Boot-Shiro-RememberMe/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From f21be9bf12d4d276c5fb6fa355633fac621654b7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Apr 2021 16:44:30 +0000 Subject: [PATCH 07/24] Bump shiro-spring from 1.4.0 to 1.7.0 in /14.Spring-Boot-Shiro-Redis Bumps shiro-spring from 1.4.0 to 1.7.0. Signed-off-by: dependabot[bot] --- 14.Spring-Boot-Shiro-Redis/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/14.Spring-Boot-Shiro-Redis/pom.xml b/14.Spring-Boot-Shiro-Redis/pom.xml index 055ba948..a1ec09bc 100644 --- a/14.Spring-Boot-Shiro-Redis/pom.xml +++ b/14.Spring-Boot-Shiro-Redis/pom.xml @@ -47,7 +47,7 @@ org.apache.shiro shiro-spring - 1.4.0 + 1.7.0 From 581559fe88c526605677aa23eee33349419a45c7 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 09:22:50 +0800 Subject: [PATCH 08/24] =?UTF-8?q?=E5=BC=80=E5=90=AFspringboot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.Start-Spring-Boot/pom.xml | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/01.Start-Spring-Boot/pom.xml b/01.Start-Spring-Boot/pom.xml index e9fe5c98..f7b62ce0 100644 --- a/01.Start-Spring-Boot/pom.xml +++ b/01.Start-Spring-Boot/pom.xml @@ -14,14 +14,14 @@ org.springframework.boot spring-boot-starter-parent - 1.5.9.RELEASE + 2.3.4.RELEASE UTF-8 UTF-8 - 1.7 + 1.8 @@ -37,13 +37,7 @@ - - - nexus-aliyun - Nexus aliyun - http://maven.aliyun.com/nexus/content/groups/public - - + From e17bf96f3ee744478cefab8fccad5f6c6684756b Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 09:23:10 +0800 Subject: [PATCH 09/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02.Spring-Boot-Config/pom.xml b/02.Spring-Boot-Config/pom.xml index b84d7045..5f8551f6 100644 --- a/02.Spring-Boot-Config/pom.xml +++ b/02.Spring-Boot-Config/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.9.RELEASE + 2.3.4.RELEASE From f89a76b30e582c15d7fcab63b5649348643634f5 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 09:23:54 +0800 Subject: [PATCH 10/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/pom.xml | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/02.Spring-Boot-Config/pom.xml b/02.Spring-Boot-Config/pom.xml index 5f8551f6..a23c99a6 100644 --- a/02.Spring-Boot-Config/pom.xml +++ b/02.Spring-Boot-Config/pom.xml @@ -21,7 +21,7 @@ UTF-8 UTF-8 - 1.7 + 1.8 @@ -38,14 +38,6 @@ - - - nexus-aliyun - Nexus aliyun - http://maven.aliyun.com/nexus/content/groups/public - - - @@ -55,5 +47,4 @@ - From 1b42d07c4a4a19d18634df204151219e6d840b11 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 21:29:52 +0800 Subject: [PATCH 11/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/src/main/resources/banner.txt | 2 +- 02.Spring-Boot-Config/src/main/resources/test.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/02.Spring-Boot-Config/src/main/resources/banner.txt b/02.Spring-Boot-Config/src/main/resources/banner.txt index a3965402..9b065100 100644 --- a/02.Spring-Boot-Config/src/main/resources/banner.txt +++ b/02.Spring-Boot-Config/src/main/resources/banner.txt @@ -1,4 +1,4 @@ _ _ _ _ _ _ / \ / \ / \ / \ / \ / \ -( m | r | b | i | r | d ) +( s | p | r | i | n | g ) \_/ \_/ \_/ \_/ \_/ \_/ diff --git a/02.Spring-Boot-Config/src/main/resources/test.properties b/02.Spring-Boot-Config/src/main/resources/test.properties index 25ef8a30..1e5172dc 100644 --- a/02.Spring-Boot-Config/src/main/resources/test.properties +++ b/02.Spring-Boot-Config/src/main/resources/test.properties @@ -1,2 +1,2 @@ -test.name=KangKang +test.name=abc test.age=25 \ No newline at end of file From 32137b6038a32ecd6e0005617861d35e96e80564 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 21:36:11 +0800 Subject: [PATCH 12/24] =?UTF-8?q?=E5=BC=80=E5=90=AFspringboot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {01.Start-Spring-Boot => 01.Spring-Boot-Start}/pom.xml | 2 +- .../src/main/java/com/springboot/demo/DemoApplication.java | 0 .../src/main/resources/application.properties | 0 .../src/test/java/com/springboot/demo/DemoApplicationTests.java | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename {01.Start-Spring-Boot => 01.Spring-Boot-Start}/pom.xml (97%) rename {01.Start-Spring-Boot => 01.Spring-Boot-Start}/src/main/java/com/springboot/demo/DemoApplication.java (100%) rename {01.Start-Spring-Boot => 01.Spring-Boot-Start}/src/main/resources/application.properties (100%) rename {01.Start-Spring-Boot => 01.Spring-Boot-Start}/src/test/java/com/springboot/demo/DemoApplicationTests.java (100%) diff --git a/01.Start-Spring-Boot/pom.xml b/01.Spring-Boot-Start/pom.xml similarity index 97% rename from 01.Start-Spring-Boot/pom.xml rename to 01.Spring-Boot-Start/pom.xml index f7b62ce0..0bbd41c0 100644 --- a/01.Start-Spring-Boot/pom.xml +++ b/01.Spring-Boot-Start/pom.xml @@ -8,7 +8,7 @@ 0.0.1-SNAPSHOT jar - Start-Spring-Boot + 01.Spring-Boot-Start Demo project for Spring Boot diff --git a/01.Start-Spring-Boot/src/main/java/com/springboot/demo/DemoApplication.java b/01.Spring-Boot-Start/src/main/java/com/springboot/demo/DemoApplication.java similarity index 100% rename from 01.Start-Spring-Boot/src/main/java/com/springboot/demo/DemoApplication.java rename to 01.Spring-Boot-Start/src/main/java/com/springboot/demo/DemoApplication.java diff --git a/01.Start-Spring-Boot/src/main/resources/application.properties b/01.Spring-Boot-Start/src/main/resources/application.properties similarity index 100% rename from 01.Start-Spring-Boot/src/main/resources/application.properties rename to 01.Spring-Boot-Start/src/main/resources/application.properties diff --git a/01.Start-Spring-Boot/src/test/java/com/springboot/demo/DemoApplicationTests.java b/01.Spring-Boot-Start/src/test/java/com/springboot/demo/DemoApplicationTests.java similarity index 100% rename from 01.Start-Spring-Boot/src/test/java/com/springboot/demo/DemoApplicationTests.java rename to 01.Spring-Boot-Start/src/test/java/com/springboot/demo/DemoApplicationTests.java From 6c04efbefd2c39e556f8da739b60ad536599f2bd Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 21:36:39 +0800 Subject: [PATCH 13/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/pom.xml | 2 +- .../main/java/com/springboot/Application.java | 1 - .../controller/IndexController.java | 22 +++++++++---------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/02.Spring-Boot-Config/pom.xml b/02.Spring-Boot-Config/pom.xml index a23c99a6..56ba3fb7 100644 --- a/02.Spring-Boot-Config/pom.xml +++ b/02.Spring-Boot-Config/pom.xml @@ -8,7 +8,7 @@ 0.0.1-SNAPSHOT jar - Spring-Boot-Config + 02.Spring-Boot-Config Demo project for Spring Boot diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java index eca23874..9f1a53ed 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java @@ -11,7 +11,6 @@ @EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) //@ImportResource({"classpath:some-application.xml"}) public class Application { - public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setAddCommandLineProperties(false); diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index 16063f01..cd4348bb 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java @@ -11,15 +11,15 @@ @RestController public class IndexController { - @Autowired - private BlogProperties blogProperties; - @Autowired - private ConfigBean configBean; - @Autowired - private TestConfigBean testConfigBean; - - @RequestMapping("/") - String index() { - return testConfigBean.getName()+","+testConfigBean.getAge(); - } + @Autowired + private BlogProperties blogProperties; + @Autowired + private ConfigBean configBean; + @Autowired + private TestConfigBean testConfigBean; + + @RequestMapping("/") + String index() { + return testConfigBean.getName() + "," + testConfigBean.getAge(); + } } From 0e9622e795f32a0329f5f7b4f699cdd6cb8faddd Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 22:23:45 +0800 Subject: [PATCH 14/24] =?UTF-8?q?=E5=BC=80=E5=90=AFspringboot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/{DemoApplication.java => Application01.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename 01.Spring-Boot-Start/src/main/java/com/springboot/demo/{DemoApplication.java => Application01.java} (83%) diff --git a/01.Spring-Boot-Start/src/main/java/com/springboot/demo/DemoApplication.java b/01.Spring-Boot-Start/src/main/java/com/springboot/demo/Application01.java similarity index 83% rename from 01.Spring-Boot-Start/src/main/java/com/springboot/demo/DemoApplication.java rename to 01.Spring-Boot-Start/src/main/java/com/springboot/demo/Application01.java index e584ba86..295712c4 100644 --- a/01.Spring-Boot-Start/src/main/java/com/springboot/demo/DemoApplication.java +++ b/01.Spring-Boot-Start/src/main/java/com/springboot/demo/Application01.java @@ -7,7 +7,7 @@ @RestController @SpringBootApplication -public class DemoApplication { +public class Application01 { @RequestMapping("/") String index() { @@ -15,6 +15,6 @@ String index() { } public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); + SpringApplication.run(Application01.class, args); } } From 0c6918ef82452acc1329f44114dfca8c164072bf Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 22:24:20 +0800 Subject: [PATCH 15/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/readme02.md | 224 ++++++++++++++++++ .../{Application.java => Application02.java} | 6 +- 2 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 02.Spring-Boot-Config/readme02.md rename 02.Spring-Boot-Config/src/main/java/com/springboot/{Application.java => Application02.java} (76%) diff --git a/02.Spring-Boot-Config/readme02.md b/02.Spring-Boot-Config/readme02.md new file mode 100644 index 00000000..ef88ee33 --- /dev/null +++ b/02.Spring-Boot-Config/readme02.md @@ -0,0 +1,224 @@ +## 定制Banner + +Spring Boot项目在启动的时候会有一个默认的启动图案: + +``` + . ____ _ __ _ _ + /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ +( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ + \\/ ___)| |_)| | | | | || (_| | ) ) ) ) + ' |____| .__|_| |_|_| |_\__, | / / / / + =========|_|==============|___/=/_/_/_/ + :: Spring Boot :: (v2.3.4.RELEASE) +``` + +我们可以把这个图案修改为自己想要的。在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。ASCII图案可通过网站http://www.network-science.de/ascii/一键生成,比如输入mrbird生成图案后复制到banner.txt,启动项目,控制台输出如下: + +``` + _ _ _ _ _ _ + / \ / \ / \ / \ / \ / \ +( s | p | r | i | n | g ) + \_/ \_/ \_/ \_/ \_/ \_/ +…… +2022-01-27 22:20:59.267 INFO 22216 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' +2022-01-27 22:20:59.386 INFO 22216 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' +2022-01-27 22:20:59.393 INFO 22216 --- [ main] com.springboot.Application : Started Application in 1.486 seconds (JVM running for 2.941) +``` + + + +banner也可以关闭,在main方法中: + +``` +public static void main(String[] args) { + SpringApplication app = new SpringApplication(DemoApplication.class); + app.setBannerMode(Mode.OFF); + app.run(args); +} +``` + + + +## 全局配置文件 + +在src/main/resources目录下,Spring Boot提供了一个名为application.properties的全局配置文件,可对一些默认配置的配置值进行修改。 + +> 附:[application.properties中可配置所有官方属性](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html) + +### 自定义属性值 + +Spring Boot允许我们在application.properties下自定义一些属性,比如: + +``` +mrbird.blog.name=mrbird's blog +mrbird.blog.title=Spring Boot +``` + + + +定义一个BlogProperties Bean,通过`@Value("${属性名}")`来加载配置文件中的属性值: + +``` +@Component +public class BlogProperties { + + @Value("${mrbird.blog.name}") + private String name; + + @Value("${mrbird.blog.title}") + private String title; + + // get,set略 +} +``` + + + +编写IndexController,注入该Bean: + +``` +@RestController +public class IndexController { + @Autowired + private BlogProperties blogProperties; + + @RequestMapping("/") + String index() { + return blogProperties.getName()+"——"+blogProperties.getTitle(); + } +} +``` + + + +启动项目,访问[http://localhost:8080](http://localhost:8080/),页面显示如下: + +![QQ截图20171130112735.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171130112735.png) + +在属性非常多的情况下,也可以定义一个和配置文件对应的Bean: + +``` +@ConfigurationProperties(prefix="mrbird.blog") +public class ConfigBean { + private String name; + private String title; + // get,set略 +} +``` + + + +通过注解`@ConfigurationProperties(prefix="mrbird.blog")`指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名一一对应。 + +除此之外还需在Spring Boot入口类加上注解`@EnableConfigurationProperties({ConfigBean.class})`来启用该配置: + +``` +@SpringBootApplication +@EnableConfigurationProperties({ConfigBean.class}) +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} +``` + + + +之后便可在IndexController中注入该Bean,并使用了: + +``` +@RestController +public class IndexController { + @Autowired + private ConfigBean configBean; + + @RequestMapping("/") + String index() { + return configBean.getName()+"——"+configBean.getTitle(); + } +} +``` + + + +### 属性间的引用 + +在application.properties配置文件中,各个属性可以相互引用,如下: + +``` +mrbird.blog.name=mrbird's blog +mrbird.blog.title=Spring Boot +mrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title} +``` + + + +## 自定义配置文件 + +除了可以在application.properties里配置属性,我们还可以自定义一个配置文件。在src/main/resources目录下新建一个test.properties: + +``` +test.name=KangKang +test.age=25 +``` + + + +定义一个对应该配置文件的Bean: + +``` +@Configuration +@ConfigurationProperties(prefix="test") +@PropertySource("classpath:test.properties") +@Component +public class TestConfigBean { + private String name; + private int age; + // get,set略 +} +``` + + + +注解`@PropertySource("classpath:test.properties")`指明了使用哪个配置文件。要使用该配置Bean,同样也需要在入口类里使用注解`@EnableConfigurationProperties({TestConfigBean.class})`来启用该配置。 + +## 通过命令行设置属性值 + +在运行Spring Boot jar文件时,可以使用命令`java -jar xxx.jar --server.port=8081`来改变端口的值。这条命令等价于我们手动到application.properties中修改(如果没有这条属性的话就添加)server.port属性的值为8081。 + +如果不想项目的配置被命令行修改,可以在入口文件的main方法中进行如下设置: + +``` +public static void main(String[] args) { + SpringApplication app = new SpringApplication(Application.class); + app.setAddCommandLineProperties(false); + app.run(args); +} +``` + + + +## 使用xml配置 + +虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解`@ImportResource({"classpath:some-application.xml"})`来引入xml配置文件。 + +## Profile配置 + +Profile用来针对不同的环境下使用不同的配置文件,多环境配置文件必须以`application-{profile}.properties`的格式命,其中`{profile}`为环境标识。比如定义两个配置文件: + +- application-dev.properties:开发环境 + + ``` + server.port=8080 + ``` + +- application-prod.properties:生产环境 + + ``` + server.port=8081 + ``` + +至于哪个具体的配置文件会被加载,需要在application.properties文件中通过`spring.profiles.active`属性来设置,其值对应`{profile}`值。 + +如:`spring.profiles.active=dev`就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令`java -jar xxx.jar --spring.profiles.active={profile}`切换不同的环境配置。 \ No newline at end of file diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java similarity index 76% rename from 02.Spring-Boot-Config/src/main/java/com/springboot/Application.java rename to 02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index 9f1a53ed..8a339e5b 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -1,5 +1,6 @@ package com.springboot; +import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -10,10 +11,11 @@ @SpringBootApplication @EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) //@ImportResource({"classpath:some-application.xml"}) -public class Application { +public class Application02 { public static void main(String[] args) { - SpringApplication app = new SpringApplication(Application.class); + SpringApplication app = new SpringApplication(Application02.class); app.setAddCommandLineProperties(false); + app.setBannerMode(Banner.Mode.OFF); app.run(args); } } From c4bb71cb139ec9e24d35990dff0d8e687a88e5f7 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 22:36:56 +0800 Subject: [PATCH 16/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/pom.xml | 8 +++++++- .../main/java/com/springboot/Application02.java | 2 +- .../java/com/springboot/bean/BlogProperties.java | 9 ++++++++- .../main/java/com/springboot/bean/ConfigBean.java | 12 ++++++++++-- .../springboot/controller/IndexController.java | 15 +++++++++++++-- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/02.Spring-Boot-Config/pom.xml b/02.Spring-Boot-Config/pom.xml index 56ba3fb7..3335efa2 100644 --- a/02.Spring-Boot-Config/pom.xml +++ b/02.Spring-Boot-Config/pom.xml @@ -29,7 +29,13 @@ org.springframework.boot spring-boot-starter-web - + + + org.springframework.boot + spring-boot-configuration-processor + true + + org.springframework.boot spring-boot-starter-test diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index 8a339e5b..bdbd1857 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -15,7 +15,7 @@ public class Application02 { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application02.class); app.setAddCommandLineProperties(false); - app.setBannerMode(Banner.Mode.OFF); +// app.setBannerMode(Banner.Mode.OFF); app.run(args); } } diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java index 15694fc6..b0e45d7d 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java @@ -27,5 +27,12 @@ public String getTitle() { public void setTitle(String title) { this.title = title; } - + + @Override + public String toString() { + return "BlogProperties{" + + "name='" + name + '\'' + + ", title='" + title + '\'' + + '}'; + } } diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java index 2a57ee13..c95e74d4 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java @@ -24,6 +24,14 @@ public String getWholeTitle() { } public void setWholeTitle(String wholeTitle) { this.wholeTitle = wholeTitle; - } - + } + + @Override + public String toString() { + return "ConfigBean{" + + "name='" + name + '\'' + + ", title='" + title + '\'' + + ", wholeTitle='" + wholeTitle + '\'' + + '}'; + } } diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index cd4348bb..dc1bf985 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java @@ -10,6 +10,7 @@ @RestController +@RequestMapping("/v2") public class IndexController { @Autowired private BlogProperties blogProperties; @@ -18,8 +19,18 @@ public class IndexController { @Autowired private TestConfigBean testConfigBean; - @RequestMapping("/") - String index() { + @RequestMapping("/blog") + public String testBlog(){ + return blogProperties.toString(); + } + + @RequestMapping("/conf") + public String testConfig(){ + return configBean.toString(); + } + + @RequestMapping("/test") + public String index() { return testConfigBean.getName() + "," + testConfigBean.getAge(); } } From 29c31bf0a63b43c16591880d1eb5b3b7dbf9c627 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 23:05:46 +0800 Subject: [PATCH 17/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/springboot/Application02.java | 4 +- .../java/com/springboot/bean/ConfigBean.java | 7 +++ .../java/com/springboot/bean/ConfigBean2.java | 45 +++++++++++++++++++ .../com/springboot/bean/TestConfigBean.java | 42 ++++++++++------- ...roperties.java => ValueGetProperties.java} | 7 ++- .../controller/IndexController.java | 19 ++++++-- .../main/resources/application-dev.properties | 2 +- .../resources/application-prod.properties | 2 +- .../src/main/resources/application.properties | 4 ++ 9 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java rename 02.Spring-Boot-Config/src/main/java/com/springboot/bean/{BlogProperties.java => ValueGetProperties.java} (79%) diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index bdbd1857..4189f4d4 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -9,8 +9,8 @@ import com.springboot.bean.TestConfigBean; @SpringBootApplication -@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) -//@ImportResource({"classpath:some-application.xml"}) +@EnableConfigurationProperties({ConfigBean.class}) +//@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) public class Application02 { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application02.class); diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java index c95e74d4..6f81ad4f 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java @@ -2,6 +2,13 @@ import org.springframework.boot.context.properties.ConfigurationProperties; +/** + * @author: wyq + * @create time: 2022/1/27 + * @description: + * ProfileProperties类没有加@Component注解。 + * 在要使用ProfileProperties的地方使用@EnableConfigurationProperties注册我们的配置 bean: + */ @ConfigurationProperties(prefix="mrbird.blog") public class ConfigBean { private String name; diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java new file mode 100644 index 00000000..0cab0584 --- /dev/null +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java @@ -0,0 +1,45 @@ +package com.springboot.bean; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * @author: wyq + * @create time: 2022/1/27 + * @description: + * 通过@ConfigurationProperties读取配置信息并与bean绑定。 + */ +@Component +@ConfigurationProperties(prefix="my.config.get") +public class ConfigBean2 { + private String name; + private String title; + private String wholeTitle; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getTitle() { + return title; + } + public void setTitle(String title) { + this.title = title; + } + public String getWholeTitle() { + return wholeTitle; + } + public void setWholeTitle(String wholeTitle) { + this.wholeTitle = wholeTitle; + } + + @Override + public String toString() { + return "ConfigBean{" + + "name='" + name + '\'' + + ", title='" + title + '\'' + + ", wholeTitle='" + wholeTitle + '\'' + + '}'; + } +} diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java index 46673053..66e1c409 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java @@ -5,24 +5,32 @@ import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; -@Configuration -@ConfigurationProperties(prefix="test") +/** + * @author: wyq + * @create time: 2022/1/27 + * @description: @PropertySource读取指定 properties文件 + */ +@ConfigurationProperties(prefix = "test") @PropertySource("classpath:test.properties") @Component public class TestConfigBean { - private String name; - private int age; - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public int getAge() { - return age; - } - public void setAge(int age) { - this.age = age; - } - + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + } diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ValueGetProperties.java similarity index 79% rename from 02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java rename to 02.Spring-Boot-Config/src/main/java/com/springboot/bean/ValueGetProperties.java index b0e45d7d..3add7bec 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ValueGetProperties.java @@ -3,8 +3,13 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +/** + * @author: wyq + * @create time: 2022/1/27 + * @description: 使用@Value("${property}")读取比较简单的配置信息 + */ @Component -public class BlogProperties { +public class ValueGetProperties { @Value("${mrbird.blog.name}") private String name; diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index dc1bf985..d1ce64a9 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java @@ -1,10 +1,11 @@ package com.springboot.controller; +import com.springboot.bean.ConfigBean2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.springboot.bean.BlogProperties; +import com.springboot.bean.ValueGetProperties; import com.springboot.bean.ConfigBean; import com.springboot.bean.TestConfigBean; @@ -13,22 +14,32 @@ @RequestMapping("/v2") public class IndexController { @Autowired - private BlogProperties blogProperties; + private ValueGetProperties blogProperties; + @Autowired private ConfigBean configBean; + + @Autowired + private ConfigBean2 configBean2; + @Autowired private TestConfigBean testConfigBean; @RequestMapping("/blog") - public String testBlog(){ + public String testBlog() { return blogProperties.toString(); } @RequestMapping("/conf") - public String testConfig(){ + public String testConfig() { return configBean.toString(); } + @RequestMapping("/conf2") + public String testConfig2() { + return configBean2.toString(); + } + @RequestMapping("/test") public String index() { return testConfigBean.getName() + "," + testConfigBean.getAge(); diff --git a/02.Spring-Boot-Config/src/main/resources/application-dev.properties b/02.Spring-Boot-Config/src/main/resources/application-dev.properties index a3ac65ce..53e50368 100644 --- a/02.Spring-Boot-Config/src/main/resources/application-dev.properties +++ b/02.Spring-Boot-Config/src/main/resources/application-dev.properties @@ -1 +1 @@ -server.port=8080 \ No newline at end of file +server.port=8002 \ No newline at end of file diff --git a/02.Spring-Boot-Config/src/main/resources/application-prod.properties b/02.Spring-Boot-Config/src/main/resources/application-prod.properties index bafddced..53e50368 100644 --- a/02.Spring-Boot-Config/src/main/resources/application-prod.properties +++ b/02.Spring-Boot-Config/src/main/resources/application-prod.properties @@ -1 +1 @@ -server.port=8081 \ No newline at end of file +server.port=8002 \ No newline at end of file diff --git a/02.Spring-Boot-Config/src/main/resources/application.properties b/02.Spring-Boot-Config/src/main/resources/application.properties index 1853bd2b..4bd27d7f 100644 --- a/02.Spring-Boot-Config/src/main/resources/application.properties +++ b/02.Spring-Boot-Config/src/main/resources/application.properties @@ -2,4 +2,8 @@ mrbird.blog.name=mrbird's blog mrbird.blog.title=Spring Boot mrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title} +my.config.get.name=abc +my.config.get.title=test +my.config.get.wholeTitle=${my.config.get.name}--${my.config.get.title} + spring.profiles.active=dev From 23027194e1ef6c2b55255e8307091abac9fc4fce Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 23:07:56 +0800 Subject: [PATCH 18/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/springboot/Application02.java | 5 ++--- .../bean/{TestConfigBean.java => PropertySourceGet.java} | 2 +- .../main/java/com/springboot/controller/IndexController.java | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) rename 02.Spring-Boot-Config/src/main/java/com/springboot/bean/{TestConfigBean.java => PropertySourceGet.java} (95%) diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index 4189f4d4..68637c6c 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -1,16 +1,15 @@ package com.springboot; -import org.springframework.boot.Banner; +import com.springboot.bean.PropertySourceGet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import com.springboot.bean.ConfigBean; -import com.springboot.bean.TestConfigBean; @SpringBootApplication @EnableConfigurationProperties({ConfigBean.class}) -//@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) +//@EnableConfigurationProperties({ConfigBean.class, PropertySourceGet.class}) public class Application02 { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application02.class); diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java similarity index 95% rename from 02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java rename to 02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java index 66e1c409..08b3acdf 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java @@ -13,7 +13,7 @@ @ConfigurationProperties(prefix = "test") @PropertySource("classpath:test.properties") @Component -public class TestConfigBean { +public class PropertySourceGet { private String name; private int age; diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index d1ce64a9..c1eec6e3 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java @@ -7,7 +7,7 @@ import com.springboot.bean.ValueGetProperties; import com.springboot.bean.ConfigBean; -import com.springboot.bean.TestConfigBean; +import com.springboot.bean.PropertySourceGet; @RestController @@ -23,7 +23,7 @@ public class IndexController { private ConfigBean2 configBean2; @Autowired - private TestConfigBean testConfigBean; + private PropertySourceGet testConfigBean; @RequestMapping("/blog") public String testBlog() { From 71d2231b6c0e4f6bbab49322123e989e275f38f4 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 23:26:21 +0800 Subject: [PATCH 19/24] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot/bean/PropertySourceGet.java | 4 +- .../springboot/bean/PropertySourceGet2.java | 44 +++++++++++++++++++ .../controller/IndexController.java | 13 +++--- .../yml/MixPropertySourceFactory.java | 38 ++++++++++++++++ .../{test.properties => property.properties} | 0 .../src/main/resources/property.yml | 3 ++ 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java create mode 100644 02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java rename 02.Spring-Boot-Config/src/main/resources/{test.properties => property.properties} (100%) create mode 100644 02.Spring-Boot-Config/src/main/resources/property.yml diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java index 08b3acdf..9b0460a0 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java @@ -9,9 +9,11 @@ * @author: wyq * @create time: 2022/1/27 * @description: @PropertySource读取指定 properties文件 + * @PropertySource不支持加载yml配置文件,所以需要自定义一个配置类 */ @ConfigurationProperties(prefix = "test") -@PropertySource("classpath:test.properties") +@PropertySource("classpath:property.properties") +//@PropertySource("classpath:property.yml") //此时获取name和age分别为null,0 @Component public class PropertySourceGet { private String name; diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java new file mode 100644 index 00000000..85e6fa71 --- /dev/null +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java @@ -0,0 +1,44 @@ +package com.springboot.bean; + +import com.springboot.yml.MixPropertySourceFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +/** + * @author: wyq + * @create time: 2022/1/27 + * @description: @PropertySource读取指定 properties文件 + * @PropertySource不支持加载yml配置文件,所以需要自定义一个配置类 + */ +@ConfigurationProperties(prefix = "test2") +@PropertySource(value = {"classpath:property.yml"}, factory = MixPropertySourceFactory.class) +@Component +public class PropertySourceGet2 { + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return "PropertySourceGet2{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } +} diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index c1eec6e3..6575ec5a 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java @@ -1,14 +1,10 @@ package com.springboot.controller; -import com.springboot.bean.ConfigBean2; +import com.springboot.bean.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.springboot.bean.ValueGetProperties; -import com.springboot.bean.ConfigBean; -import com.springboot.bean.PropertySourceGet; - @RestController @RequestMapping("/v2") @@ -24,6 +20,8 @@ public class IndexController { @Autowired private PropertySourceGet testConfigBean; + @Autowired + private PropertySourceGet2 testConfigBean2; @RequestMapping("/blog") public String testBlog() { @@ -44,4 +42,9 @@ public String testConfig2() { public String index() { return testConfigBean.getName() + "," + testConfigBean.getAge(); } + + @RequestMapping("/test2") + public String index2() { + return testConfigBean2.toString(); + } } diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java b/02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java new file mode 100644 index 00000000..7bd2e7e0 --- /dev/null +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java @@ -0,0 +1,38 @@ +package com.springboot.yml; + +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.DefaultPropertySourceFactory; +import org.springframework.core.io.support.EncodedResource; + +import java.io.IOException; +import java.util.Properties; + +/** + * @author :wyq + * @date :Created in 2022/1/27 + * @description : + */ +public class MixPropertySourceFactory extends DefaultPropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + String sourceName = name != null ? name : resource.getResource().getFilename(); + if (!resource.getResource().exists()) { + return new PropertiesPropertySource(sourceName, new Properties()); + } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) { + Properties propertiesFromYaml = loadYml(resource); + return new PropertiesPropertySource(sourceName, propertiesFromYaml); + } else { + return super.createPropertySource(name, resource); + } + } + + private Properties loadYml(EncodedResource resource) throws IOException { + YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); + factory.setResources(resource.getResource()); + factory.afterPropertiesSet(); + return factory.getObject(); + } +} diff --git a/02.Spring-Boot-Config/src/main/resources/test.properties b/02.Spring-Boot-Config/src/main/resources/property.properties similarity index 100% rename from 02.Spring-Boot-Config/src/main/resources/test.properties rename to 02.Spring-Boot-Config/src/main/resources/property.properties diff --git a/02.Spring-Boot-Config/src/main/resources/property.yml b/02.Spring-Boot-Config/src/main/resources/property.yml new file mode 100644 index 00000000..dd196d42 --- /dev/null +++ b/02.Spring-Boot-Config/src/main/resources/property.yml @@ -0,0 +1,3 @@ +test2: + name: property + age: 50 \ No newline at end of file From 00ca5c9d9424bdf456a1de92b81315b8142a0c74 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Fri, 28 Jan 2022 15:44:58 +0800 Subject: [PATCH 20/24] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E4=BD=BF=E7=94=A8M?= =?UTF-8?q?yBatis=20=20oracle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.Spring-Boot-MyBatis/pom.xml | 19 +- 03.Spring-Boot-MyBatis/readme03.md | 349 ++++++++++++++++++ .../{Application.java => Application03.java} | 4 +- .../src/main/resources/application.yml | 4 +- 4 files changed, 366 insertions(+), 10 deletions(-) create mode 100644 03.Spring-Boot-MyBatis/readme03.md rename 03.Spring-Boot-MyBatis/src/main/java/com/springboot/{Application.java => Application03.java} (73%) diff --git a/03.Spring-Boot-MyBatis/pom.xml b/03.Spring-Boot-MyBatis/pom.xml index fc26d684..7dfbd785 100644 --- a/03.Spring-Boot-MyBatis/pom.xml +++ b/03.Spring-Boot-MyBatis/pom.xml @@ -8,20 +8,20 @@ 0.0.1-SNAPSHOT jar - demo + 03.Spring-Boot-MyBatis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent - 1.5.9.RELEASE + 2.3.4.RELEASE UTF-8 UTF-8 - 1.7 + 1.8 @@ -34,7 +34,7 @@ org.mybatis.spring.boot mybatis-spring-boot-starter - 1.3.1 + 2.1.4 @@ -43,15 +43,22 @@ test - + + + + com.oracle + ojdbc6 + 11.2.0.4.0-atlassian-hosted + com.alibaba druid-spring-boot-starter - 1.1.6 + 1.2.4 diff --git a/03.Spring-Boot-MyBatis/readme03.md b/03.Spring-Boot-MyBatis/readme03.md new file mode 100644 index 00000000..b7e68513 --- /dev/null +++ b/03.Spring-Boot-MyBatis/readme03.md @@ -0,0 +1,349 @@ +整合MyBatis之前,先搭建一个基本的Spring Boot项目开启Spring Boot。然后引入`mybatis-spring-boot-starter`和数据库连接驱动(这里使用关系型数据库Oracle 11g)。 + +## mybatis-spring-boot-starter + +在pom中引入: + +``` + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.1 + +``` + + + +不同版本的Spring Boot和MyBatis版本对应不一样,具体可查看官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/。 + +通过`dependency:tree`命令查看`mybatis-spring-boot-starter`都有哪些隐性依赖: + +``` ++- org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:1.3.1:compile +| +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.9.RELEASE:compile +| | +- org.apache.tomcat:tomcat-jdbc:jar:8.5.23:compile +| | | \- org.apache.tomcat:tomcat-juli:jar:8.5.23:compile +| | \- org.springframework:spring-jdbc:jar:4.3.13.RELEASE:compile +| | \- org.springframework:spring-tx:jar:4.3.13.RELEASE:compile +| +- org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:jar:1.3.1:compile +| +- org.mybatis:mybatis:jar:3.4.5:compile +| \- org.mybatis:mybatis-spring:jar:1.3.1:compile +``` + + + +可发现其包含了`spring-boot-starter-jdbc`依赖,默认使用tomcat-jdbc数据源。 + +## 引入ojdbc6 + +由于版权的原因,我们需要将ojdbc6.jar依赖安装到本地的maven仓库,然后才可以在pom中进行配置。 + +下载ojdbc6.jar文件后,将其放到比较好找的目录下,比如D盘根目录。然后运行以下命令: + +``` +C:\Users\Administrator>mvn install:install-file -Dfile=D:/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=6.0 - +Dpackaging=jar -DgeneratePom=true + +... +[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom --- +[INFO] Installing D:\ojdbc6.jar to D:\m2\repository\com\oracle\ojdbc6\6.0\ojdbc6-6.0.jar +[INFO] Installing C:\Users\ADMINI~1\AppData\Local\Temp\mvninstall9103688544010617483.pom to D:\m2\repository\com\oracle\ojdbc +6\6.0\ojdbc6-6.0.pom +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD SUCCESS +[INFO] ------------------------------------------------------------------------ +[INFO] Total time: 0.940 s +[INFO] Finished at: 2017-08-13T15:06:38+08:00 +[INFO] Final Memory: 6M/145M +[INFO] ------------------------------------------------------------------------ +``` + + + +接着在pom中引入: + +``` + + com.oracle + ojdbc6 + 6.0 + +``` + + + +这里的groupid就是你之前安装时指定的-Dgroupid的值,artifactid就是你安装时指定的-Dartifactid的值,version也一样。 + +## Druid数据源 + +Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,地址:https://github.com/alibaba/druid。Druid不但提供连接池的功能,还提供监控功能,可以实时查看数据库连接池和SQL查询的工作情况。 + +### 配置Druid依赖 + +Druid为Spring Boot项目提供了对应的starter: + +``` + + com.alibaba + druid-spring-boot-starter + 1.1.6 + +``` + + + +### Druid数据源配置 + +上面通过查看mybatis starter的隐性依赖发现,Spring Boot的数据源配置的默认类型是`org.apache.tomcat.jdbc.pool.Datasource`,为了使用Druid连接池,需要在application.yml下配置: + +``` +server: + context-path: /web + +spring: + datasource: + druid: + # 数据库访问配置, 使用druid数据源 + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name: oracle.jdbc.driver.OracleDriver + url: jdbc:oracle:thin:@localhost:1521:ORCL + username: scott + password: 123456 + # 连接池配置 + initial-size: 5 + min-idle: 5 + max-active: 20 + # 连接等待超时时间 + max-wait: 30000 + # 配置检测可以关闭的空闲连接间隔时间 + time-between-eviction-runs-millis: 60000 + # 配置连接在池中的最小生存时间 + min-evictable-idle-time-millis: 300000 + validation-query: select '1' from dual + test-while-idle: true + test-on-borrow: false + test-on-return: false + # 打开PSCache,并且指定每个连接上PSCache的大小 + pool-prepared-statements: true + max-open-prepared-statements: 20 + max-pool-prepared-statement-per-connection-size: 20 + # 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙 + filters: stat,wall + # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔 + aop-patterns: com.springboot.servie.* + + + # WebStatFilter配置 + web-stat-filter: + enabled: true + # 添加过滤规则 + url-pattern: /* + # 忽略过滤的格式 + exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' + + # StatViewServlet配置 + stat-view-servlet: + enabled: true + # 访问路径为/druid时,跳转到StatViewServlet + url-pattern: /druid/* + # 是否能够重置数据 + reset-enable: false + # 需要账号密码才能访问控制台 + login-username: druid + login-password: druid123 + # IP白名单 + # allow: 127.0.0.1 + # IP黑名单(共同存在时,deny优先于allow) + # deny: 192.168.1.218 + + # 配置StatFilter + filter: + stat: + log-slow-sql: true +``` + + + +上述配置不但配置了Druid作为连接池,而且还开启了Druid的监控功能。 其他配置可参考官方wiki——https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter + +此时,运行项目,访问http://localhost:8080/web/druid: + +![QQ截图20171204160941.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204160941.png) + +输入账号密码即可看到Druid监控后台: + +![QQ截图20171204161133.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204161133.png) + +关于Druid的更多说明,可查看官方wiki——[https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98](https://github.com/alibaba/druid/wiki/常见问题) + +## 使用MyBatis + +使用的库表: + +``` +CREATE TABLE "SCOTT"."STUDENT" ( + "SNO" VARCHAR2(3 BYTE) NOT NULL , + "SNAME" VARCHAR2(9 BYTE) NOT NULL , + "SSEX" CHAR(2 BYTE) NOT NULL +); + +INSERT INTO "SCOTT"."STUDENT" VALUES ('001', 'KangKang', 'M '); +INSERT INTO "SCOTT"."STUDENT" VALUES ('002', 'Mike', 'M '); +INSERT INTO "SCOTT"."STUDENT" VALUES ('003', 'Jane', 'F '); +``` + + + +创建对应实体: + +``` +public class Student implements Serializable{ + private static final long serialVersionUID = -339516038496531943L; + private String sno; + private String name; + private String sex; + // get,set略 +} +``` + + + +创建一个包含基本CRUD的StudentMapper: + +``` +public interface StudentMapper { + int add(Student student); + int update(Student student); + int deleteByIds(String sno); + Student queryStudentById(Long id); +} +``` + + + +StudentMapper的实现可以基于xml也可以基于注解。 + +### 使用注解方式 + +继续编辑StudentMapper: + +``` +@Component +@Mapper +public interface StudentMapper { + @Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})") + int add(Student student); + + @Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}") + int update(Student student); + + @Delete("delete from student where sno=#{sno}") + int deleteBysno(String sno); + + @Select("select * from student where sno=#{sno}") + @Results(id = "student",value= { + @Result(property = "sno", column = "sno", javaType = String.class), + @Result(property = "name", column = "sname", javaType = String.class), + @Result(property = "sex", column = "ssex", javaType = String.class) + }) + Student queryStudentBySno(String sno); +``` + + + +简单的语句只需要使用@Insert、@Update、@Delete、@Select这4个注解即可,动态SQL语句需要使用@InsertProvider、@UpdateProvider、@DeleteProvider、@SelectProvider等注解。具体可参考MyBatis官方文档:http://www.mybatis.org/mybatis-3/zh/java-api.html。 + +### 使用xml方式 + +使用xml方式需要在application.yml中进行一些额外的配置: + +``` +mybatis: + # type-aliases扫描路径 + # type-aliases-package: + # mapper xml实现扫描路径 + mapper-locations: classpath:mapper/*.xml + property: + order: BEFORE +``` + + + +## 测试 + +接下来编写Service: + +``` +public interface StudentService { + int add(Student student); + int update(Student student); + int deleteBysno(String sno); + Student queryStudentBySno(String sno); +} +``` + + + +实现类: + +``` +@Service("studentService") +public class StudentServiceImp implements StudentService{ + @Autowired + private StudentMapper studentMapper; + + @Override + public int add(Student student) { + return this.studentMapper.add(student); + } + + @Override + public int update(Student student) { + return this.studentMapper.update(student); + } + + @Override + public int deleteBysno(String sno) { + return this.studentMapper.deleteBysno(sno); + } + + @Override + public Student queryStudentBySno(String sno) { + return this.studentMapper.queryStudentBySno(sno); + } +} +``` + + + +编写controller: + +``` +@RestController +public class TestController { + + @Autowired + private StudentService studentService; + + @RequestMapping( value = "/querystudent", method = RequestMethod.GET) + public Student queryStudentBySno(String sno) { + return this.studentService.queryStudentBySno(sno); + } +} +``` + + + +完整的项目目录如下图所示: + +![QQ截图20171204184527.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204184527.png) + +启动项目访问:http://localhost:8080/web/querystudent?sno=001: + +![QQ截图20171204171627.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204171627.png) + +查看SQL监控情况: + +![QQ截图20171204184402.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204184402.png) + +可看到其记录的就是刚刚访问[/querystudent](https://mrbird.cc/querystudent)得到的SQL。 \ No newline at end of file diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java similarity index 73% rename from 03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application.java rename to 03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java index a66ea9fe..dadb0333 100644 --- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application.java +++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java @@ -5,9 +5,9 @@ @SpringBootApplication -public class Application { +public class Application03 { public static void main(String[] args) { - SpringApplication.run(Application.class,args); + SpringApplication.run(Application03.class,args); } } diff --git a/03.Spring-Boot-MyBatis/src/main/resources/application.yml b/03.Spring-Boot-MyBatis/src/main/resources/application.yml index 73b2b848..f479f57e 100644 --- a/03.Spring-Boot-MyBatis/src/main/resources/application.yml +++ b/03.Spring-Boot-MyBatis/src/main/resources/application.yml @@ -8,8 +8,8 @@ spring: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:ORCL - username: test - password: 123456 + username: scott + password: admin12345 # 连接池配置 initial-size: 5 min-idle: 5 From 0925a9b64ff1b99b78adf40ead6867af0ee24154 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Fri, 28 Jan 2022 18:11:40 +0800 Subject: [PATCH 21/24] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E4=BD=BF=E7=94=A8M?= =?UTF-8?q?yBatis=20=20oracle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.Spring-Boot-MyBatis/pom.xml | 8 ++------ 03.Spring-Boot-MyBatis/readme03.md | 9 ++++++--- .../src/main/resources/application.yml | 5 ++++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/03.Spring-Boot-MyBatis/pom.xml b/03.Spring-Boot-MyBatis/pom.xml index 7dfbd785..bbb9640c 100644 --- a/03.Spring-Boot-MyBatis/pom.xml +++ b/03.Spring-Boot-MyBatis/pom.xml @@ -42,16 +42,12 @@ spring-boot-starter-test test - - + com.oracle ojdbc6 + 11.2.0.4.0-atlassian-hosted diff --git a/03.Spring-Boot-MyBatis/readme03.md b/03.Spring-Boot-MyBatis/readme03.md index b7e68513..b931756d 100644 --- a/03.Spring-Boot-MyBatis/readme03.md +++ b/03.Spring-Boot-MyBatis/readme03.md @@ -38,6 +38,8 @@ 由于版权的原因,我们需要将ojdbc6.jar依赖安装到本地的maven仓库,然后才可以在pom中进行配置。 +https://www.oracle.com/database/technologies/faq-jdbc.html#02_03 + 下载ojdbc6.jar文件后,将其放到比较好找的目录下,比如D盘根目录。然后运行以下命令: ``` @@ -70,8 +72,6 @@ Dpackaging=jar -DgeneratePom=true ``` - - 这里的groupid就是你之前安装时指定的-Dgroupid的值,artifactid就是你安装时指定的-Dartifactid的值,version也一样。 ## Druid数据源 @@ -97,8 +97,11 @@ Druid为Spring Boot项目提供了对应的starter: 上面通过查看mybatis starter的隐性依赖发现,Spring Boot的数据源配置的默认类型是`org.apache.tomcat.jdbc.pool.Datasource`,为了使用Druid连接池,需要在application.yml下配置: ``` +#SpringBoot在2.0版本之后已经弃用server.context-path,而代替为server.servlet.context-path server: - context-path: /web + servlet: + context-path: /web + port: 8003 spring: datasource: diff --git a/03.Spring-Boot-MyBatis/src/main/resources/application.yml b/03.Spring-Boot-MyBatis/src/main/resources/application.yml index f479f57e..8df7a489 100644 --- a/03.Spring-Boot-MyBatis/src/main/resources/application.yml +++ b/03.Spring-Boot-MyBatis/src/main/resources/application.yml @@ -1,5 +1,8 @@ +#SpringBoot在2.0版本之后已经弃用server.context-path,而代替为server.servlet.context-path server: - context-path: /web + servlet: + context-path: /web + port: 8003 spring: datasource: From a69debde360a7444c40af663f0310d1c591ddecf Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Fri, 28 Jan 2022 18:12:29 +0800 Subject: [PATCH 22/24] =?UTF-8?q?=E5=BC=80=E5=90=AFspringboot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01.Spring-Boot-Start/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01.Spring-Boot-Start/pom.xml b/01.Spring-Boot-Start/pom.xml index 0bbd41c0..aca509fa 100644 --- a/01.Spring-Boot-Start/pom.xml +++ b/01.Spring-Boot-Start/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.springboot - Start-Spring-Boot + Spring-Boot-Start 0.0.1-SNAPSHOT jar From 4e523e15a79afa9d876341b76bdc4397fdc1cfdc Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Fri, 28 Jan 2022 21:54:15 +0800 Subject: [PATCH 23/24] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E4=BD=BF=E7=94=A8M?= =?UTF-8?q?yBatis=20=20oracle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.Spring-Boot-MyBatis/readme03.md | 8 +++++--- .../src/main/java/com/springboot/Application03.java | 7 +++---- .../java/com/springboot/controller/TestController.java | 7 +++++-- .../main/java/com/springboot/service/StudentService.java | 3 +++ .../com/springboot/service/impl/StudentServiceImp.java | 1 - 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/03.Spring-Boot-MyBatis/readme03.md b/03.Spring-Boot-MyBatis/readme03.md index b931756d..81e1e351 100644 --- a/03.Spring-Boot-MyBatis/readme03.md +++ b/03.Spring-Boot-MyBatis/readme03.md @@ -8,11 +8,11 @@ org.mybatis.spring.boot mybatis-spring-boot-starter - 1.3.1 + 2.3.4.RELEASE ``` - +orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=gelc123 entries=5 force=y 不同版本的Spring Boot和MyBatis版本对应不一样,具体可查看官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/。 @@ -347,6 +347,8 @@ public class TestController { 查看SQL监控情况: +http://localhost:8003/web/druid/ + ![QQ截图20171204184402.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204184402.png) -可看到其记录的就是刚刚访问[/querystudent](https://mrbird.cc/querystudent)得到的SQL。 \ No newline at end of file +可看到其记录的就是刚刚访问/querystudent得到的SQL。 \ No newline at end of file diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java index dadb0333..a99e61aa 100644 --- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java +++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java @@ -6,8 +6,7 @@ @SpringBootApplication public class Application03 { - - public static void main(String[] args) { - SpringApplication.run(Application03.class,args); - } + public static void main(String[] args) { + SpringApplication.run(Application03.class, args); + } } diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java index 3fc7b8b0..c9af1db7 100644 --- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java +++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java @@ -13,9 +13,12 @@ public class TestController { @Autowired private StudentService studentService; - + + /** + * http://localhost:8003/web/querystudent?sno=002 + */ @RequestMapping( value = "/querystudent", method = RequestMethod.GET) public Student queryStudentBySno(String sno) { - return this.studentService.queryStudentBySno(sno); + return studentService.queryStudentBySno(sno); } } diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java index fcfd40b5..fc7a11a3 100644 --- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java +++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java @@ -4,7 +4,10 @@ public interface StudentService { int add(Student student); + int update(Student student); + int deleteBysno(String sno); + Student queryStudentBySno(String sno); } diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java index 8983d001..be4a58e1 100644 --- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java +++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java @@ -2,7 +2,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; - import com.springboot.bean.Student; import com.springboot.mapper.StudentMapper; import com.springboot.service.StudentService; From 22c753c2523e8a637f007d9e7ec2eb46ae332d58 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Fri, 28 Jan 2022 22:19:49 +0800 Subject: [PATCH 24/24] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E4=BD=BF=E7=94=A8M?= =?UTF-8?q?yBatis=20=20oracle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.Spring-Boot-MyBatis/readme03.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/03.Spring-Boot-MyBatis/readme03.md b/03.Spring-Boot-MyBatis/readme03.md index 81e1e351..d9fd0c75 100644 --- a/03.Spring-Boot-MyBatis/readme03.md +++ b/03.Spring-Boot-MyBatis/readme03.md @@ -335,11 +335,9 @@ public class TestController { } ``` - - 完整的项目目录如下图所示: -![QQ截图20171204184527.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171204184527.png) +![image-20220128215438475](C:\Users\yunqing3.wang\AppData\Roaming\Typora\typora-user-images\image-20220128215438475.png) 启动项目访问:http://localhost:8080/web/querystudent?sno=001: