广州飞狐科技有限公司官网
技术文章
2020-12-27 18:28:38

Spring与Mybatis整合 ,遇到的三种报错(已解决)

分享到:

在Spring整合Mybatis时,遇到此问题。

1.


java.lang.NoSuchMethodException: com.gql.pojo.User.() 解决方案


Caused by: java.lang.NoSuchMethodException: com.gql.pojo.User.<init>()
at java.lang.Class.getConstructor0(Class.java:2892)
at java.lang.Class.getDeclaredConstructor(Class.java:2058)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:60)
... 69 more


解决方案

这是因为mybatis框架在初始化bean的时候需要无参构造器,写了有参构造器,会把无参构造器覆盖掉,需要在pojo中加上一个无参构造。


2.

[applicationContext.xml] cannot be opened because it does not exist 解决方案


在Spring整合Mybtis的时候遇到的一个问题:Java项目中ClassPathXmlApplicationContext默认加载的是src下的applicationContext.xml文件,为什么将applicationContext.xml文件放在了config文件下也能加载成功?

解决方案

这是因为config不是一个文件夹,而是和src同级目录的一个类路径,在Java项目中新建config时候,不要选择为new Folder,而应该选择new Source Folder。


3.

HttpServletRequest cannot be resolved to a type解决方案


解决方案

原因: jdk 里不包括servlet-api.jar 这个包,此包在Tomcat 中存在。

在这里插入图片描述


将tomcat中的此jar包copy到项目中并builpath即可。




上一篇:SpringCloud系列-Ribbon客户端侧负载均衡
下一篇:Spring Cloud Gateway:新一代API网关服务