初次做ssm项目有感(第二次)

经过大概一周的学习练习,已经基本掌握ssm框架在具体业务逻辑上的使用。虽然还不完全知道它内部真正的工作流程,但已经比以前进步很多。
前提ssm的配置以及get/set方法都已经建立完成
1.首先在jsp页面中编写自己想要的内容,通过action传到controller。
2.在controller中要编写一个方法(注解以及路径要和jsp中一致),用service的对象调用一个方法(还未建立)
3.然后去service层去声明刚才调用的方法,并在impl中去实现这个方法,返回值为DAO层对象调用方法本身。
4.在DAO中同样声明同名方法,但没有返回值
5.在DAO.xml中编写sql语言,实现自己需要的功能。
暂时总结到这里,后续随着学习的深入和理解的透彻会再次记录!!

  • Common:公共文件,用于存放配置类,工具类
  • Controller:控制层,处理用户输入请求
  • Dao(-mapper接口):数据访问层,持久化层,提供数据表存取机制,主要是 ORM 框架实现以对象-关系数据库的映射,编写接口和sql查询的方法,无实现
  • Dao-xml:在resource或其他地方存储,是具体实现sql方法的地方
  • Bean:模型层,存放对象
  • Service:服务层,由表现层直接调用,用于处理事务。它处理逻辑上的业务,而不去考虑具体的实现,Dao拼接的逻辑
  • Service-impl:service的实现层
  • 工具类(utils)
  • Dto:数据传输对象,把要传送的东西封装到这个对象中去传输
  • Domain/Entity/DO/VO:实体类,在类中声明各个实体
  • 在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应于数据库中存储的数据(数据表),DTO对应于除二者之外需要进行传递的数据
  • controller调service,service调dao
  • 在这里插入图片描述
//主函数
package com.miaosha_1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Miaosha1Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Miaosha1Application.class, args);
    }
}

//controller
package com.miaosha_1.controller;

import com.miaosha_1.result.CodeMsg;
import com.miaosha_1.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class DemoController {
    @ResponseBody
    @RequestMapping("/hello")
    public Result<String> hello() {
        return Result.success("hello");
    }

    @ResponseBody
    @RequestMapping("/helloerror")
    Result<Object> helloError() {
        return Result.error(CodeMsg.SERVER_ERROR);
    }
}

package com.miaosha_1.result;
import lombok.Data;
//不知道data是什么,所以用了泛型T
@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;
    public Result(CodeMsg cm) {
        if (cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.msg = cm.getMsg();
    }
    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }
    public static <T> Result<T> error(CodeMsg cm) {
        return new Result<T>(cm);
    }
    private Result(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }
}

//result包,专门放结果
package com.miaosha_1.result;

import lombok.Data;

@Data
public class CodeMsg {
    private int code;
    private String msg;
    public static CodeMsg SUCCESS = new CodeMsg(0, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "server error");

    public CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}

逻辑:运行主函数后,访问http://localhost:8080/demo/hello,主函数中的@SpringBootApplication会扫描同目录级别下的所有请求,不管是哪个包,只要有@RequestMapping注解,即可视为请求。

本项目中,根据访问路径后的/demo/hello,我们可定位到controller包中的DemoController类下的 hello()方法,此方法返回了Result类下的success()方法,并赋值hello;我们进入Result类,因为是外部调用,所以方法体是public,我们定位到了success(T data),此时data==hello,此方法返回了利用构造方法产生的new Result< T >(data)对象,而这个对象也就是controller方法中最后return的东西,而最后 @ResponseBody注解会将其转换为json数据,打印这个返回值到前端

@ResponseBody的作用其实是将java对象转为json格式的数据。
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

猜你喜欢

转载自blog.csdn.net/weixin_40485391/article/details/88952001