欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot快速入门

程序员文章站 2024-03-23 16:46:34
...

一、创建一个Maven项目不选框架
1.首先引入需要的依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.建一个类,写一个启动springboot的主函数并加上相应注解

@SpringBootApplication
public class BootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootDemoApplication.class,args);
    }
}

3.创建一个Controller

加@RestController注解,返回json就不需要在方法上加@ResponseBody了

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "你好springboot!";
    }

请求:
SpringBoot快速入门
二、配置连接池
1.首先加入依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>RELEASE</version>
  </dependency>

2.加入配置文件config.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456

3.java配置注入
类上加上@Configuration就相当于一个配置文件了

@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {

    @Value("${jdbc.url}")
    String url;
    @Value("${jdbc.driverClassName}")
    String driverClassName;
    @Value("${jdbc.username}")
    String username;
    @Value("${jdbc.password}")
    String password;

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

4.注入datasource

@RestController
public class HelloController {

    @Autowired
    private DataSource dataSource;

    @RequestMapping("/hello")
    public String hello(){
        return "你好springboot!";
    }
}

5.debug运行
注入成功
SpringBoot快速入门
springboot注入方式一:
1.加入依赖

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
   </dependency>

2.把上面的jdbc.properties改名成application.properties
3.创建一个properties类

@ConfigurationProperties(prefix = "jdbc")
@Data
public class JdbcProperties {
    String url;
    String driverClassName;
    String username;
    String password;
}

4.JdbcConfig类更改如下

@Configuration
//@PropertySource("classpath:application.properties")
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {

/*
    @Value("${jdbc.url}")
    String url;
    @Value("${jdbc.driverClassName}")
    String driverClassName;
    @Value("${jdbc.username}")
    String username;
    @Value("${jdbc.password}")
    String password;
*/
   /* //注解注入
    @Autowired
    JdbcProperties jdbcProperties;
    //构造方法注入
    public JdbcConfig(JdbcProperties jdbcProperties){
        this.jdbcProperties = jdbcProperties;
    }*/

    //参数输入
    @Bean
    public DataSource dataSource(JdbcProperties prop){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(prop.getDriverClassName());
        dataSource.setUrl(prop.getUrl());
        dataSource.setUsername(prop.getUsername());
        dataSource.setPassword(prop.getPassword());
        return dataSource;
    }

断点调试:
datasource成功注入
SpringBoot快速入门
springboot属性注入方式二:
更改如下

//@ConfigurationProperties(prefix = "jdbc")
@Data
public class JdbcProperties {
    String url;
    String driverClassName;
    String username;
    String password;
}
@Configuration
//@PropertySource("classpath:application.properties")
//@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {

/*
    @Value("${jdbc.url}")
    String url;
    @Value("${jdbc.driverClassName}")
    String driverClassName;
    @Value("${jdbc.username}")
    String username;
    @Value("${jdbc.password}")
    String password;
*/
   /* //注解注入
    @Autowired
    JdbcProperties jdbcProperties;
    //构造方法注入
    public JdbcConfig(JdbcProperties jdbcProperties){
        this.jdbcProperties = jdbcProperties;
    }*/

    //参数输入
 /*   @Bean
    public DataSource dataSource(JdbcProperties prop){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(prop.getDriverClassName());
        dataSource.setUrl(prop.getUrl());
        dataSource.setUsername(prop.getUsername());
        dataSource.setPassword(prop.getPassword());
        return dataSource;
    }*/

    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

三、yaml配置文件
除了可以用application.properties配置文件外,还可以用application.yaml配置文件

jdbc
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/test
  username: root
  password: 123456
  user:
    name: jack
    age: 21
    language:
      - java
      - php
      - ios
      - python
      - scala
@Data
public class JdbcProperties {
    String url;
    String driverClassName;
    String username;
    String password;
    User user = new User();

    class User{
        String name;
        int age;
        List<String> language;
    }