Mybatis映射报错问题
前言
在做项目时使用MyBatis映射时出现的几种问题。
一、问题一
Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.pojo.AppInfo matching [java.lang.String, java.lang.String, java.math.BigDecimal, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Long, java.lang.String]
请求处理失败;嵌套异常 org.mybatis.spring.MyBatisSystemException:在实体类中没有找到相应的构造函数。不匹配。
说的是在你使用MyBatis关系型数据库映射一条SQL语句成功后返回,如果你的返回值是一个实体类MyBatis会默认调用你返回值的无参构造函数,如果不存在则会抛出异常。
实体类中要么就有参无参构造方法都不写,要不就都要写,不能只写有参构造方法
mybatis为什么持久化类时必须提供一个不带参数的默认构造函数?
原因:因为mybatis框架会调用这个默认构造方法来构造实例对象。
反射的Class.forName(“className”).newInstance();需要对应的类提供一个无参构造函数
二、问题二
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [spring-dao.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [D:\SystemInfo\out\artifacts\SystemInfo_Web_exploded\WEB-INF\classes\com\dao\devuser\DevUserMapper.xml]’; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Mapper’s namespace cannot be empty
org.springframework.beans.factory.BeanCreationException:使用类路径资源[spring-dao.xml]中定义的名称为“sqlSessionFactory”创建bean时出错。初始化方法调用失败;嵌套异常是org.springframe .core。NestedIOException:未能解析映射资源:’file[D:\SystemInfo\out\artifacts\SystemInfo_Web_exploded\WEB-INF\classes\com\dao\devuser\DevUserMapper.xml];嵌套异常是org.apache.ibatis.builder。BuilderException:错误解析映射器XML。原因:org.apache.ibatis.builder。BuilderException:映射器的名称空间不能为空
MyBatis中< mapper namespace=”接口路径” >中没有映射命名空间也就是没有写namespace在Spring创建Bean时没有找到与之匹配指向的接口