美文网首页
SpringBoot02

SpringBoot02

作者: 建国同学 | 来源:发表于2020-06-01 14:35 被阅读0次

    复习

    @Component
    启动时,会加载贴了该注解的类
    instanceof:判断是否是该对象的实例

    一、集成freemarker

    • spring.freemarker.prefix:
      加载模板时候的前缀
      404:没添加依赖,前后缀

    • spring.freemarker.settings.number_format=0.##
      数字格式不用逗号隔开配置

    • 配置freemarker模板
      Enable_Live Templates选项,显示在new页面上

    二、 异常处理

    静态文件方式

    访问异常处理-静态页面方式
    1,SpringBoot默认情况下,把所有错误都重新定位到/error这个处理路径上,由BasicErrorController类完成处理;
    2,SpringBoot提供了默认的替换错误页面的路径:
    1,静态错误页面默认结构:

    src/
        resources/
        static/
            error/
                404.html
                401.html
                5xx.html
    

    2,也可以使用模板页面:

    src/
        resources/
        templates/
            error/
                5xx.ftl
    

    该路径方式是通过ErrorMvcAutoConfiguration中的DefaultErrorViewResolver完成的;

    统一异常处理方式

    统一异常处理使用增强控制器啦实现,级别比静态方式更高

    • @ControllerAdvice :
      controller 功能增强标签
      在请求进入映射方法之前,执行增强逻辑。经典用法:日期格式
      在请求进入映射方法之后,执行增强逻辑。经典用法:统一异常处理

    • @ExceptionHandler:
      异常处理标签,表示该方法能处理指定类型的异常

    三、集成druid

    • 导入druid依赖
    <!-- druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.21</version>
    </dependency>
    
    • 指定数据源
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql:///crmdemo?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    spring.datasource.username=root
    spring.datasource.password=admin
    

    四、集成mybaties

    • @SpringBootApplication
      springboot功能注解, springboot 所有的核心功能全部靠这个注解去实现

    • @MapperScan(basePackages="cn.wolfcode.crm.mapper")
      扫描mapper接口

    五、事务

    • 引入aop
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    
    • @Transactional
      //表示给服务层添加事务

    • @Transactional(readOnly = true)
      事务只读注解

    六、 拦截器

    1. 实现WebMvcConfigurer接口
    2. 重写addInterceptors方法
    3. // 如果是拦截的动态请求,handler参数就是HandlerMethod类实例
      // 如果是拦截是静态资源,handler参数就是不是HandLerMethod类实例

    七、

    八、

    九、

    十、

    相关文章

      网友评论

          本文标题:SpringBoot02

          本文链接:https://www.haomeiwen.com/subject/cqhlzhtx.html