黑马Javaweb-用户登录案例遇到的问题
- 1、Could not find resource org/mybatis/example/mybatis-config.xml
- 2、Class com.itheima.web.LoginServlet is not a Servlet
- 3、java.lang.ClassNotFoundException: Cannot find class: “com.mysql.jdbc.Driver
- 4、java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
- 5、有时候会出现build错误,可能是maven版本有问题
1、Could not find resource org/mybatis/example/mybatis-config.xml
- 这个问题是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
- 这个问题涉及到依赖的作用范围,共四种作用范围
- test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖
- compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去
- provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,tomcat等web服务器已经存在了,如果再打包会冲突
- 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