SpringBoot项目中遇到的BUG问题及解决方法


当前第2页 返回上一页

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

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

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

到此这篇关于SpringBoot项目中遇到的BUG问题及解决方法的文章就介绍到这了,更多相关SpringBoot项目遇到bug内容请搜索


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...