1.启动项目的时候报错

1.Error starting ApplicationContext.
To display the auto-configuration report re-run your application with 'debug' enabled.

解决方法:

在yml配置文件中加入debug: true,因为默认的话是false

2.在集成mybatis时mapper包中的类没被扫描

org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.app.mapper.UserMapper' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

解决方法:

在springboot的启动类中加入@MapperScan("mapper类的路径")
或者直接在Mapper类上面添加注解@Mapper,建议使用上面那种,不然每个mapper加个注解也挺麻烦的

3.在向数据库插入数据时报错

"\r\n### Error updating database.  Cause:
com.mysql.jdbc.MysqlDataTruncation:
Data truncation: Data too long for column 'password' at row 1\r\n###

数据库表password这个字段太短了,应该设长点

java.lang.ClassCastException:
com.app.entity.User cannot be cast to java.lang.Integer

4.用mybatis查询时报错

org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.binding.BindingException:
Parameter 'user_type' not found. Available parameters are [2, 1, 0, param1, param2, param3]
原因:@Param注解缺失,当只有一个参数时,Mapper接口中可以不使用
public User getUser(String name);

有多个参数时就必须使用

public User getUser(@Param("name") String name,@Param("password") String password);

5.Mybatis查询传入一个字符串传参数报错

mapper接口:
PkRecord findByPkStudentNumber(String pkStudentNumber);
对应的mapper配置文件
<select id="findByPkStudentNumber" resultMap="recordMap" >
SELECT * FROM pk_record
<where>
<if test="pkStudentNumber!=null">
pk_student_number=#{pkStudentNumber}
</if>
</where>
</select>

然后就会报如下错误

There is no getter for property named 'XXX' in 'class java.lang.String'
原因:

Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.num值,引起报错。

解决方法:

①在mapper配置文件中参数名,都要改成_parameter

<select id="findByPkStudentNumber" resultMap="recordMap" >
SELECT * FROM pk_record
<where>
<if test="_parameter!=null">
pk_student_number=#{_parameter}
</if>
</where>
</select>

②在mapper接口中用@Param在相关方法说明参数值

PkRecord findByPkStudentNumber(@Param("pkStudentNumber") String pkStudentNumber);

6.mybatis返回值报错

org.apache.ibatis.binding.BindingException:
Mapper method 'com.hoomsun.mybatis.dao.CostMapperDao.dongtaislq'
has an unsupported return type: class java.lang.String

dao接口类中对应的方法去掉返回值,用void,例如:

public void dongtaislq(Map map);

7.mybatis中集合与Stirng类型的比较

报错信息

invalid comparison: java.util.ArrayList and java.lang.String
原因:无法比较这两种类型
<if test="categoryIds!=null and categoryIds!=' ' ">
AND category_id IN
<foreach collection="categoryIds" item="categoryIds" open="(" separator="," close=")">
#{categoryIds}
</foreach>
</if>

在接收list的时候加了判断 list !=' ',引起了集合与Stirng类型的比较,所以报错,将判断条件改为 : list.size >0就可以了

<if test="categoryIds!=null and categoryIds.size>0" >
AND category_id IN
<foreach collection="categoryIds" item="categoryIds" open="(" separator="," close=")">
#{categoryIds}
</foreach>
</if>

8.保存对象数据进数据库后根据ID查询并返回该对象时为null

<insert id="saveUser" useGeneratedKeys="true" keyColumn="id" >
insert into user(username,password,nickname) values (#{username},#{password},#{nickname})
</insert>

这样写的话数据可以保存到数据库,没问题,ID也可以自动增长,不过保存后立刻根据ID查询时返回会为null
解决的方法是把keyColumn换成keyProperty就可以了

<insert id="saveUser" useGeneratedKeys="true" keyProperty="id" >
insert into user(username,password,nickname) values (#{username},#{password},#{nickname})
</insert>

9.idea运行项目时报错

//子容器启动失败
ERROR 8760 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase : A child container failed during start
//未能启动Tomcat组件
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException:
Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

这里的问题主要是jre环境没选好,可能是由于你之前项目要求改变jre,然后导致之前的项目jre环境也改变了。

idea具有内置tomcat,所以可以不用额外配置tomcat

在idea中点击运行→编辑结构→在配置中选择jre环境

我这里是选用1.8的环境

 
Paste_Image.png

再次启动项目:

 
Paste_Image.png

启动成功了

10.mybatis插入数据时默认值不生效

插入语句

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into mmall_category (id, name, status)
values (#{id}, #{name},#{status})
</insert>

对应的mapper

void insert(Category category);

需要传入的是一个对象,假如你在数据库设计时把status设置默认值
在传入对象时只赋值给name,结果你可以发现数据库中status的值是null

这是因为这个对象的其他属性成员你不赋值的话默认为null,并且你在sql语句中#{status},也就是把null赋给了status,但是有时候有需要传status,不能把#{status}去掉,那该怎么办呢?
解决方法:

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into mmall_category
(id, name
<if test="status != null">
,status
</if>)
values (#{id}, #{name}
<if test="status != null">
,#{status}
</if>)
</insert>

使用mybatis的if test进行值的判断,如果是null的话就不赋值

mybatis的高级结果映射

association – 一个复杂的类型关联;许多结果将包成这种类型
嵌入结果映射 – 结果映射自身的关联,或者参考一个

看起来挺难懂的,看下实例
在resultMap中,有这样的一个映射

<association property="user" column="user_id" select="com.mapper.UserMapper.selectByPrimaryKey"/>

当你用select查询出来对象时想获取userId的值要先获取映射的对象再获取其ID,不然直接获取userId会为空

11.InterlliJ Debug方式启动特别慢

Method breakpoints may dramatically slow down debugging

不管你是重启服务器和重启idea还是报这个问题。由该提示语我们可以知道要把方法断点给关掉,查看断点的快捷方式是Ctrl + Shift +F8

 
image.png

Java Method Breakpoints去掉即可

错误Caused by: java.lang.IllegalStateException: In the composition of all global method configuration, no annotation support was actually activated

原因:在所有全局方法配置的组合中,实际上没有激活注释支持

解决方法:

在启动类中加入@EnableGlobalMethodSecurity(securedEnabled = true)

@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

12.MyBatis绑定错误

Invalid bound statement (not found)

这个错误主要是因为mapper接口与mapper.xml的路径没有一一对应,并且mapper.xml不能放在src目录里,配置文件必须放resources里,src目录下的xml文件默认不会编译到target

13.使用请求转发或者重定向出现异常

java.lang.IllegalStateException: Cannot forward after response has been committed

原因:

报异常的原因是重复转发或者重定向了请求

解决方法:

如果有多个转发或者重定向,需要在每个转发或者重定向请求之后加上return语句(最后一个请求转发或者重定向可以不加)

14.SpringBoot配置数据库连接池,但日志却每次都新建连接

Mybatis中动态打印SQL语句到控制台,只需要在SpringBoot配置文件中添加如下配置即可
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

但是如果没有用到任何连接池的话,是不会打印的

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a5ca7d7] was not
registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9a51d74] will not be managed by Spring
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a5ca7d7]

解决方法:

确保有可用的连接池被使用,引入第三方连接池要做好配置

15.SpringBoot项目中service层互相引用

Description:
The dependencies of some of the beans in the application context form a cycle:
xxxController (field private aaaService xxxController.aaaService)
┌─────┐
| aaaImpl defined in file [aaaImpl.class]
↑ ↓
| bbbImpl (field private aaaService bbbImpl.orderService)
└─────┘

解决方法:

注入方式用的是@RequiredArgsConstructor 注解final方式注入报错
将注入方式改为@Autowired成功解决

16.SpringBoot配置文件中使用了过时的配置项

Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException:
The elements [spring.resources.chain.gzipped] were left unbound.

已废弃的配置项

spring:
resources:
chain:
gzipped: true

解决方法:删掉过期的配置项即可

作者:意识流丶
链接:https://www.jianshu.com/p/217017b2e73d
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

SpringBoot项目中遇到的BUG的更多相关文章

  1. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  2. 在SpringBoot项目中添加logback的MDC

    在SpringBoot项目中添加logback的MDC     先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...

  3. 自身使用的springboot项目中比较全的pom.xml

    在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...

  4. springboot 项目中获取默认注入的序列化对象 ObjectMapper

    在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...

  5. springboot项目中js、css静态文件路径访问

    springboot静态文件访问的问题,相信大家也有遇到这个问题,如下图项目结构. 项目结构如上所示,静态页面引入js.css如下所示. 大家肯定都是这样写的,但是运行的话就是出不来效果,图片也不显示 ...

  6. 解决springboot项目中@Value注解参数值为null的问题

    1.错误场景: springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出 ...

  7. springboot项目中引用其他springboot项目jar

    1. 剔除要引入的springboot项目中不需要的文件:如Application和ApplicationTests等 2.打包 不能使用springboot项目自带的打包插件进行打包: 3.打包 4 ...

  8. 在前后端分离的SpringBoot项目中集成Shiro权限框架

    参考[1].在前后端分离的SpringBoot项目中集成Shiro权限框架 参考[2]. Springboot + Vue + shiro 实现前后端分离.权限控制   以及跨域的问题也有涉及

  9. 五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链

    买买买结算系统 一年一度的双十一购物狂欢节就要到了,又到剁手党们开始表演的时刻了.当我们把种草很久的商品放入购物车以后,点击"结算"按钮时,就来到了买买买必不可少的结算页面了.让我 ...

随机推荐

  1. MQTT QOS含义

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. 后盾网lavarel视频项目---图片上传

    后盾网lavarel视频项目---图片上传 一.总结 一句话总结: 前端还是普通的前端操作,前端上传图片的地址就是图片上传的路由,后端代码也很简单 public function uploader(R ...

  3. php面向对象 2

    继承概念:如果一个类有子类,那么该子类会继承父类的一切东西(私有成员访问不到)在定义子类的时候需要加一个关键字:extends特点:单继承,一个类只能有一个父类如果父类中有构造函数,子类在实例化的时候 ...

  4. Linux 的路由功能

    目录 文章目录 目录 前文列表 路由器 Router 路由 Routing 静态路由与动态路由 通过路由实现的全网通信示例 Linux 作为路由器 route 指令 路由表项的类型 ip route ...

  5. 【DVWA】SQL Injection(SQL 注入)通关教程

    日期:2019-07-28 20:43:48 更新: 作者:Bay0net 介绍: 0x00.基本信息 关于 mysql 相关的注入,传送门. SQL 注入漏洞之 mysql - Bay0net - ...

  6. 字体Lucida Console

    曾经有个段子说的是,一眼能认出黑客的原因就是因为对方在使用黑屏荧光字加Lucida Console其实这正说明了Lucida Console在终端使用的受欢迎程度.Lucida Console也是英文 ...

  7. github创建项目,并提交本地文件

    1.如图所示,不要点选"Initialize this repository with README",不然就看不到第二幅图的提示信息了 2.根据下面提示,初始化本地文件,然后上传

  8. Arouter核心思路和源码

    前言 阅读本文之前,建议读者: 对Arouter的使用有一定的了解. 对Apt技术有所了解. Arouter是一款Alibaba出品的优秀的路由框架,本文不对其进行全面的分析,只对其最重要的功能进行源 ...

  9. python 连接 hive数据库环境搭建

    首先需要安装以下Python 包:(我用的是Python 2) 在安装Python包之前需要安装一些依赖工具: Debian/Ubuntu: apt-get install python-dev li ...

  10. 关于token的理解

    什么是token token的意思是“令牌”,是服务端生成的一串字符串,作为客户端进行请求的一个标识. 当用户第一次登录后,服务器生成一个token并将此token返回给客户端,以后客户端只需带上这个 ...