重生之我在地球Online当程序员31

SpringBoot入门程序
第一步:依赖
<!--
    SpringBoot快速构建应用的脚手架;脚手架:快速构建项目。
    使用SpringBoot可以轻松方便的与第三方框架集成。
    对于一个SpringBoot项目,需要集成一个父项目:spring-boot-starter-parent
    父项目:对常用的组件的版本进行统一管理;避免了jar包冲突。
-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.8.RELEASE</version>
</parent>
<dependencies>
    <!--添加web启动器: 集成了springmvc、内嵌了tomcat-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
第二步:启动类
/**
 * SpringBoot启动类
 */
@SpringBootApplication // 标识是一个SpringBoot启动类
public class CommunityApplication {
    public static void main(String[] args) {
        SpringApplication.run(CommunityApplication.class,args);
    }
}
第三步:控制器
/**
 * 楼栋管理
 */
@RestController
@RequestMapping("/building")
public class BuildingController {
    /**
     * http://localhost:8080/building/findById?id=100
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Building findById(Integer id) {
        System.out.println("id = " + id);
        return new Building(1,"1栋","1单元","无");
    }
}
入门程序分析
问题1:依赖管理

springboot项目,一定要继承一个父项目:spring-boot-starter-parent

项目关系:

image-20230228105417809

父项目中:dependencyManagement 表示版本锁定 理念: 父项目中统一控制各个组件的版本,子项目继承父项目不需要指定组件版本,只需要指定GroupId、ArtifactId即可; Version在父项目中统一指定

问题2:SpringBoot包扫描

默认扫描:启动类平级的包及其子包。如果扫描其他的包,需要通过@ComponentScan注解指定。需要注意的是,一旦使用了@ComponentScan扫描指定的包,就不会再扫描启动类平级的包了。

问题3:rest路径参数

@PathVariable注解: 获取rest请求中路径参数。

/**
 * 楼栋管理
 */
@RestController
@RequestMapping("/building")
public class BuildingController {
    /**
     * 请求地址:http://localhost:8080/building/100
     * 路径映射:@RequestMapping("/{id}")  这里的{id}相当于占位符
     * 方法参数:通过@PathVariable注解获取路径参数。
     *
     * rest路径写法:更简洁、更有层次感、更易于缓存。
     */
    @RequestMapping("/{id}")
    public Building findById(@PathVariable("id") Integer id) {
        System.out.println("id = " + id);
        return new Building(1,"1栋","1单元","无");
    }
}
SpringBoot集成MyBatis
步骤

1、依赖: mysql驱动、mybatis启动器、springboot对junit的支持包

2、application.yml 配置

3、mapper接口

4、service开发

5、controller调用service

6、测试

实现

1、依赖: mysql驱动、mybatis启动器、springboot对junit的支持包

<properties>
    <!--在这里统一定义各个组件的版本-->
    <mysql.version>5.1.49</mysql.version>
</properties>
<parent>
    <groupId>org.springframework.boot&l
  • 23
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值