Spring Boot知识点复习

   日期:2020-05-08     浏览:90    评论:0    
核心提示:知识点复习:1、IoC的具体应用装配Bean的注解: @Service @Component @Rejava

知识点复习: 

1、IoC的具体应用

 装配Bean的注解:
   @Service
   @Component
   @Repository
   @Controller
  
  @Bean : 用在方法上面,将方法的返回值装配成Bean

2、AOP的具体应用
3、MVC
4、RESTfull
5、DAO层

      MySQL、MyBatis
      NoSQL 

事务

 6、拦截器
 7、登录认证

    JWT

8、应用程序打包部署

    操作系统:Linux/Unix
    前后端分离部署:
    
         Webserver   App server
         nginx        tomcat
         HttpServer/nginx   WebSphere/Weblogic/JBoss
    
    部署手册

导excel第三方包:
1、 JXL 
2、POI

依赖:
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

注意:光理论是不够的,在此送大家十套2020最新Java架构实战教程+大厂面试题库,进裙: 783802103 裙文件自行获取一起交流进步哦!

为了让小伙伴更快对Spring Boot有一个系统的总结,下面以代码形式

所需的工具:idea+MySQL以及postman测试工具

MySQL:

T_EMP表

CREATE TABLE `T_EMP` (
  `empno` int(11) NOT NULL,
  `ename` varchar(765) COLLATE utf8_bin DEFAULT NULL,
  `mgr` int(11) DEFAULT NULL,
  `hiredate` datetime DEFAULT NULL,
  `job` varchar(150) COLLATE utf8_bin DEFAULT NULL,
  `sal` float DEFAULT NULL,
  `comm` float DEFAULT NULL,
  `deptno` int(11) DEFAULT NULL,
  PRIMARY KEY (`empno`),
  KEY `hiredate_idx` (`hiredate`),
  KEY `hiredate_name_idx` (`hiredate`,`ename`),
  KEY `name_hiredate_idx` (`ename`,`hiredate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

T_DEPT表 

CREATE TABLE `T_DEPT` (
  `deptno` int(11) DEFAULT NULL,
  `dname` varchar(765) COLLATE utf8_bin DEFAULT NULL,
  `loc` varchar(300) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

 T_EMP表中的数据:

INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (1,'王慧云',3,'2018-05-02 00:00:00','Java工程师',8000,200,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (2,'唐思远',NULL,'2020-03-12 00:00:00','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (3,'刘早',NULL,'2018-05-03 00:00:00','Java工程师',2506,200,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (4,'周敏',3,'2018-05-03 15:40:17','Java工程师',1500,10,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (5,'刘丽丽',3,'2018-10-10 00:00:00','Java工程师',9001,0,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (6,'汤灿',3,'2018-10-10 08:08:08','Java工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (7,'刘丽丽2',3,'2018-10-10 08:08:08','Java工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (8,'刘丽丽3',11,'2018-10-10 08:08:08','Java工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (9,'刘丽丽4',11,'2018-10-10 08:08:08','Java工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (10,'刘丽丽5',11,'2018-10-10 08:08:08','Java工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (11,'刘丽丽6',NULL,'2018-10-10 08:08:08','C++工程师',9000,NULL,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (12,'刘丽丽x',1,'2018-10-10 00:00:00','C++工程师',9000,0,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (13,'刘丽丽8',1,'2018-10-10 08:08:08','C++工程师',9000,NULL,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (14,'刘丽丽9',1,'2018-10-10 08:08:08','C++工程师',9000,NULL,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (15,'刘丽丽10',1,'2018-10-10 08:08:08','C++工程师',9000,NULL,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (17,'刘丽丽12',1,'2018-10-10 08:08:08','C++工程师',9000,NULL,4);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (18,'刘丽丽13',1,'2018-10-10 00:00:00','C++工程师',9000,3,4);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (19,'昌寒阳',NULL,'2020-03-05 00:00:00',NULL,NULL,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (20,'张三',NULL,'2018-10-10 00:00:00','C++工程师',10000,0,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (21,'刘丽丽16',NULL,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (22,'刘丽丽17',NULL,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (23,'刘丽丽18',NULL,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (24,'马方',NULL,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (25,'王圆',21,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (26,'谢文彬',21,'2018-10-10 00:00:00','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (27,'刘丽丽20',21,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (28,'刘丽丽21',21,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (29,'刘丽丽22',21,'2018-10-10 08:08:08','C++工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (31,'刘丽丽24',21,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (32,'刘丽丽25',21,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (33,'刘丽丽26',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (34,'刘丽丽27',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (35,'刘丽丽28',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (36,'刘丽丽28',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (37,'刘丽丽30',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (38,'刘丽丽31',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (39,'刘丽丽32',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (40,'刘丽丽33',NULL,'2018-10-10 08:08:08','Python工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (41,'刘丽丽x',NULL,'2018-10-10 08:08:08','Java工程师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (43,'jack',NULL,'2018-10-10 08:08:08','Java讲师',9000,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (45,'小丽丽',NULL,'2019-09-16 00:00:00','销售经理',10000,30,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (90,'小崔',NULL,'2020-03-04 00:00:00',NULL,NULL,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (99,'强哥',NULL,'2019-09-26 00:00:00','Java工程师',7776,7788,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (100,'张六哥',NULL,'2019-12-26 00:00:00',NULL,NULL,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (101,'小五',NULL,'2019-12-17 00:00:00',NULL,NULL,NULL,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (120,'张芳',NULL,'2019-12-05 11:55:14',NULL,NULL,NULL,5);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (121,'强哥',NULL,'2019-10-10 00:00:00','C工程师',0,0,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (122,'尹俊华Y',NULL,'2019-10-10 00:00:00','Java工程师',4444,0,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (123,'尹俊华Y',NULL,'2019-10-10 00:00:00','Java工程师',0,0,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (124,'唐思远',1,'2020-03-12 00:00:00','Java工程师',10000,4000,5);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (125,'吴昊',1,'2020-03-12 00:00:00','测试主管',12000,1000,4);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (222,'付明强X',NULL,'1998-10-10 08:00:00',NULL,NULL,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (333,'Jordan',NULL,'2020-03-12 00:00:00',NULL,9000,NULL,5);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (334,'Jackson',NULL,'2020-03-12 00:00:00',NULL,9000,NULL,3);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (446,'寒阳X',NULL,'2020-04-16 00:00:00','Java工程师',9000,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (665,'邹鑫3',NULL,'2020-03-04 08:00:00',NULL,NULL,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (666,'何绪辉',NULL,'2020-03-12 00:00:00',NULL,9999,NULL,6);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (667,'谢文彬',NULL,NULL,'Java攻城狮',10000,1,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (669,'陈鹏',NULL,'2020-04-14 00:00:00','程序员',9999,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (777,'唐思远',NULL,'2020-03-17 00:00:00',NULL,4500,NULL,2);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (888,'邹鑫',NULL,'2020-03-23 10:59:07','Java工程师',5000,NULL,NULL);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (993,'兰翔',NULL,'2020-04-16 00:00:00','Java工程师',9990,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (7777,'Jams',NULL,'2020-03-25 10:00:46','前端工程师',NULL,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (7778,'陈宇',NULL,NULL,NULL,9999,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (7779,'陈宇2',NULL,NULL,NULL,8999,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (7780,'陈宇3X',NULL,'2020-04-10 00:00:00','Java工程师',7999,NULL,1);
INSERT INTO `T_EMP` (`empno`,`ename`,`mgr`,`hiredate`,`job`,`sal`,`comm`,`deptno`) VALUES (7781,'天宇',NULL,'2020-04-08 00:00:00','Java工程师',10000,NULL,NULL);

 

T_DEPT表 里的数据 

INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (1,'开发一部','长沙');
INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (2,'开发二部','北京');
INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (3,'开发三部','深圳');
INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (4,'销售部','长沙');
INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (5,'推广部','长沙');
INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (6,'公关部','深圳');
INSERT INTO `T_DEPT` (`deptno`,`dname`,`loc`) VALUES (7,'后勤部','长沙');

 

工程目录:

application.properties(配置信息)

server.port=8080

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
#数据库名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#数据库引擎
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

 

Dept.java

package com.example.demo.pojo;

import java.io.Serializable;


public class Dept implements Serializable {

    private Integer deptno;
    private String dname;
    private String loc;

    public Dept() {
    }

    public Dept(Integer deptno, String dname, String loc) {
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

Emp.java

package com.example.demo.pojo;


import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.util.Date;


public class Emp implements Serializable {

    private Integer empno;
    private String ename;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date hiredate;

    private Dept  dept;

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}

ErrorInfo.java

package com.example.demo.pojo;

public class ErrorInfo<T> {

    private Integer code;
    private  String info;
    private  T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

IEmpMapper.java

package com.example.demo.mapper;


import com.example.demo.pojo.Emp;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;



public interface IEmpMapper {

    @Select({
            "SELECT EMPNO,ENAME,HIREDATE,b.DEPTNO,b.DNAME,LOC",
            "FROM T_EMP a LEFT JOIN T_DEPT b",
            "ON a.DEPTNO=b.DEPTNO",
            "ORDER BY b.DEPTNO ASC"
    })
    @Results({
            @Result(property = "empno", column = "EMPNO"),
            @Result(property = "ename", column = "ENAME"),
            @Result(property = "hiredate", column = "HIREDATE"),
            @Result(property = "dept.deptno", column = "DEPTNO"),
            @Result(property = "dept.dname", column = "DNAME"),
            @Result(property = "dept.loc", column = "LOC")
    })
    public List<Emp> query();
}

IEmpService.java

package com.example.demo.service;



import com.example.demo.pojo.Emp;

import java.util.List;


public interface IEmpService {

    
    public List<Emp> query();
}

EmpServiceImpl.java

package com.example.demo.service.impl;


import com.example.demo.mapper.IEmpMapper;
import com.example.demo.pojo.Emp;
import com.example.demo.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service("empService")
public class EmpServiceImpl implements IEmpService {

    //需要将DAO注入至服务层
    @Autowired
    private IEmpMapper empMapper;


    
    @Override
    public List<Emp> query() {
        return empMapper.query();
    }
}

GlobalException.java

package com.example.demo.excepetion;

import com.example.demo.pojo.ErrorInfo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalException {

    
    @ExceptionHandler(Exception.class) //标记用于处理异常的方法
    public ErrorInfo<?> doException(Exception e){
        ErrorInfo<Map<String,String>> info=new ErrorInfo<>();
        info.setCode(500);
        info.setInfo(e.getMessage());

        Map<String,String> map=new HashMap<>();
        map.put("uid","zhangsan");
        map.put("realname","张三");
        map.put("sex","F");
        info.setData(map);

        return info;
    }
}

EmpController.java

package com.example.demo.controller;


import com.example.demo.pojo.Emp;
import com.example.demo.service.IEmpService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController//@Controller+@ResponseBody 组合
@RequestMapping("/api")
public class EmpController {

    //注入服务层
    @Resource(name = "empService")
    private IEmpService empService;


    //直接返回数据
    @GetMapping("/emp")
    public ResponseEntity<?> query() {
        //调用服务层获得数据
        List<Emp> emps = empService.query();
        ResponseEntity<?> resp = new ResponseEntity<>(emps, HttpStatus.OK);
        return resp;
    }

    @GetMapping("/emp/{empno}")
    public ResponseEntity<?> queryById(@PathVariable("empno") Integer empno){

        Emp emp = null;
        ResponseEntity<?> resp = new ResponseEntity<>(emp, HttpStatus.OK);
        return resp;
    }


}

DemoApplication.java(别忘记@MapperScan)

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.**.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

程序运行,浏览器打开:

注意:最后送大家十套2020最新Java架构实战教程+大厂面试题库,进裙 783802103 裙文件自行获取一起交流进步哦! 

可以看到数据取到了,关于 Spring Boot知识点复习就到这,后续会说的Spring cloud,有问题的小伙伴,欢迎留言!!!

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服