imooc视频:https://www.imooc.com/video/16713
gitee:https://gitee.com/AbbYbz/imooc-springboot-starter

1 新建一个新的demo

https://start.spring.io/

  • controller层
  • @RequestMapping等价于url中的路径
  • maven中新增一个spring-boot-starter-web依赖
    1. @RestController
    2. public class HelloController {
    3. @RequestMapping("/hello")
    4. public Object hello(){
    5. return "hello springboot.";
    6. }
    7. }

2 springboot接口返回json

  • JsonIgnore/JsonFormat/JsonInclude 可以用于修饰返回的json
  • @RestController,等价于@Controller+@ResponseBody,表明返回json

3 资源属性配置-映射

  • @Configuration
  • @ConfigurationProperties(prefix=”com.springboot.opensource”) //配置中的前缀
  • @PropertySource(value=”classpath:resource.properties”)
  • @Autowired //实现自动装配

新增maven依赖

  1. <!--资源文件映射-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-configuration-processor</artifactId>
  5. <optional>true</optional>
  6. </dependency>

配置resource.properties

  1. com.springboot.opensource.name=imooc
  2. com.springboot.opensource.website=www.imooc.com
  3. com.springboot.opensource.language=java

Resource对象(声明关联配置文件)+Controller Autowired

pojo/Resource & HelloController &

资源文件配置server

修改application.properties
服务端相关配置:
SpringBoot - 图1
tomcat 相关配置:
SpringBoot - 图2

4 springboot整合模板引擎

  • freemarker
  • thymeleaf * 主要用这个

maven依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

资源属性配置

ThymeleafController + index.html & test.html

  1. #设定静态文件路径,js,css等
  2. spring.mvc.static-path-pattern=/static/**
  3. ############################################################
  4. # thymeleaf 静态资源配置
  5. ############################################################
  6. spring.thymeleaf.prefix=classpath:/templates/
  7. spring.thymeleaf.suffix=.html
  8. spring.thymeleaf.mode=HTML5
  9. spring.thymeleaf.encoding=UTF-8
  10. spring.thymeleaf.content-type=text/html
  11. # 关闭缓存, 即时刷新, 上线生产环境需要改为true
  12. spring.thymeleaf.cache=false
  13. ############################################################
  14. # 配置i18n 资源文件,供thymeleaf 读取
  15. # (调用资源文件属性时会用到)
  16. ############################################################
  17. spring.messages.basename=i18n/messages
  18. spring.messages.cache-seconds=3600
  19. spring.messages.encoding=UTF-8

5 异常处理

  • 普通页面异常
  • ajax异常 ```java package com.springboot.exception;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView;

import com.springboot.pojo.IMoocJSONResult;

/* 异常的助手类

*/ @ControllerAdvice //全局异常处理 public class IMoocExceptionHandler {

  1. public static final String IMOOC_ERROR_VIEW = "error";
  2. @ExceptionHandler(value = Exception.class) //指明异常处理类型
  3. public Object errorHandler(HttpServletRequest reqest,
  4. HttpServletResponse response, Exception e) throws Exception {
  5. e.printStackTrace();
  6. // 如果是ajax异常,返回报错信息
  7. // 如果是页面普通异常,返回至error页面
  8. if (isAjax(reqest)) {
  9. return IMoocJSONResult.errorException(e.getMessage());
  10. } else {
  11. ModelAndView mav = new ModelAndView();
  12. mav.addObject("exception", e);
  13. mav.addObject("url", reqest.getRequestURL());
  14. mav.setViewName(IMOOC_ERROR_VIEW);
  15. return mav; //返回错误页面
  16. }
  17. }
  18. /**
  19. *
  20. * @Title: IMoocExceptionHandler.java
  21. * @Package com.imooc.exception
  22. * @Description: 判断是否是ajax请求
  23. * Copyright: Copyright (c) 2017
  24. * Company:FURUIBOKE.SCIENCE.AND.TECHNOLOGY
  25. *
  26. * @author leechenxiang
  27. * @date 2017年12月3日 下午1:40:39
  28. * @version V1.0
  29. */
  30. public static boolean isAjax(HttpServletRequest httpRequest){
  31. return (httpRequest.getHeader("X-Requested-With") != null
  32. && "XMLHttpRequest"
  33. .equals( httpRequest.getHeader("X-Requested-With").toString()) );
  34. }

}

  1. <a name="3Sm0e"></a>
  2. ### 6 mybatis整合
  3. 方式1:xml √<br />方式2:实体类 x <br />方式3:注解形式 x<br />mybatis: [https://github.com/abel533/MyBatis-Spring-Boot](https://github.com/abel533/MyBatis-Spring-Boot)
  4. <a name="4j4X4"></a>
  5. #### maven依赖
  6. ```xml
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>druid</artifactId>
  10. <version>1.1.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>mysql</groupId>
  14. <artifactId>mysql-connector-java</artifactId>
  15. <version>5.1.41</version>
  16. </dependency>
  17. <!--mybatis-->
  18. <dependency>
  19. <groupId>org.mybatis.spring.boot</groupId>
  20. <artifactId>mybatis-spring-boot-starter</artifactId>
  21. <version>1.3.1</version>
  22. </dependency>
  23. <!--mapper-->
  24. <dependency>
  25. <groupId>tk.mybatis</groupId>
  26. <artifactId>mapper-spring-boot-starter</artifactId>
  27. <version>1.2.4</version>
  28. </dependency>
  29. <!--pagehelper-->
  30. <dependency>
  31. <groupId>com.github.pagehelper</groupId>
  32. <artifactId>pagehelper-spring-boot-starter</artifactId>
  33. <version>1.2.3</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.alibaba</groupId>
  37. <artifactId>druid-spring-boot-starter</artifactId>
  38. <version>1.1.9</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.mybatis.generator</groupId>
  42. <artifactId>mybatis-generator-core</artifactId>
  43. <version>1.3.2</version>
  44. <scope>compile</scope>
  45. <optional>true</optional>
  46. </dependency>

配置

image.png
image.png

逆向生成pojo&mapper

GeneratorDisplay
image.png
image.png

mybatis的CRUD功能

1 扫描 mybatis mapper 包路径image.png
2 service
image.png
3 controller(MyBatisCRUDController) 调用service
image.png

mybatis-pagehelper实现分页

image.png
image.png

自定义mapper实现

1 SysUserMapperCustom & SysUserMapperCustom.xml
image.png
2 controller调用
image.png

SpringBoot整合持久层事务

image.png
3 service声明事务
image.png
image.png

7 redis缓存

image.png
image.png
image.png

redis工具类

RedisOperator
image.png

8 定时任务task

image.png
image.png
image.png

9 异步执行程序

image.png
image.png
异步任务
image.png
调用异步任务
image.png

10 拦截器使用

image.png

拦截器配置

image.png

具体拦截器过程

继承HandlerInterceptor
image.png