• SpringBoot:入门简单使用


    目录

    一、SpringBoot的简介

    二、简单使用

    创建springboot项目

     使用Java代码

     使用springboot写增删改查

     优化:使用响应封装类

    一、SpringBoot的简介

    Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。
    也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具

     注意:

    注1:敏捷式开发

    注2:spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式, 就像maven整合了所有的jar包,spring boot整合了所有的框架

    注3:基于Spring框架的一站式解决方

    二、简单使用

    使用idea新建一个springboot的项目

    创建springboot项目

     

     

     

     

     到这里就新建成功了

    需要注意的是:在创建该项目时 勾选开发工具不能全勾 或者没有计划的配置却勾选 会报错

     使用Java代码

    测试一下 是否能用

    创建成功后的项目便有一个Spring01Application 类

    1. package com.cdl.spring01;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication()
    5. public class Spring01Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Spring01Application.class, args);
    8. }
    9. }

     自己写一个controller

    1. package com.cdl.spring01.demo;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. /**
    5. * @author cdl
    6. * @site www.cdl.com
    7. * @create 2022-10-28 19:23
    8. */
    9. //RestController = @responseBody + @controller
    10. @RestController
    11. public class IndexController {
    12. @RequestMapping("/")
    13. public String index(){
    14. System.out.println("com in");
    15. //index的字符串
    16. return "index";
    17. }
    18. }

     

     springboot有内置的tomcat

     能够正常使用

     修改配置

     再运行

     下载所需要的插件

     右键选中修改配置的那个文件 转换一下格式

     转换格式之后

     使用springboot写增删改查

    1. package com.cdl.spring01.demo;
    2. import jdk.nashorn.internal.objects.annotations.Constructor;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. import org.springframework.web.bind.annotation.DeleteMapping;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.PostMapping;
    8. import org.springframework.web.bind.annotation.PutMapping;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.web.bind.annotation.RestController;
    11. import java.util.Arrays;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. /**
    15. * @author cdl
    16. * @site www.cdl.com
    17. * @create 2022-10-28 19:56
    18. */
    19. @RestController
    20. @RequestMapping("/book")
    21. public class BookController {
    22. @RequestMapping("/list")
    23. // @GetMapping("list")
    24. public Map list(){
    25. Map map = new HashMap();
    26. map.put("data", Arrays.asList(
    27. new Book(1,"足球"),
    28. new Book(2,"篮球"),
    29. new Book(3,"排球"),
    30. new Book(4,"羽毛球"),
    31. new Book(5,"台球")
    32. ));
    33. map.put("total",168);
    34. map.put("msg","查询成功");
    35. map.put("code",200);
    36. return map;
    37. }
    38. @RequestMapping("/add")
    39. // @PutMapping("/add")
    40. public Map add(){
    41. Map map = new HashMap();
    42. map.put("msg","新增成功");
    43. map.put("code",200);
    44. return map;
    45. }
    46. @RequestMapping("/load")
    47. public Map load(){
    48. Map map = new HashMap();
    49. map.put("msg","加载成功");
    50. map.put("data",new Book(3,"排球"));
    51. map.put("code",200);
    52. return map;
    53. }
    54. @RequestMapping("/edit")
    55. // @PostMapping("/edit")
    56. public Map edit(){
    57. Map map = new HashMap();
    58. map.put("msg","修改成功");
    59. map.put("code",200);
    60. return map;
    61. }
    62. @RequestMapping("/delete")
    63. // @DeleteMapping("/delete")
    64. public Map delete(){
    65. Map map = new HashMap();
    66. map.put("msg","删除成功");
    67. map.put("code",200);
    68. return map;
    69. }
    70. }
    71. //构造器
    72. //@Constructor
    73. @AllArgsConstructor
    74. @Data
    75. class Book{
    76. private int id;
    77. private String name;
    78. }

    增加

     修改

     删除

     查询

     将方法注释换成

    1. package com.cdl.spring01.demo;
    2. import jdk.nashorn.internal.objects.annotations.Constructor;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. import org.springframework.web.bind.annotation.DeleteMapping;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.PostMapping;
    8. import org.springframework.web.bind.annotation.PutMapping;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.web.bind.annotation.RestController;
    11. import java.util.Arrays;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. /**
    15. * @author cdl
    16. * @site www.cdl.com
    17. * @create 2022-10-28 19:56
    18. */
    19. @RestController
    20. @RequestMapping("/book")
    21. public class BookController {
    22. @RequestMapping("/list")
    23. //@GetMapping("list")
    24. public Map list(){
    25. Map map = new HashMap();
    26. map.put("data", Arrays.asList(
    27. new Book(1,"足球"),
    28. new Book(2,"篮球"),
    29. new Book(3,"排球"),
    30. new Book(4,"羽毛球"),
    31. new Book(5,"台球")
    32. ));
    33. map.put("total",168);
    34. map.put("msg","查询成功");
    35. map.put("code",200);
    36. return map;
    37. }
    38. @RequestMapping("/add")
    39. // @PutMapping("/add")
    40. public Map add(){
    41. Map map = new HashMap();
    42. map.put("msg","新增成功");
    43. map.put("code",200);
    44. return map;
    45. }
    46. @RequestMapping("/load")
    47. public Map load(){
    48. Map map = new HashMap();
    49. map.put("msg","加载成功");
    50. map.put("data",new Book(3,"排球"));
    51. map.put("code",200);
    52. return map;
    53. }
    54. @RequestMapping("/edit")
    55. // @PostMapping("/edit")
    56. public Map edit(){
    57. Map map = new HashMap();
    58. map.put("msg","修改成功");
    59. map.put("code",200);
    60. return map;
    61. }
    62. // @RequestMapping("/delete")
    63. @DeleteMapping("/delete")
    64. public Map delete(){
    65. Map map = new HashMap();
    66. map.put("msg","删除成功");
    67. map.put("code",200);
    68. return map;
    69. }
    70. }
    71. //构造器
    72. //@Constructor
    73. @AllArgsConstructor
    74. @Data
    75. class Book{
    76. private int id;
    77. private String name;
    78. }

     

     优化:使用响应封装类

    新建一个result的包

    放入错误编码类

    1. package com.cdl.spring01.Result;
    2. public class CodeMsg {
    3. private int code;
    4. private String msg;
    5. //通用的错误码
    6. public static CodeMsg SUCCESS = new CodeMsg(200, "success");
    7. public static CodeMsg SERVER_ERROR = new CodeMsg(500, "服务端异常");
    8. public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
    9. public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法");
    10. public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500104, "访问太频繁!");
    11. //登录模块5002XX
    12. public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
    13. public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
    14. public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
    15. public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
    16. public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
    17. public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");
    18. //商品模块5003XX
    19. //订单模块5004XX
    20. public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在");
    21. //秒杀模块5005XX
    22. public static CodeMsg MIAO_SHA_OVER = new CodeMsg(500500, "商品已经秒杀完毕");
    23. public static CodeMsg REPEATE_MIAOSHA = new CodeMsg(500501, "不能重复秒杀");
    24. public static CodeMsg MIAOSHA_FAIL = new CodeMsg(500502, "秒杀失败");
    25. private CodeMsg() {
    26. }
    27. private CodeMsg(int code, String msg) {
    28. this.code = code;
    29. this.msg = msg;
    30. }
    31. public int getCode() {
    32. return code;
    33. }
    34. public void setCode(int code) {
    35. this.code = code;
    36. }
    37. public String getMsg() {
    38. return msg;
    39. }
    40. public void setMsg(String msg) {
    41. this.msg = msg;
    42. }
    43. public CodeMsg fillArgs(Object... args) {
    44. int code = this.code;
    45. String message = String.format(this.msg, args);
    46. return new CodeMsg(code, message);
    47. }
    48. @Override
    49. public String toString() {
    50. return "CodeMsg [code=" + code + ", msg=" + msg + "]";
    51. }
    52. }

    统一响应类

    1. package com.cdl.spring01.Result;
    2. public class Result {
    3. private int code;
    4. private String msg;
    5. //因为返回的数据不知道是什么类型,所以定义一个泛型
    6. private T data;
    7. private long total;
    8. public long getTotal() {
    9. return total;
    10. }
    11. public void setTotal(long total) {
    12. this.total = total;
    13. }
    14. /**
    15. * 成功的时候调用
    16. */
    17. public static Result ok(T data) {
    18. return new Result(data);
    19. }
    20. public static Result ok(int code,String msg) {
    21. return new Result<>(code,msg);
    22. }
    23. public static Result ok(int code,String msg,T data) {
    24. Result success = ok(data);
    25. success.setCode(code);
    26. success.setMsg(msg);
    27. return success;
    28. }
    29. public static Result ok(int code,String msg,T data,long total) {
    30. Result success = ok(code,msg,data);
    31. success.setTotal(total);
    32. return success;
    33. }
    34. /**
    35. * 失败的时候调用
    36. */
    37. public static Result error(CodeMsg codeMsg) {
    38. return new Result(codeMsg);
    39. }
    40. private Result(T data) {
    41. this.data = data;
    42. }
    43. private Result(int code, String msg) {
    44. this.code = code;
    45. this.msg = msg;
    46. }
    47. private Result(CodeMsg codeMsg) {
    48. if (codeMsg != null) {
    49. this.code = codeMsg.getCode();
    50. this.msg = codeMsg.getMsg();
    51. }
    52. }
    53. public int getCode() {
    54. return code;
    55. }
    56. public void setCode(int code) {
    57. this.code = code;
    58. }
    59. public String getMsg() {
    60. return msg;
    61. }
    62. public void setMsg(String msg) {
    63. this.msg = msg;
    64. }
    65. public T getData() {
    66. return data;
    67. }
    68. public void setData(T data) {
    69. this.data = data;
    70. }
    71. }

    controller

    1. package com.cdl.spring01.demo;
    2. import com.cdl.spring01.Result.CodeMsg;
    3. import com.cdl.spring01.Result.Result;
    4. import lombok.AllArgsConstructor;
    5. import lombok.Data;
    6. import org.springframework.web.bind.annotation.DeleteMapping;
    7. import org.springframework.web.bind.annotation.GetMapping;
    8. import org.springframework.web.bind.annotation.PostMapping;
    9. import org.springframework.web.bind.annotation.PutMapping;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. import java.util.Arrays;
    13. import java.util.HashMap;
    14. import java.util.Map;
    15. /**
    16. * @author cdl
    17. * @site www.cdl.com
    18. * @create 2022-10-28 19:56
    19. */
    20. @RestController
    21. @RequestMapping("/book2")
    22. public class BookController02 {
    23. // @RequestMapping("/list")
    24. @GetMapping("list")
    25. public Result list(){
    26. return Result.ok(200,"查询成功",
    27. Arrays.asList(
    28. new Book(1,"足球"),
    29. new Book(2,"篮球"),
    30. new Book(3,"排球"),
    31. new Book(4,"羽毛球"),
    32. new Book(5,"台球")
    33. ),168);
    34. }
    35. // @RequestMapping("/add")
    36. @PutMapping("/add")
    37. public Result add(){
    38. return Result.ok(200,"新增成功");
    39. }
    40. @RequestMapping("/load")
    41. public Result load(){
    42. return Result.ok(200,"加载成功",new Book(3,"排球"));
    43. }
    44. // @RequestMapping("/edit")
    45. @PostMapping("/edit")
    46. public Result edit(){
    47. return Result.ok(200,"修改成功");
    48. }
    49. // @RequestMapping("/delete")
    50. @DeleteMapping("/delete")
    51. public Result delete(){
    52. return Result.ok(200,"删除成功");
    53. }
    54. @RequestMapping("/queryByUser")
    55. public Result queryByUser(){
    56. return Result.error(CodeMsg.BIND_ERROR.fillArgs("密码等级太低"));
    57. }
    58. }

    使用测试工具postman测试结果

    查询

     新增

     修改

     删除

     

  • 相关阅读:
    【距离注意残差网络:超分】
    【Proteus仿真】【STM32单片机】蔬菜大棚温湿度控制系统设计
    奥迪Q3电瓶损坏问题解决思路
    前段10W+字八股+半年实习经历+400道算法+两年学校创新创业团队开发也无法上岸的经历~之前端我不干了!
    晨控CK-GW08系列网关控制器与CODESYS软件MODBUSTCP通讯手册
    【资源信息获取方法】
    【UVM】我的 main_phase 都跑完了,为啥 case 无法退出?太不讲道理!
    游戏开发的魔法之笔:建造者设计模式的崭新艺术
    复数类。。
    从医疗保健攻击到HIPAA 合规性
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/127618679