01_SpringBoot 入门案例

张创琦 2022.03.10

相关配置看文件说明。此处只展示代码和运行结果。

目录结构

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.lxs</groupId>
  7. <artifactId>spring-boot-demo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <java.version>1.8</java.version>
  11. </properties>
  12. <!-- 添加父坐标工程 -->
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.0.0.RELEASE</version>
  17. </parent>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.alibaba</groupId>
  25. <artifactId>druid</artifactId>
  26. <version>1.1.10</version>
  27. </dependency>
  28. </dependencies>
  29. </project>

application.properties (原名:jdbc.properties)

  1. jdbc.driverClassName=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm1
  3. jdbc.username=root
  4. jdbc.password=123456

Application.java

  1. package com.lxs;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

HelloController.java

  1. package com.lxs.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import javax.sql.DataSource;
  6. @RestController
  7. public class HelloController {
  8. @Autowired
  9. private DataSource dataSource;
  10. @GetMapping("/hello")
  11. public String hello(){
  12. return "hello, spring boot! " + dataSource;
  13. }
  14. }

JdbcConfig.java

方法1: 通过value注解

  1. package com.lxs.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.PropertySource;
  8. // 注意这里的包是sql的!!!
  9. import javax.management.MXBean;
  10. import javax.sql.DataSource;
  11. @Configuration
  12. @PropertySource("classpath:application.properties")
  13. public class JdbcConfig {
  14. @Value("${jdbc.url}")
  15. String url;
  16. @Value("${jdbc.driverClassName}")
  17. String driverClassName;
  18. @Value("${jdbc.username}")
  19. String username;
  20. @Value("${jdbc.password}")
  21. String password;
  22. @Bean
  23. public DataSource dataSource(){
  24. DruidDataSource dataSource = new DruidDataSource();
  25. dataSource.setUrl(url);
  26. dataSource.setDriverClassName(driverClassName);
  27. dataSource.setUsername(username);
  28. dataSource.setPassword(password);
  29. return dataSource;
  30. }
  31. }

方法2: 通过EnableConfigurationProperties注解

  1. package com.lxs.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.PropertySource;
  8. // 注意这里的包是sql的!!!
  9. import javax.management.MXBean;
  10. import javax.sql.DataSource;
  11. @Configuration
  12. @EnableConfigurationProperties(JdbcProperties.class)
  13. public class JdbcConfig {
  14. @Bean
  15. public DataSource dataSource(JdbcProperties jdbc){
  16. DruidDataSource dataSource = new DruidDataSource();
  17. dataSource.setUrl(jdbc.getUrl());
  18. dataSource.setDriverClassName(jdbc.getDriverClassName());
  19. dataSource.setUsername(jdbc.getUsername());
  20. dataSource.setPassword(jdbc.getPassword());
  21. return dataSource;
  22. }
  23. }

运行结果展示
  1. Tomcat initialized with port(s): 8080 (http)
  2. Servlet dispatcherServlet mapped to [/]
  3. Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.lxs.controller.HelloController.hello()

debug相关结果展示:

松散绑定

(未调试,记得调试)

举例:

可以将application.properties中的驼峰命名方式转换,

比如将 jdbc.driver_class_name 在 jdbcConfig 中可以转换为 jdbc.driverClassName 。

更优雅的注入

比如:删掉 JdbcProperties

JdbcConfig.java

  1. package com.lxs.config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.PropertySource;
  8. // 注意这里的包是sql的!!!
  9. import javax.management.MXBean;
  10. import javax.sql.DataSource;
  11. @Configuration
  12. public class JdbcConfig {
  13. // 自动将同名属性配置,高科技!
  14. @Bean
  15. public DataSource dataSource(){
  16. DruidDataSource dataSource = new DruidDataSource();
  17. return dataSource;
  18. }
  19. }