Spring Boot 入门 需求一 使用 Spring Boot 开发 Web 应用,浏览器发起/hello请求,返回字符串”hello wordld”
步骤 
创建 Maven 工程 
导入spring-boot-stater-web 起步依赖 
编写 Controller 
提供启动类 
 
实现 创建项目
添加依赖
创建 Controller 包,HelloController 类
package  com.sakurasep.springboot.controller;import  org.springframework.web.bind.annotation.PostMapping;import  org.springframework.web.bind.annotation.RequestMapping;import  org.springframework.web.bind.annotation.RestController;@RestController public  class  HelloController  {    @RequestMapping("/hello")      public  String hello ()      {         return  "hello world" ;     } } 
 
运行启动类
 测试结果
需求二 认识配置文件 - application.yml
相比于 properties 更推荐 yml
server:   port:  9090    servlet:      context-path:  /start  
 
使用 @Value 注解获取配置文件键名
email: 	user:  sakurasep@qq.com  	auth:  true  
 
@Value("{email.user}") public  String user;@Value("{email.auth}") public  boolean  auth
 
使用 @ConfigurationProperties 注解获取配置文件
@ConfigurationProperties(prefix = "email") public  class  User  {    public  String user;     public  boolean  auth; } 
 
需求三 使用 Spring 整合 mybatis,实现web 访问 /findByid?id=1 将从数据库读取 id 为1的数据
编写 application.yml 配置文件
spring:   datasource:      driver-class-name:  com.mysql.cj.jdbc.Driver      username:  root      password:  123456      url:  jdbc:mysql://localhost:3306/tlias  
 
在 pom.xml 添加 mybatis 和 mysql 驱动依赖(添加完成后使用 Maven 构建一下)
<dependency >     <groupId > org.mybatis.spring.boot</groupId >      <artifactId > mybatis-spring-boot-starter</artifactId >      <version > 3.0.0</version >  </dependency > <dependency >     <groupId > com.mysql</groupId >      <artifactId > mysql-connector-j</artifactId >  </dependency > 
 
在 pojo 层下面创建 User 实体类
pojo层的用途 在Spring Boot项目中,通常会创建一个名为pojo(Plain Old Java Object)的软件包。POJO是指普通的Java对象,它们是没有任何特殊限制的简单Java对象,用于表示应用程序中的数据实体或值对象。这些对象通常只包含私有字段和公共访问器方法,没有特定的框架或库的依赖。
 
package  com.sakurasep.springboot.pojo;public  class  User  {    private  Integer id;     private  String username;     private  String password;     private  String name;     public  Integer getId ()  {         return  id;     }     public  void  setId (Integer id)  {         this .id = id;     }     public  String getUsername ()  {         return  username;     }     public  void  setUsername (String username)  {         this .username = username;     }     public  String getPassword ()  {         return  password;     }     public  void  setPassword (String password)  {         this .password = password;     }     public  String getName ()  {         return  name;     }     public  void  setName (String name)  {         this .name = name;     }     public  Integer getGender ()  {         return  gender;     }     public  void  setGender (Integer gender)  {         this .gender = gender;     }     private  Integer gender; } 
 
在 mapper 层下创建 UserMapper 接口,用于 Mybatis 实现对数据库的操作
mapper层的用途 在Spring Boot项目中,mapper包通常用于存放数据访问对象(DAO)的接口或类。这些接口或类通常用于定义与数据持久化相关的操作,如数据库的CRUD(创建、读取、更新、删除)操作。
 
package  com.sakurasep.springboot.mapper;import  com.sakurasep.springboot.pojo.User;import  org.apache.ibatis.annotations.Mapper;import  org.apache.ibatis.annotations.Select;@Mapper public  interface  UserMapper  {    @Select("select * from emp where id = #{id}")      public  User findByid (Integer id) ; } 
 
在 service 层 创建 UserService 接口 以及 具体的业务逻辑代码 UserServiceImpl 类
Service层的用途 在Spring Boot项目中,Service层通常是业务逻辑层,用于处理应用程序的业务逻辑。Service层在MVC(模型-视图-控制器)架构中位于控制器层和持久化层之间,它负责处理业务逻辑、协调不同的持久化操作,并提供一组服务供控制器层调用。
 
impl包的用途 在一个典型的Spring Boot项目中,Service层下通常会有一个名为impl(implementation的缩写)的包,它是Service层的具体实现。这个包中包含了实际执行业务逻辑的类。
 
package  com.sakurasep.springboot.service;import  com.sakurasep.springboot.pojo.User;public  interface  UserService  {    public  User findByid (Integer id) ; } 
 
package  com.sakurasep.springboot.service.impl;import  com.sakurasep.springboot.mapper.UserMapper;import  com.sakurasep.springboot.pojo.User;import  com.sakurasep.springboot.service.UserService;import  org.apache.ibatis.annotations.Select;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.stereotype.Service;@Service public  class  UserServiceImpl  implements  UserService  {    @Autowired      private  UserMapper userMapper;     public  User findByid (Integer id) {         return  userMapper.findByid(id);     } } 
 
在 Controller 层 编写 UserController 类
Controller层的用途 Controller是Spring Boot项目中的一个组成部分,负责处理客户端请求并将结果返回给客户端。它充当了应用程序的入口点,并与用户交互,通常用于接收HTTP请求、调用Service层处理业务逻辑,并将处理结果返回给客户端。
 
package  com.sakurasep.springboot.controller;import  com.sakurasep.springboot.mapper.UserMapper;import  com.sakurasep.springboot.pojo.User;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.web.bind.annotation.RequestMapping;import  org.springframework.web.bind.annotation.RestController;@RestController public  class  UserController  {    @Autowired      private  UserMapper userMapper;     @RequestMapping("/findByid")      public  User findByid (Integer id) {         return  userMapper.findByid(id);     } } 
 
测试结果