目录
Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。
也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具
注意:
注1:敏捷式开发
注2:spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式, 就像maven整合了所有的jar包,spring boot整合了所有的框架
注3:基于Spring框架的一站式解决方
使用idea新建一个springboot的项目






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

测试一下 是否能用
创建成功后的项目便有一个Spring01Application 类
- package com.cdl.spring01;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication()
- public class Spring01Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Spring01Application.class, args);
- }
-
- }
自己写一个controller
- package com.cdl.spring01.demo;
-
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-10-28 19:23
- */
- //RestController = @responseBody + @controller
- @RestController
- public class IndexController {
- @RequestMapping("/")
- public String index(){
- System.out.println("com in");
- //index的字符串
- return "index";
- }
-
-
-
-
-
- }
springboot有内置的tomcat

能够正常使用
修改配置

再运行

下载所需要的插件

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

转换格式之后
- package com.cdl.spring01.demo;
-
- import jdk.nashorn.internal.objects.annotations.Constructor;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-10-28 19:56
- */
- @RestController
- @RequestMapping("/book")
- public class BookController {
- @RequestMapping("/list")
- // @GetMapping("list")
- public Map list(){
- Map map = new HashMap();
- map.put("data", Arrays.asList(
- new Book(1,"足球"),
- new Book(2,"篮球"),
- new Book(3,"排球"),
- new Book(4,"羽毛球"),
- new Book(5,"台球")
- ));
- map.put("total",168);
- map.put("msg","查询成功");
- map.put("code",200);
- return map;
- }
-
- @RequestMapping("/add")
- // @PutMapping("/add")
- public Map add(){
- Map map = new HashMap();
- map.put("msg","新增成功");
- map.put("code",200);
- return map;
- }
-
- @RequestMapping("/load")
- public Map load(){
- Map map = new HashMap();
- map.put("msg","加载成功");
- map.put("data",new Book(3,"排球"));
- map.put("code",200);
- return map;
- }
-
-
- @RequestMapping("/edit")
- // @PostMapping("/edit")
- public Map edit(){
- Map map = new HashMap();
- map.put("msg","修改成功");
- map.put("code",200);
- return map;
- }
-
- @RequestMapping("/delete")
- // @DeleteMapping("/delete")
- public Map delete(){
- Map map = new HashMap();
- map.put("msg","删除成功");
- map.put("code",200);
- return map;
- }
- }
-
- //构造器
- //@Constructor
- @AllArgsConstructor
- @Data
- class Book{
- private int id;
- private String name;
-
- }
增加

修改

删除

查询

将方法注释换成
- package com.cdl.spring01.demo;
-
- import jdk.nashorn.internal.objects.annotations.Constructor;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-10-28 19:56
- */
- @RestController
- @RequestMapping("/book")
- public class BookController {
- @RequestMapping("/list")
- //@GetMapping("list")
- public Map list(){
- Map map = new HashMap();
- map.put("data", Arrays.asList(
- new Book(1,"足球"),
- new Book(2,"篮球"),
- new Book(3,"排球"),
- new Book(4,"羽毛球"),
- new Book(5,"台球")
- ));
- map.put("total",168);
- map.put("msg","查询成功");
- map.put("code",200);
- return map;
- }
-
- @RequestMapping("/add")
- // @PutMapping("/add")
- public Map add(){
- Map map = new HashMap();
- map.put("msg","新增成功");
- map.put("code",200);
- return map;
- }
-
- @RequestMapping("/load")
- public Map load(){
- Map map = new HashMap();
- map.put("msg","加载成功");
- map.put("data",new Book(3,"排球"));
- map.put("code",200);
- return map;
- }
-
-
- @RequestMapping("/edit")
- // @PostMapping("/edit")
- public Map edit(){
- Map map = new HashMap();
- map.put("msg","修改成功");
- map.put("code",200);
- return map;
- }
-
- // @RequestMapping("/delete")
- @DeleteMapping("/delete")
- public Map delete(){
- Map map = new HashMap();
- map.put("msg","删除成功");
- map.put("code",200);
- return map;
- }
- }
-
- //构造器
- //@Constructor
- @AllArgsConstructor
- @Data
- class Book{
- private int id;
- private String name;
-
- }

新建一个result的包
放入错误编码类
- package com.cdl.spring01.Result;
-
- public class CodeMsg {
-
- private int code;
- private String msg;
-
- //通用的错误码
- public static CodeMsg SUCCESS = new CodeMsg(200, "success");
- public static CodeMsg SERVER_ERROR = new CodeMsg(500, "服务端异常");
- public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
- public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法");
- public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500104, "访问太频繁!");
- //登录模块5002XX
- public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
- public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
- public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
- public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
- public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
- public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");
-
- //商品模块5003XX
-
- //订单模块5004XX
- public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在");
-
- //秒杀模块5005XX
- public static CodeMsg MIAO_SHA_OVER = new CodeMsg(500500, "商品已经秒杀完毕");
- public static CodeMsg REPEATE_MIAOSHA = new CodeMsg(500501, "不能重复秒杀");
- public static CodeMsg MIAOSHA_FAIL = new CodeMsg(500502, "秒杀失败");
-
- private CodeMsg() {
- }
-
- private CodeMsg(int code, String msg) {
- this.code = code;
- this.msg = msg;
- }
-
- public int getCode() {
- return code;
- }
-
- public void setCode(int code) {
- this.code = code;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
- this.msg = msg;
- }
-
- public CodeMsg fillArgs(Object... args) {
- int code = this.code;
- String message = String.format(this.msg, args);
- return new CodeMsg(code, message);
- }
-
- @Override
- public String toString() {
- return "CodeMsg [code=" + code + ", msg=" + msg + "]";
- }
- }
统一响应类
- package com.cdl.spring01.Result;
-
- public class Result
{ -
- private int code;
- private String msg;
- //因为返回的数据不知道是什么类型,所以定义一个泛型
- private T data;
- private long total;
-
- public long getTotal() {
- return total;
- }
-
- public void setTotal(long total) {
- this.total = total;
- }
-
- /**
- * 成功的时候调用
- */
- public static
Result ok(T data) { - return new Result
(data); - }
- public static
Result ok(int code,String msg) { - return new Result<>(code,msg);
- }
- public static
Result ok(int code,String msg,T data) { - Result
success = ok(data); - success.setCode(code);
- success.setMsg(msg);
- return success;
- }
- public static
Result ok(int code,String msg,T data,long total) { - Result
success = ok(code,msg,data); - success.setTotal(total);
- return success;
- }
-
- /**
- * 失败的时候调用
- */
- public static
Result error(CodeMsg codeMsg) { - return new Result
(codeMsg); - }
-
- private Result(T data) {
- this.data = data;
- }
-
- private Result(int code, String msg) {
- this.code = code;
- this.msg = msg;
- }
-
- private Result(CodeMsg codeMsg) {
- if (codeMsg != null) {
- this.code = codeMsg.getCode();
- this.msg = codeMsg.getMsg();
- }
- }
-
- public int getCode() {
- return code;
- }
-
- public void setCode(int code) {
- this.code = code;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
- this.msg = msg;
- }
-
- public T getData() {
- return data;
- }
-
- public void setData(T data) {
- this.data = data;
- }
- }
controller
- package com.cdl.spring01.demo;
-
- import com.cdl.spring01.Result.CodeMsg;
- import com.cdl.spring01.Result.Result;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.PutMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-10-28 19:56
- */
- @RestController
- @RequestMapping("/book2")
- public class BookController02 {
- // @RequestMapping("/list")
- @GetMapping("list")
- public Result list(){
- return Result.ok(200,"查询成功",
- Arrays.asList(
- new Book(1,"足球"),
- new Book(2,"篮球"),
- new Book(3,"排球"),
- new Book(4,"羽毛球"),
- new Book(5,"台球")
- ),168);
- }
-
- // @RequestMapping("/add")
- @PutMapping("/add")
- public Result add(){
- return Result.ok(200,"新增成功");
- }
-
- @RequestMapping("/load")
- public Result load(){
- return Result.ok(200,"加载成功",new Book(3,"排球"));
- }
-
-
- // @RequestMapping("/edit")
- @PostMapping("/edit")
- public Result edit(){
- return Result.ok(200,"修改成功");
- }
-
- // @RequestMapping("/delete")
- @DeleteMapping("/delete")
- public Result delete(){
- return Result.ok(200,"删除成功");
- }
-
- @RequestMapping("/queryByUser")
- public Result queryByUser(){
- return Result.error(CodeMsg.BIND_ERROR.fillArgs("密码等级太低"));
- }
- }
-
-
使用测试工具postman测试结果
查询

新增

修改

删除
