黑马Javaweb-用户登录案例遇到的问题

1、Could not find resource org/mybatis/example/mybatis-config.xml

错误1

  • 这个问题是loginServlet中传入mybatis-config.xml的路径错误导致的,默认从mybatis中文网中复制过来的是全路径,案例是将该配置文件放在resources目录下,可以直接读取
//String resource = "org/mybatis/example/mybatis-config.xml"; 错误
String resource = "mybatis-config.xml"; //正确
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

2、Class com.itheima.web.LoginServlet is not a Servlet

  • 这个问题涉及到依赖的作用范围,共四种作用范围
  1. test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖
  2. compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去
  3. provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
  4. runtime在运行的时候依赖,在编译的时候不依赖

默认的依赖范围是compile

在这里插入图片描述

3、java.lang.ClassNotFoundException: Cannot find class: “com.mysql.jdbc.Driver

  • 这个问题应该有两种情况:
    • 是MySQL驱动版本和MySQL版本不一致导致的,我用的8.0.28的MySQL,按照老师的步骤使用5.1.34版本驱动,就会发生这个问题
    • 高版本的驱动位置写错了,应该是com.mysql.cj.jdbc.Driver

4、java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)

  • mybatis-config.xml配置中的数据库连接密码改成自己的就行

5、有时候会出现build错误,可能是maven版本有问题

  • 我被这个问题折磨了两三天,甚至一度不想看,最后竟然发现真的换了maven版本就可以了,从3.8.5换到3.6.3,问题立刻解决了。后来想重现问题,整理错误记录,发现换回来也可以了,build有一个爆红,但是程序正常可以执行,也不知道问题出在哪里
org.codehaus.plexus.component.repository.exception.ComponentLookupException: com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.NoSuchMethodError: org.apache.maven.model.validation.DefaultModelValidator: method <init>()V not found
  at org.jetbrains.idea.maven.server.embedder.CustomModelValidator.<init>(Unknown Source)
  while locating org.jetbrains.idea.maven.server.embedder.CustomModelValidator
  at ClassRealm[maven.ext, parent: ClassRealm[plexus.core, parent: null]] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)
  while locating org.apache.maven.model.validation.ModelValidator annotated with @com.google.inject.name.Named(value=ide)

1 error
      role: org.apache.maven.model.validation.ModelValidator
  roleHint: ide
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值