• SpringBoot 入门


    目录

    1.简介

            1.1 spring是什么

            1.2 特点

    2.入门

            2.1 创建springboot项目

            2.2 做配置

    3.响应式封装配置(案例)


    1.简介

            1.1 spring是什么

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

    同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),
    Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都
    只需要非常少量的配置代码,开发者能够更加专注于业务逻辑

            1.2 特点

    注1:敏捷式开发

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

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

    2.入门

            2.1 创建springboot项目

    #可以换源
    http://start.aliyun.com

    创建项目的时候有分国内源国外源 

     创建需要的项目

     注意勾选项目

    这样一个目录就创建好啦 

            2.2 做配置

    接下来我们做一些配置

    exclude = DataSourceAutoConfiguration.class

     可以输入相关的配置

     

     下载插件:

    右键导入改变:

    保留必要的配置

     写一个方法进行测试:

    当当当,好啦 

    3.响应式封装配置(案例)

    书类:

    1. package com.zking.spboot2.entity;
    2. import jdk.nashorn.internal.objects.annotations.Constructor;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. /**
    6. * @author ruojuan
    7. * @site www.ruojuan.com
    8. * @company 玉渊工作室
    9. * @create 2022年10月28日 15:06
    10. **/
    11. @AllArgsConstructor
    12. @Data
    13. public class Book {
    14. private int id;
    15. private String name;
    16. }

              案例1

    效果:

    增加:

    修改:

    删除:

    代码:

    1. package com.zking.spboot2.dome;
    2. import com.zking.spboot2.entity.Book;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.Arrays;
    6. import java.util.HashMap;
    7. import java.util.List;
    8. import java.util.Map;
    9. /**
    10. * @author ruojuan
    11. * @site www.ruojuan.com
    12. * @company 玉渊工作室
    13. * @create 2022年10月28日 15:05
    14. **/
    15. @RestController
    16. @RequestMapping("/book")
    17. public class BookController {
    18. //查询
    19. @RequestMapping("/list")
    20. public Map list(){
    21. //查询数据库
    22. List books = Arrays.asList(
    23. new Book(1,"aa"),
    24. new Book(2,"bb"),
    25. new Book(3,"cc"),
    26. new Book(4,"dd"));
    27. Map map = new HashMap();
    28. map.put("data",books);
    29. map.put("total",122);
    30. map.put("msg","查询成功");
    31. map.put("code",200);
    32. return map;
    33. }
    34. //增加
    35. @RequestMapping("/add")
    36. public Map add(){
    37. Map map = new HashMap();
    38. map.put("msg","增加成功");
    39. map.put("code",200);
    40. return map;
    41. }
    42. //修改
    43. @RequestMapping("/update")
    44. public Map update(){
    45. Map map = new HashMap();
    46. map.put("msg","修改成功");
    47. map.put("code",200);
    48. return map;
    49. }
    50. //删除
    51. @RequestMapping("/del")
    52. public Map del(){
    53. Map map = new HashMap();
    54. map.put("msg","删除成功");
    55. map.put("code",200);
    56. return map;
    57. }
    58. //查询单个
    59. @RequestMapping("/getAll")
    60. public Map getAll(){
    61. Map map = new HashMap();
    62. map.put("data",new Book(1,"aa"));
    63. map.put("msg","查询成功");
    64. map.put("code",200);
    65. return map;
    66. }
    67. }

              案例2

    查询是  GetMapping

    删除是  DeleteMapping

    修改是  PostMapping

    增加是  PutMapping

     下载一下Eolink

    安装步骤简单省略

    此时idea要处在运行状态哦

     代码:

    1. package com.zking.spboot2.dome;
    2. import com.zking.spboot2.entity.Book;
    3. import org.springframework.web.bind.annotation.*;
    4. import java.util.Arrays;
    5. import java.util.HashMap;
    6. import java.util.List;
    7. import java.util.Map;
    8. /**
    9. * @author ruojuan
    10. * @site www.ruojuan.com
    11. * @company 玉渊工作室
    12. * @create 2022年10月28日 15:05
    13. **/
    14. @RestController
    15. @RequestMapping("/book1")
    16. public class BookController1 {
    17. //查询
    18. @GetMapping("/list")
    19. public Map list(){
    20. //查询数据库
    21. List books = Arrays.asList(
    22. new Book(1,"aa"),
    23. new Book(2,"bb"),
    24. new Book(3,"cc"),
    25. new Book(4,"dd"));
    26. Map map = new HashMap();
    27. map.put("data",books);
    28. map.put("total",122);
    29. map.put("msg","查询成功");
    30. map.put("code",200);
    31. return map;
    32. }
    33. //增加
    34. @PutMapping("/add")
    35. public Map add(Book book){
    36. System.out.println(book);
    37. Map map = new HashMap();
    38. map.put("msg","增加成功");
    39. map.put("code",200);
    40. return map;
    41. }
    42. //修改
    43. @PostMapping("/update")
    44. public Map update(){
    45. Map map = new HashMap();
    46. map.put("msg","修改成功");
    47. map.put("code",200);
    48. return map;
    49. }
    50. //删除
    51. @DeleteMapping("/del")
    52. public Map del(int bid){
    53. System.out.println(bid);
    54. Map map = new HashMap();
    55. map.put("msg","删除成功");
    56. map.put("code",200);
    57. return map;
    58. }
    59. //查询单个
    60. @GetMapping("/getAll")
    61. public Map getAll(){
    62. Map map = new HashMap();
    63. map.put("data",new Book(1,"aa"));
    64. map.put("msg","查询成功");
    65. map.put("code",200);
    66. return map;
    67. }
    68. }

              案例3

    CodeMsg

    1. package com.zking.spboot2.dome;
    2. /**
    3. * @author ruojuan
    4. * @site www.ruojuan.com
    5. * @company 玉渊工作室
    6. * @create 2022年10月28日 15:47
    7. **/
    8. public class CodeMsg {
    9. private int code;
    10. private String msg;
    11. //通用的错误码
    12. public static CodeMsg SUCCESS = new CodeMsg(200, "success");
    13. public static CodeMsg SERVER_ERROR = new CodeMsg(500, "服务端异常");
    14. public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
    15. public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法");
    16. public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500104, "访问太频繁!");
    17. //登录模块5002XX
    18. public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
    19. public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
    20. public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
    21. public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
    22. public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
    23. public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");
    24. //商品模块5003XX
    25. //订单模块5004XX
    26. public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在");
    27. //秒杀模块5005XX
    28. public static CodeMsg MIAO_SHA_OVER = new CodeMsg(500500, "商品已经秒杀完毕");
    29. public static CodeMsg REPEATE_MIAOSHA = new CodeMsg(500501, "不能重复秒杀");
    30. public static CodeMsg MIAOSHA_FAIL = new CodeMsg(500502, "秒杀失败");
    31. private CodeMsg() {
    32. }
    33. private CodeMsg(int code, String msg) {
    34. this.code = code;
    35. this.msg = msg;
    36. }
    37. public int getCode() {
    38. return code;
    39. }
    40. public void setCode(int code) {
    41. this.code = code;
    42. }
    43. public String getMsg() {
    44. return msg;
    45. }
    46. public void setMsg(String msg) {
    47. this.msg = msg;
    48. }
    49. public CodeMsg fillArgs(Object... args) {
    50. int code = this.code;
    51. String message = String.format(this.msg, args);
    52. return new CodeMsg(code, message);
    53. }
    54. @Override
    55. public String toString() {
    56. return "CodeMsg [code=" + code + ", msg=" + msg + "]";
    57. }
    58. }
    Result
    
    1. package com.zking.spboot2.dome;
    2. /**
    3. * @author ruojuan
    4. * @site www.ruojuan.com
    5. * @company 玉渊工作室
    6. * @create 2022年10月28日 15:45
    7. **/
    8. public class Result {
    9. private int code;
    10. private String msg;
    11. //因为返回的数据不知道是什么类型,所以定义一个泛型
    12. private T data;
    13. private long total;
    14. public long getTotal() {
    15. return total;
    16. }
    17. public void setTotal(long total) {
    18. this.total = total;
    19. }
    20. /**
    21. * 成功的时候调用
    22. */
    23. public static Result ok(T data) {
    24. return new Result(data);
    25. }
    26. public static Result ok(int code,String msg) {
    27. return new Result<>(code,msg);
    28. }
    29. public static Result ok(int code,String msg,T data) {
    30. Result success = ok(data);
    31. success.setCode(code);
    32. success.setMsg(msg);
    33. return success;
    34. }
    35. public static Result ok(int code,String msg,T data,long total) {
    36. Result success = ok(code,msg,data);
    37. success.setTotal(total);
    38. return success;
    39. }
    40. /**
    41. * 失败的时候调用
    42. */
    43. public static Result error(CodeMsg codeMsg) {
    44. return new Result(codeMsg);
    45. }
    46. private Result(T data) {
    47. this.data = data;
    48. }
    49. private Result(int code, String msg) {
    50. this.code = code;
    51. this.msg = msg;
    52. }
    53. private Result(CodeMsg codeMsg) {
    54. if (codeMsg != null) {
    55. this.code = codeMsg.getCode();
    56. this.msg = codeMsg.getMsg();
    57. }
    58. }
    59. public int getCode() {
    60. return code;
    61. }
    62. public void setCode(int code) {
    63. this.code = code;
    64. }
    65. public String getMsg() {
    66. return msg;
    67. }
    68. public void setMsg(String msg) {
    69. this.msg = msg;
    70. }
    71. public T getData() {
    72. return data;
    73. }
    74. public void setData(T data) {
    75. this.data = data;
    76. }
    77. }
    BookController2
    1. package com.zking.spboot2.dome;
    2. import com.zking.spboot2.entity.Book;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RestController;
    5. import java.util.Arrays;
    6. import java.util.HashMap;
    7. import java.util.List;
    8. import java.util.Map;
    9. /**
    10. * @author ruojuan
    11. * @site www.ruojuan.com
    12. * @company 玉渊工作室
    13. * @create 2022年10月28日 15:05
    14. **/
    15. @RestController
    16. @RequestMapping("/book2")
    17. public class BookController2 {
    18. @RequestMapping("/list")
    19. public Result list(){
    20. return Result.ok(200,"查询成功",
    21. Arrays.asList(
    22. new Book(1,"aa"),
    23. new Book(2,"bb"),
    24. new Book(3,"cc"),
    25. new Book(4,"dd"),
    26. new Book(5,"ee")
    27. ),168);
    28. }
    29. @RequestMapping("/add")
    30. public Result add(){
    31. return Result.ok(200,"新增成功");
    32. }
    33. @RequestMapping("/load")
    34. public Result load(){
    35. return Result.ok(200,"加载成功",new Book(3,"排球"));
    36. }
    37. @RequestMapping("/edit")
    38. public Result edit(){
    39. return Result.ok(200,"修改成功");
    40. }
    41. @RequestMapping("/delete")
    42. public Result delete(){
    43. return Result.ok(200,"删除成功");
    44. }
    45. @RequestMapping("/queryByUser")
    46. public Result queryByUser(){
    47. return Result.error(CodeMsg.BIND_ERROR.fillArgs("密码等级太低"));
    48. }
    49. }

  • 相关阅读:
    德迅云安全专注网络安全,稳定、高防
    SVN一直报错Error running context: 由于目标计算机积极拒绝,无法连接。解决办法【杭州多测师_王sir】...
    【项目问题】Vue2和Vue3如何使用Pinia?数据持久化?防止storage被篡改?
    C# 串口通信(modbus),自动重连
    (亲测有效)推荐2024最新的免费漫画软件app,无广告,聚合全网资源!
    Mybatis-Plus时间范围查询
    线性规划的对偶理论
    MES管理系统与ERP系统的实施顺序与决策
    基于霍夫曼(Huffman)图像编码的图像压缩和重建-含Matlab代码
    找实习之从0开始的后端学习日记【9.19】
  • 原文地址:https://blog.csdn.net/weixin_67338832/article/details/127576541