JavaWeb后端(spring--boot)

B/S架构 :Browser/Server 浏览器/服务器架构模式。 客户只需浏览器,应用程序的逻辑和数据都存在服务器端。

C/S架构:Client/Server,客户端/服务器架构模式,需要单独开发维护客户端。

SpringBoot 入门:

java 复制代码
@RestController  //当前类是请求处理类
public class HelloController {
 @RequestMapping("/hello")
    public String hello(String name){
     System.out.println("name :" + name);
        return "hello " + name;
    }
}

运行Main方法就将web应用启动

登录 lcoalhost:8080

HTTP协议:超文本传输协议

1:基于TCP协议:面向连接,安全.

2:一次请求对应一次响应

3:无记忆能力,每次请求独立

状态码:

代码入门

java 复制代码
/**
 * 用户实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;
     private Integer age;
     private LocalDateTime updateTime;  
}
java 复制代码
package com.study.controller;

import cn.hutool.core.io.IoUtil;
import com.study.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户控制器类
 */
// ... existing code ...
@RestController
public class UserController {
    @RequestMapping("/list")
    public List<User> list() throws FileNotFoundException {


        InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
        // Add null check for input stream
        ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());





        //2.  解析对象 封装为User对象
        // Reuse DateTimeFormatter (create once outside stream)

        List<User> userList = lines.stream()
                .map(line -> {
                    String[] parts = line.split(",");
                    Integer id = Integer.parseInt(parts[0]);
                    String username = parts[1];
                    String password = parts[2];
                    String name = parts[3];
                    Integer age = Integer.parseInt(parts[4]);
                    return new User(id, username, password, name, age);
                }).toList();
        //3.返回数据(json)
        return userList;
    }
}

三层架构:

controller:控制层,接收前端发送的请求,对请求进行处理,并响应数据。

java 复制代码
@RestController
public class UserController {
    @Autowired
       private UserService userService;

       @RequestMapping("/list")
       public List<User> findall(){
           return userService.findall();
       }
}

service:业务逻辑层,处理具体的业务逻辑,

java 复制代码
@service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserDao userDao ;
    @Override
    public List<User> findall() {
        List<String> lines = userDao.findall();
        List<User> userList = lines.stream()
                .map(line -> {
                    String[] parts = line.split(",");
                    Integer id = Integer.parseInt(parts[0]);
                    String username = parts[1];
                    String password = parts[2];
                    String name = parts[3];
                    Integer age = Integer.parseInt(parts[4]);
                    return new User(id, username, password, name, age);
                }).toList();
        return userList;
    }
}

dao:数据访问层,负责数据访问操作,包括数据的增,删,改,查.(Repository)

java 复制代码
@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public List<String> findall() {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
        // Add null check for input stream
        ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());
        return lines;
    }
}

分层解耦:

高内聚 低耦合

耦合:衡量软件中各个层的依赖关系

内聚:软件中各个功能模块内部的功能联系

控制反转:IOC 对象的创建控制权由程序自身转移到容器

依赖注入:DI 容器为应用程序提供运行时,所依赖的资源,称之为依赖注入。

Bean对象:IOC容器中创建、管理的对象,称之为Bean。

1.将Dao 及 Service层的实现类,交给I0C容器管理 加注解@component

2.为Controller及 Service注入运行时所依赖的对象。 加注解AutoWired

多个bean

相关推荐
凡人叶枫12 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
极客先躯12 小时前
高级java每日一道面试题-2026年02月01日-实战篇[Docker]-Docker Volume 的生命周期管理是怎样的?
java·运维·docker·容器·持久化·架构图·容器卷
NE_STOP12 小时前
Raft算法处理细节
java
小小龙学IT12 小时前
Go 语言后端开发:从并发模型到生产落地的工程实践
开发语言·后端·golang
努力攻坚操作系统12 小时前
编程语言编译运行机制对比:C / Java / Python
java·c语言·python
慧一居士12 小时前
对比两个文件内容是否完全一致,java实现示例
java
ytttr87312 小时前
Qt 数字键盘实现
开发语言·qt
wearegogog12312 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
再写一行代码就下班12 小时前
Cursor配置Java环境、创建Spring Boot项目的步骤
java·开发语言·spring boot
零陵上将军_xdr12 小时前
后端转全栈学习-Day5-JavaScript 基础-3
开发语言·javascript·学习