在做SSM项目整合时,发现在测试下列代码时,出现空指针异常错误
根据ssh做项目,总是报空指针不能进入userservice,调试,userservice为null
发现是spring的配置文件没有配置成功。
加上:
@RunWith(SpringJUnit4ClassRunner.class)
出现以下异常:
在pom.xml中修改依赖
继续出现以下错误:
[16:56:24:373] [WARN] - org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:559) - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
[16:56:24:377] [INFO] - com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1908) - {dataSource-1} closed
[16:56:24:381] [ERROR] - org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@39d9314d] to prepare test instance [cn.imu.service.UserServiceTest@604b1e1d]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
Caused by: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
其中最为关键的地方就是:
主要原因:
[17:11:30:177] [WARN] - org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:559) - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [conf/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'cn/imu/dao/LoginLogMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LoginLogMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: LoginLogMap
解决方案:Caused by: java.lang.ClassNotFoundException: Cannot find class: userMap
java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for cn.imu.dao.UserMapper.UserMap
** BEGIN NESTED EXCEPTION **
Could not find parameter map Java.util.Map Parameter Maps collection does not contain value for java
发现自己使用了 parameterMap标签,因为mybatis官方已经停止支持parameterMap标签 所以 以后不要再用,改为parameterType,但是又会报最开始的错误。
真烦人,最好的办法就是,放弃使用结果集映射,重新建立实体类,使数据库中的字段和实体类中的属性一一对应。在parameterType后填入新建实体类。