SpringBoot学习笔记

总体思路:先写dao,再写service

1.https://start.spring.io

生成对应的模板

2.写TestCotroller类,类上写@RestCotroller注解

3.TestCotroller类里写方法,方法上写@GetMapping("/方法名")注解

4.不一定要写GetMapping,具体看做什么操作

5.服务跑起来,试试效果

6.安装MySQL数据库,网上很多资料,可以参考https://www.cnblogs.com/zhangkanghui/p/9613844.html

7.建立测试库

CREATE DATABASE test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

8.建表

CREATE TABLE student( id int AUTO_INCREMENT PRIMARY KEY,name VARCHAR(50) NOT NULL,email VARCHAR(100) NOT NULL,age int );

9.返回IDEA,写dao层,dao层都是接口,负责与数据库的交互。加上注解@Restpository

注解作用:告诉SpringBoot框架,这是Repository层的代码。底层原理是依赖注入,将代码加入容器中,让框架能扫描到。

实现要继承JpaRepository,因为父类JpaRepository几乎包含了对数据库的增删查改。

10.在dao层中建一个student类,这个类用来映射数据库中的student表。

注解:

@Entity

@Table(name="student")

告诉SpringBoot框架这个类用来映射数据库中的student表。

注解:

@Id,告诉SpringBoot框架,它是一个自增的主键。

@Colum(name="age"),写在字段头上,告诉SpringBoot框架,java中的字段对应数据库中哪个字段。如果相同可以不写。

@GeneratedValue(strategy="IDENTITY"),写在字段头,告诉SpringBoot框架,id字段是数据库自增的字段。

如果不知道注解的参数,可以按住crtl键,鼠标左键进去看。

10.返回去写StudentRepository接口,给父类的泛类型添上值。

11.开始写service层

面向接口编程,先写接口,再写实现。

注解:@Service在实现层,让SpringBoot框架来注入。

@Override:告诉SpringBoot的框架,这个方法是实现接口的。(自动带出)

声明StudentRepository接口,使用@AutoWired注入

@Autowired:告诉SpringBoot框架,StudentRepository接口被注入到实现类中。

12.开始写Controller层的代码。

注入@RestController,告诉SpringBoot框架这个是Controller层。

因为是查询,所以再注入@GetMapping。

@PathVariable:对应路径变量。

@Autowired代表:注入Service层的代码

总结,可以看到,咱们先写了dao层,然后写service层的时候,使用了这个注解@Autowired,这是把dao层的代码注入进Service层,然后Controller层也写了这个注解@Autowired,这就是Service层的代码又注入到Controller层。

13.配置数据库url

14.手工插数据进mysql数据库

15.启动项目,网页输出json

相关推荐

  1. SpringBoot学习笔记

    2024-06-10 13:36:01       33 阅读
  2. SpringBoot学习笔记

    2024-06-10 13:36:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-10 13:36:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-10 13:36:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-10 13:36:01       82 阅读
  4. Python语言-面向对象

    2024-06-10 13:36:01       91 阅读

热门阅读

  1. web前端微服务设计:深入剖析与实践

    2024-06-10 13:36:01       30 阅读
  2. spring和Mybatis的各种查询

    2024-06-10 13:36:01       28 阅读
  3. linux 触屏, 旋转后配置pen

    2024-06-10 13:36:01       38 阅读
  4. github的个人readme文件

    2024-06-10 13:36:01       35 阅读
  5. C#进阶高级语法之LINQ

    2024-06-10 13:36:01       32 阅读
  6. #11 提升效率:Stable Diffusion批处理技术

    2024-06-10 13:36:01       29 阅读
  7. 25-ARM-V7架构

    2024-06-10 13:36:01       28 阅读
  8. Spark MLlib 机器学习

    2024-06-10 13:36:01       25 阅读
  9. Jira的原理及应用详解(五)

    2024-06-10 13:36:01       30 阅读
  10. pytorch中的zero_grad()函数的含义和使用

    2024-06-10 13:36:01       29 阅读
  11. Ubuntu安装Protobuf

    2024-06-10 13:36:01       24 阅读
  12. 影子,介绍一下自己

    2024-06-10 13:36:01       25 阅读