SpringBoot基本使用

1.入门案例

1.创建项目

也可以在官网创建springboot项目

2.新建一个controller类

package com.yrh.controller;

@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/{id}")
    public String getById(Integer id){
        System.out.println("id = "+id);
        return "hello spring boot";
    }
}

3.运行

然后运行Springboot01demoApplication类,在postman中测试连接,服务器正确的收到了id,并返回字符串,说明springboot程序成功执行

 2.springboot的配置

1.修改端口号:

也可以将此处的ym扩展名改为yaml

配置文件优先级:peopertiex > yml > yaml 

以后做配置主要在yml中配置

 2.读取yml中的数据

 用Environment读取数据(注意自动装配):

package com.yrh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/books")
public class BookController {

    @Value("${lesson}")
    private String lesson;

    @Value("${enterprise.subject[0]}")
    private String subject00;

    @Autowired
    private Environment environment;
    @GetMapping("/{id}")
    public String getById(Integer id) {
        //System.out.println("id is " + id);
        System.out.println(lesson);
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("enterprise.subject[0]"));
        return "hello wang wei";
    }

}

 3.通过Enterprise类读取数据(常用)

1.定义Enterprise类

@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private int age;
    public String tel;
    public String[] subject;

    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;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String[] getSubject() {
        return subject;
    }

    public void setSubject(String[] subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        return "Enterprise{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", subject=" + Arrays.toString(subject) +
                '}';
    }
}

2.自动装配

@RestController
@RequestMapping("/books")
public class BookController {

    @Value("${lesson}")
    private String lesson;

    @Value("${enterprise.subject[0]}")
    private String subject00;

    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(Integer id) {
        //System.out.println("id is " + id);
        System.out.println(enterprise);

        return "hello wang wei";
    }

}

3.springboot整合Mybatis

 事先定义好Account实体类,和数据库表account

@Mapper
public interface AccountDao {

    @Select("select * from account where id = #{id}")
    public Account getById(int i);
}

在测试类中调用getById 

@SpringBootTest
class Springboot01demoApplicationTests {

	@Autowired
	private AccountDao account;


	@Test
	void contextLoads() {
		Account account1 = account.getById(1);
		System.out.println(account1);
	}

}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴能为您解答关于快速开发之SpringBoot的问题。下面是对您的问题的回答: 1)Maven搭建 Maven是一款Java项目的自动化构建工具,可以通过它来统一管理项目中的依赖库和构建过程,并可以自动进行打包、测试、发布等操作。 要使用Maven,首先需要安装Maven。可以在Maven官网(https://maven.apache.org/)下载Maven二进制文件,并按照官方文档中的说明进行安装和配置。安装完成后,就可以在命令行中使用mvn命令来执行Maven相关的操作了。 2)SpringBoot搭建 SpringBoot是一款基于Spring框架的快速开发框架,可以通过它来快速搭建Web应用、RESTful API等。要使用SpringBoot,需要在Maven的项目配置文件(pom.xml)中添加SpringBoot相关的依赖库,并编写SpringBoot的配置文件。 3)SpringBoot基本使用 使用SpringBoot可以快速搭建一个Web应用或RESTful API。要使用SpringBoot,需要在Java代码中添加注解来标识需要自动配置的组件,比如@Controller、@Service、@Repository等。此外,还需要在SpringBoot的配置文件中设置相关的配置选项,比如数据库连接、日志输出等。 4)完成AI智能平台的后台服务搭建 要完成AI智能平台的后台服务搭建,可以使用SpringBoot来快速搭建一个RESTful API。在Java代码中,可以使用SpringMVC来处理HTTP请求,并使用Spring Data JPA来访问数据库。在SpringBoot的配置文件中,可以设置相关的数据库连接、日志输出等选项。同时,可以使用Spring Security来进行用户认证和授权,以保证系统的安全性。 希望我的回答能对您有所帮助。如果您还有其他问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值