搭建SpringBoot项目问题汇总(上)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 搭建SpringBoot项目问题汇总

搭建SpringBoot项目整合SSM框架问题汇总

1、java: 非法字符: '\ufeff'

在这里插入图片描述

解决方式:设置编码移除BOM

在这里插入图片描述

在这里插入图片描述

2、修改员工信息报错400

控制台报错
2022-09-12 15:49:39.658  WARN 18724 --- [nio-8080-exec-6]
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: 
org.springframework.validation.BeanPropertyBindingResult: 1 
errors<EOL>Field error in object 'employee' on field 'birth': 
rejected value [2022-10-12 15:49]; codes 
[typeMismatch.employee.birth,typeMismatch.birth,typeMismatch.java.
util.Date,typeMismatch]; arguments 
[org.springframework.context.support.DefaultMessageSourceResolvabl
e: codes [employee.birth,birth]; arguments []; default message 
[birth]]; default message [**Failed to convert property value of 
type 'java.lang.String' to required type 'java.util.Date' for 
property 'birth'**; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed 
to convert from type [java.lang.String] to type [java.util.Date] 
for value '2022-10-12 15:49'; nested exception is 
java.lang.IllegalArgumentException: Parse attempt failed for value 
[2022-10-12 15:49]]]

大概意思是:日期在转换时 格式错误 String类型无法转换成Util.Date类型

网页报错

在这里插入图片描述

解决问题
  • springboot默认日期格式为:2018-06-17T07:24:07.430+0000。
  • springboot在配置文件中可以修改日期格式

在这里插入图片描述

  • 日期格式转换出现问题,检查前端页面传来的日期格式和后端能接受日期格式

按照上图所示,后端配置的日期格式为:yyyy-MM-dd hh:mm

查看前端update页面设置的日期格式:yyyy-MM-dd HH:mm

在这里插入图片描述

由此可以发现 HH 和 hh格式不一样,才导致了错误

HH:24小时制
hh: 12小时制

  • 修改后端配置文件中的日期格式与前端保持一致即可解决问题
补充
  • 在实体类的字段上方加如下注解也可实现自定义日期格式
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")

在这里插入图片描述

3、配置了数据源,但是自动注入时还是爆红

原因:未知

解决方式:降低springboot版本

原版本2.7.3:

在这里插入图片描述

在这里插入图片描述

改为2.6.11:

在这里插入图片描述

在这里插入图片描述

4、Caused by: org.xml.sax.SAXParseException: 前言中不允许有内容。

报错原因:application.properties中没有配置mybatis.mapper-locations或者路径写错

解决方式:

  • 正确格式如下
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

5、Caused by: java.io.FileNotFoundException: class path resource [mybatis/mapper/*.xml] cannot be opened because it does not exist

  • 报错如下:

image-20220917235937764

  • 配置了mapper的配置文件路径但还是报错说文件不存在
注意:细节 locations 和 location 少了个 's'是不一样的
  • 正确的格式如下:
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

6、未找到mapper绑定的xml配置文件

  • 报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.springboot07mybatis.mapper.UserMapper.getUserList
  • 百度翻译:无效的绑定语句(未找到):com.springboot07mybatis.mapper.UserMapper.getUserList

在这里插入图片描述

查找错误根源:配置文件绑定地址错误 或者 mapper的namespace错误 或者 资源过滤问题
  • 检查配置文件绑定地址

在这里插入图片描述

我这里修改时粗心写错了才导致报错,正确格式如下:

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
  • 检查mapper的namespace

在这里插入图片描述

我这里是正确的

  • Maven过滤问题添加如下代码:
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

添加后运行检查输出路径 是否有xml文件在指定的路径中,如下图:

在这里插入图片描述

往往这几处没有问题了,基本就不会报错了。

7、数据源 url 的数据库在练习时也容易填错

  • 报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'springboot.user' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_292]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_292]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_292]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_292]
分析:练习时复制以前的写过的url地址,忘记更改数据库了

在这里插入图片描述

我这里应该是 mybatis 数据库,都是粗心啊兄弟们!!!

完整 mysql 5.1.47 url:

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8

ps:有的时候一个小错就会找半天,还找不到错误的原因,不过没关系,不要上头,要冷静分析,冷静不了就出去散散心,然后回来慢慢一点点的查找错误(心得呀都是,不回女朋友信息就是在嘎嘎修bug)

8、关于设计数据库表字段是否自动递增的问题

如果数据库 id字段没有设计自动递增,并且添加信息语句中也没有 id字段会怎样?

在这里插入图片描述
在这里插入图片描述

不出意外,肯定会报错,报错信息如下:

Cause: java.sql.SQLException: Field 'id' doesn't have a default value

; Field 'id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id' doesn't have a default value] with root cause

java.sql.SQLException: Field 'id' doesn't have a default value

在这里插入图片描述

两种解决方式
  • 第一种:设计自动递增(推荐)

原因:因为大多数时候我们让添加信息的业务都是用户填写相关信息,而id都是隐藏起来的不会让用户填写

  • 第二种:在添加信息的sql语句中添加id字段,可以自己练习的时候使用,开发项目就不建议了

原因:如上

9、扫描不到 yaml 配置文件

报错:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

在这里插入图片描述

原因:maven资源过滤问题

解决方式

添加如下代码:(注意区别,这里的资源过滤 添加了对yml /yaml格式文件的支持

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.yml</include>
                <include>**/*.yaml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.yml</include>
                <include>**/*.yaml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

当资源成功被扫描到后,在运行之后会在输出目录生成相应的文件,如下图:

在这里插入图片描述

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
Java 应用服务中间件 Maven
传统maven项目和现在spring boot项目的区别
Spring Boot:传统 Web 项目与采用 Spring Boot 项目区别
365 0
传统maven项目和现在spring boot项目的区别
|
XML Java 数据库连接
创建springboot项目的基本流程——以宠物类别为例
创建springboot项目的基本流程——以宠物类别为例
127 0
创建springboot项目的基本流程——以宠物类别为例
|
存储 机器学习/深度学习 IDE
SpringBoot 项目与被开发快速迁移|学习笔记
快速学习 SpringBoot 项目与被开发快速迁移
161 0
SpringBoot 项目与被开发快速迁移|学习笔记
|
安全 Java 关系型数据库
SpringSecurity与SpringBoot在集中式项目中整合步骤说明|学习笔记
快速学习SpringSecurity与SpringBoot在集中式项目中整合步骤说明
109 0
|
Java Spring
自定义SpringBoot项目的启动Banner
``Banner``是``SpringBoot``框架一个特色的部分,其设计的目的无非就是一个框架的标识,其中包含了版本号、框架名称等内容,既然``SpringBoot``为我们提供了这个模块,它肯定也是可以更换的这也是``Spring``开源框架的设计理念。
|
前端开发 Java 应用服务中间件
基于springboot+mybatisplus+vue-科技项目评审及专家库管理系统
基于springboot+mybatisplus+vue-科技项目评审及专家库管理系统
199 0
基于springboot+mybatisplus+vue-科技项目评审及专家库管理系统
|
Java Spring
【Java】【Spring Boot】CP01:创建一个SpringBoot项目(Spring Initializr)
【Java】【Spring Boot】CP01:创建一个SpringBoot项目(Spring Initializr)
215 0
【Java】【Spring Boot】CP01:创建一个SpringBoot项目(Spring Initializr)
|
SQL 弹性计算 前端开发
使用aliyunECS服务器+宝塔面板部署springboot后端项目并测试接口
在部署过程中遇到了很多问题,解决起来也是十分繁琐,这里写个笔记记录一下遇到的问题和思路 这里我先打算测试以下后端接口,前端代码还没有进行运行。 还没有购买域名,因为域名需要备案时间要一周所以暂时使用公网ip进行访问。
|
消息中间件 NoSQL Java
47K Star 的SpringBoot+MyBatis+docker电商项目,附超详细的文档
该项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现,采用Docker容器化部署。 前台商城系统:首页门户、商品推荐、商品搜索、商品展示、购物车、订单流程、会员中心、客户服务、帮助中心等模块。 后台管理系统:商品管理、订单管理、会员管理、促销管理、运营管理、内容管理、统计报表、财务管理、权限管理、设置等模块。 该项目使用现阶段主流技术实现。涵盖了SpringBoot 2.3.0、MyBatis 3.4.6、Elasticsearch 7.6.2、RabbitMQ 3.7.15、Redis 5.0、MongoDB 4.2.5、
|
安全 Java 关系型数据库
Mall电商实战项目全面升级!支持最新版SpringBoot,干掉循环依赖
技术栈升级 mall项目采用现阶主流技术实现,这些主流技术基本都升级了目前最新稳定版,具体升级内容大家可以参考下表。 技术版本说明