小言_互联网的博客

Spring Boot知识点复习

330人阅读  评论(0)

知识点复习: 

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


  
  1. 依赖:
  2. <dependency>
  3. <groupId>org.apache.poi </groupId>
  4. <artifactId>poi </artifactId>
  5. <version>4.1.2 </version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.poi </groupId>
  9. <artifactId>poi-ooxml </artifactId>
  10. <version>4.1.2 </version>
  11. </dependency>

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


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

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

MySQL:

T_EMP表


  
  1. CREATE TABLE `T_EMP` (
  2. `empno` int( 11) NOT NULL,
  3. `ename` varchar( 765) COLLATE utf8_bin DEFAULT NULL,
  4. `mgr` int( 11) DEFAULT NULL,
  5. `hiredate` datetime DEFAULT NULL,
  6. `job` varchar( 150) COLLATE utf8_bin DEFAULT NULL,
  7. `sal` float DEFAULT NULL,
  8. `comm` float DEFAULT NULL,
  9. `deptno` int( 11) DEFAULT NULL,
  10. PRIMARY KEY ( `empno`),
  11. KEY `hiredate_idx` ( `hiredate`),
  12. KEY `hiredate_name_idx` ( `hiredate`, `ename`),
  13. KEY `name_hiredate_idx` ( `ename`, `hiredate`)
  14. ) ENGINE= InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

T_DEPT表 


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

 T_EMP表中的数据:


  
  1. 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);
  2. 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);
  3. 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);
  4. 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);
  5. 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);
  6. 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);
  7. 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);
  8. 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);
  9. 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);
  10. 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);
  11. 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);
  12. 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);
  13. 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);
  14. 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);
  15. 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);
  16. 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);
  17. 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);
  18. 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);
  19. 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);
  20. 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);
  21. 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);
  22. 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);
  23. 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);
  24. 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);
  25. 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);
  26. 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);
  27. 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);
  28. 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);
  29. 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);
  30. 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);
  31. 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);
  32. 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);
  33. 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);
  34. 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);
  35. 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);
  36. 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);
  37. 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);
  38. 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);
  39. 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);
  40. 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);
  41. INSERT INTO `T_EMP` ( `empno`, `ename`, `mgr`, `hiredate`, `job`, `sal`, `comm`, `deptno`) VALUES ( 45, '小丽丽', NULL, '2019-09-16 00:00:00', '销售经理', 10000, 30, 2);
  42. 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);
  43. 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);
  44. 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);
  45. 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);
  46. 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);
  47. 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);
  48. 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);
  49. 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);
  50. 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);
  51. INSERT INTO `T_EMP` ( `empno`, `ename`, `mgr`, `hiredate`, `job`, `sal`, `comm`, `deptno`) VALUES ( 125, '吴昊', 1, '2020-03-12 00:00:00', '测试主管', 12000, 1000, 4);
  52. 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);
  53. 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);
  54. 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);
  55. 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);
  56. 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);
  57. 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);
  58. INSERT INTO `T_EMP` ( `empno`, `ename`, `mgr`, `hiredate`, `job`, `sal`, `comm`, `deptno`) VALUES ( 667, '谢文彬', NULL, NULL, 'Java攻城狮', 10000, 1, 1);
  59. INSERT INTO `T_EMP` ( `empno`, `ename`, `mgr`, `hiredate`, `job`, `sal`, `comm`, `deptno`) VALUES ( 669, '陈鹏', NULL, '2020-04-14 00:00:00', '程序员', 9999, NULL, 1);
  60. 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);
  61. 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);
  62. 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);
  63. 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);
  64. INSERT INTO `T_EMP` ( `empno`, `ename`, `mgr`, `hiredate`, `job`, `sal`, `comm`, `deptno`) VALUES ( 7778, '陈宇', NULL, NULL, NULL, 9999, NULL, 1);
  65. INSERT INTO `T_EMP` ( `empno`, `ename`, `mgr`, `hiredate`, `job`, `sal`, `comm`, `deptno`) VALUES ( 7779, '陈宇2', NULL, NULL, NULL, 8999, NULL, 1);
  66. 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);
  67. 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表 里的数据 


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

 


工程目录:


application.properties(配置信息)


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

 


Dept.java


  
  1. package com.example.demo.pojo;
  2. import java.io.Serializable;
  3. public class Dept implements Serializable {
  4. private Integer deptno;
  5. private String dname;
  6. private String loc;
  7. public Dept() {
  8. }
  9. public Dept(Integer deptno, String dname, String loc) {
  10. this.deptno = deptno;
  11. this.dname = dname;
  12. this.loc = loc;
  13. }
  14. public Integer getDeptno() {
  15. return deptno;
  16. }
  17. public void setDeptno(Integer deptno) {
  18. this.deptno = deptno;
  19. }
  20. public String getDname() {
  21. return dname;
  22. }
  23. public void setDname(String dname) {
  24. this.dname = dname;
  25. }
  26. public String getLoc() {
  27. return loc;
  28. }
  29. public void setLoc(String loc) {
  30. this.loc = loc;
  31. }
  32. }

Emp.java


  
  1. package com.example.demo.pojo;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import java.io.Serializable;
  4. import java.util.Date;
  5. public class Emp implements Serializable {
  6. private Integer empno;
  7. private String ename;
  8. @JsonFormat(pattern = "yyyy-MM-dd")
  9. private Date hiredate;
  10. private Dept dept;
  11. public Integer getEmpno() {
  12. return empno;
  13. }
  14. public void setEmpno(Integer empno) {
  15. this.empno = empno;
  16. }
  17. public String getEname() {
  18. return ename;
  19. }
  20. public void setEname(String ename) {
  21. this.ename = ename;
  22. }
  23. public Date getHiredate() {
  24. return hiredate;
  25. }
  26. public void setHiredate(Date hiredate) {
  27. this.hiredate = hiredate;
  28. }
  29. public Dept getDept() {
  30. return dept;
  31. }
  32. public void setDept(Dept dept) {
  33. this.dept = dept;
  34. }
  35. }

ErrorInfo.java


  
  1. package com.example.demo.pojo;
  2. public class ErrorInfo<T> {
  3. private Integer code;
  4. private String info;
  5. private T data;
  6. public Integer getCode() {
  7. return code;
  8. }
  9. public void setCode(Integer code) {
  10. this.code = code;
  11. }
  12. public String getInfo() {
  13. return info;
  14. }
  15. public void setInfo(String info) {
  16. this.info = info;
  17. }
  18. public T getData() {
  19. return data;
  20. }
  21. public void setData(T data) {
  22. this.data = data;
  23. }
  24. }

IEmpMapper.java


  
  1. package com.example.demo.mapper;
  2. import com.example.demo.pojo.Emp;
  3. import org.apache.ibatis.annotations.Result;
  4. import org.apache.ibatis.annotations.Results;
  5. import org.apache.ibatis.annotations.Select;
  6. import java.util.List;
  7. public interface IEmpMapper {
  8. @Select({
  9. "SELECT EMPNO,ENAME,HIREDATE,b.DEPTNO,b.DNAME,LOC",
  10. "FROM T_EMP a LEFT JOIN T_DEPT b",
  11. "ON a.DEPTNO=b.DEPTNO",
  12. "ORDER BY b.DEPTNO ASC"
  13. })
  14. @Results({
  15. @Result(property = "empno", column = "EMPNO"),
  16. @Result(property = "ename", column = "ENAME"),
  17. @Result(property = "hiredate", column = "HIREDATE"),
  18. @Result(property = "dept.deptno", column = "DEPTNO"),
  19. @Result(property = "dept.dname", column = "DNAME"),
  20. @Result(property = "dept.loc", column = "LOC")
  21. })
  22. public List<Emp> query();
  23. }

IEmpService.java


  
  1. package com.example.demo.service;
  2. import com.example.demo.pojo.Emp;
  3. import java.util.List;
  4. public interface IEmpService {
  5. /**
  6. * 得到所有员工信息
  7. * @return
  8. */
  9. public List<Emp> query();
  10. }

EmpServiceImpl.java


  
  1. package com.example.demo.service.impl;
  2. import com.example.demo.mapper.IEmpMapper;
  3. import com.example.demo.pojo.Emp;
  4. import com.example.demo.service.IEmpService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. /**
  9. * @author QuanLijian
  10. */
  11. @Service( "empService")
  12. public class EmpServiceImpl implements IEmpService {
  13. //需要将DAO注入至服务层
  14. @Autowired
  15. private IEmpMapper empMapper;
  16. /**
  17. * 得到所有员工信息
  18. *
  19. * @return
  20. */
  21. @Override
  22. public List<Emp> query() {
  23. return empMapper.query();
  24. }
  25. }

GlobalException.java


  
  1. package com.example.demo.excepetion;
  2. import com.example.demo.pojo.ErrorInfo;
  3. import org.springframework.web.bind.annotation.ExceptionHandler;
  4. import org.springframework.web.bind.annotation.RestControllerAdvice;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @RestControllerAdvice
  8. public class GlobalException {
  9. /**
  10. * 用于处理异常
  11. * @return
  12. */
  13. @ExceptionHandler(Exception.class) //标记用于处理异常的方法
  14. public ErrorInfo<?> doException(Exception e){
  15. ErrorInfo<Map<String,String>> info= new ErrorInfo<>();
  16. info.setCode( 500);
  17. info.setInfo(e.getMessage());
  18. Map<String,String> map= new HashMap<>();
  19. map.put( "uid", "zhangsan");
  20. map.put( "realname", "张三");
  21. map.put( "sex", "F");
  22. info.setData(map);
  23. return info;
  24. }
  25. }

EmpController.java


  
  1. package com.example.demo.controller;
  2. import com.example.demo.pojo.Emp;
  3. import com.example.demo.service.IEmpService;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.web.bind.annotation.*;
  7. import javax.annotation.Resource;
  8. import java.util.List;
  9. @RestController //@Controller+@ResponseBody 组合
  10. @RequestMapping( "/api")
  11. public class EmpController {
  12. //注入服务层
  13. @Resource(name = "empService")
  14. private IEmpService empService;
  15. //直接返回数据
  16. @GetMapping( "/emp")
  17. public ResponseEntity<?> query() {
  18. //调用服务层获得数据
  19. List<Emp> emps = empService.query();
  20. ResponseEntity<?> resp = new ResponseEntity<>(emps, HttpStatus.OK);
  21. return resp;
  22. }
  23. @GetMapping( "/emp/{empno}")
  24. public ResponseEntity<?> queryById( @PathVariable( "empno") Integer empno){
  25. Emp emp = null;
  26. ResponseEntity<?> resp = new ResponseEntity<>(emp, HttpStatus.OK);
  27. return resp;
  28. }
  29. }

DemoApplication.java(别忘记@MapperScan)


  
  1. package com.example.demo;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan( "com.example.**.mapper")
  7. public class DemoApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DemoApplication.class, args);
  10. }
  11. }

程序运行,浏览器打开:


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

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


转载:https://blog.csdn.net/weixin_44364444/article/details/105920325
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场