Springboot-入门错误笔记记录

一、hello word,入门程序.

  1. 404 :将Controller中的注解 @controller 改为 @RestController

二、配置文件及小知识点

  1. yml中空格问题: 值的前面必须有空格,例8081前面必须有空格 port: 8081
  2. @ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not…":
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

**知识点:**当project-A 依赖project-B, project-B 依赖project-D时,当project-B的<optional>true</optional>时, project-A中如果没有显式的引入project-D, 则project-A不依赖project-D, 即project-A可以自己选择是否依赖project-D

3.properties文件中的汉字输出到控制台是乱码问题,yml没有问题,加入以后需要重新启动idea

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <!-- spring-boot:run 中文乱码 -->
    <configuration>
        <fork>true</fork>
        <!--增加jvm参数-->
        <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
    </configuration>
</plugin>

4.JSR303是什么:类似@Email判断是不是邮箱格式

5.@propertiesResource的null,使用时,@ConfigurationProperties不能删除,需要将文件中的内容注释
6.@ImportResouce在主配置文件(main)中使用,但是不常这么使用,而是添加组件(添加配置类,全注解方式)

7.多个profile文件指定:spring.profiles.active=dev #指定dev得properties配置

8.配置项目名称404:1.x版本的是server.context-path,2.X版本的是server.servlet.context-path

9.使用测试文件经常要解决依赖的问题:
Junit4
@Test默认是JUnit5,而是要是org.junit.test的, @RunWith 使用org.junit.runner.RunWith

Junit5需要导入的依赖

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.6.2</version>
    <scope>test</scope>
</dependency>

三、日志文件

1.导入logger错误:导入org.slf4j的包

2.配置中logging.file过时:使用logging.file.name=springboot.log , logging.file.path=路径

四、web相关

1.springBoot2.x过后static下的静态资源无法访问:
点击右侧maven打开后,点击第二个文件夹刷新,多点击几次就可以了,删除target文件重新生成

2.在这里面@RestController,无法识别html: @Controller @ResponseBody

3.自行编写配置器继承接口过时:新版的是替换为implements WebMvcConfigure。
(错误的是 extends WebMvcConfigurationSupport,继承完毕以后会出现静态资源不能访问的问题。 底层中@ConditionalOnMissingBean(WebMvcConfigurationSupport.class),SpringBoot做了这个限制, 只有当WebMvcConfigurationSupport类不存在的时候才会生效WebMvc自动化配置。)

4.修改访问时localhost:8080/默认页的方法(当使用thymeleaf组件时,retrun 才会找该文件下的文件,否则只会找其他的默认文件下的)

方法一:在controller中编写一个方法,return时返回的是resources/templates中的文件

@RequestMapping({"","/login.html"})
public String home(){
   return "login";
}

方法二:在implements WebMvcConfigure中的Myconfig中填写

@Override
 public void addViewControllers(ViewControllerRegistry registry) {
    //浏览器发送/springboot请求,来到成功页面
    registry.addViewController("/springboot").setViewName("success");
}

方法三:同上但是写法不同

@Bean
public WebMvcConfigurer webMvcConfigurer() {
    WebMvcConfigurer wmc = new WebMvcConfigurer() {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("login");
            registry.addViewController("/login.html").setViewName("login");
        }
    };
    return wmc;
}

5.??login.tip_zh_CN??问题解决:
首先要在applicationConfig.properties中配置上spring.messager.basename=包名.国际化文件名。
注意拼写问题

6.国际化遇到的问题:实现的接口错误应该是LocaleResolver不是LocaleContextResolver

7.拦截器问题:
拦截所有放行XX:registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/").excludePathPatterns("/","/asserts/");
只拦截:registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/main.html");

8.日期转换:在applicationConfig.properties中 spring.mvc.date-format=yyyy-mm-dd

9.开启配置HiddenHttpMethodFilter:1.5.X自动开启,2.2.X没有开启需要开启 spring.mvc.hiddenmethod.filter.enabled = true

10.@deteleMapping不能使用:同样是配置spring.mvc.hiddenmethod.filter.enabled=true
知识点: 浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter。

11.exception不能再前端显示:若没有进行重写方法,需要在配置文件中添加 server.error.include-exception=true
若是进行了重写方法,需要在自己的配置文件中重新调用

serverProperties.getError().isIncludeException()
public MyErrorAttributes(ServerProperties serverProperties) {
    super(serverProperties.getError().isIncludeException());
}

12.使用外置的Tomcat,创建war项目:设置根目录为 / ,不加项目名称目的是能加载上springboot的application.properties

五Java数据访问
1.使用配置文件创建数据库表:
一定要注意schema的配置是 -空格classpath:XXX.sql 冒号后面没有空格,并且需要加上spring-

datasource-initialization-mode: always
spring:
  datasource:
    initialization-mode: always
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    schema:
      - classpath:department.sql

2.使用Druid配置数据源,查看同级笔记

3.使用mybits的逆向工程时:
Failed to execute goal org.mybatis.springboot generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli)
解决:导入相应的<classPathEntry location="D:\server\maven\repository\mysql\mysql-connector-java\5.1.29\mysql-connector-java-5.1.29.jar" />相应的包,然后检查驱动driver是否正确,虽然在使用默认的数据源时使用的是com.mysql.cj.jdbc.Driver但是数据库是低版本的所以generator.xml中要符合版本要求

4.当数据库中的字段和实体类中名称不一致时:开启驼峰写法,并且会将字段中的下划线改为字母大写
方法一:自己编写一个配置文件

package com.springboot.config;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

方法二:在配置文件中开启

mybatis:
  configuration:
    map-underscore-to-camel-case: true

5.MapperScan(value = “com.springboot.dao(Mapper)”)批量扫描

6.insert和inserSelective的区别:insert是不管传过来的值有没有都执行,inserServletive是只给有值添加没有的是空会进行一个非空的判断

六、缓存

1.在applicationConfig.properties中添加配置使之sql进行debug:logging.level.com.springboot.dao = debug
2.缓存不起作用:
要在配置文件中开启 :spring.cache.type=redis 或者:SpringBootApplication中要加@EnableCaching注解,
缓存的对象必须实现Serializable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值