Mybatis出现的错误

在使用Mybatis持久层框架进行操作数据库的时候,出现了以下的问题:

严重:  

严重:Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logContrller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.youquan.service.LogService com.youquan.mvc.LogContrller.logService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.youquan.dao.LogDao com.youquan.service.LogServiceImpl.logDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.youquan.dao.LogDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

一直没有看懂是哪里的问题,问了朋友说是xml映射错误,后来自己仔细的看了下错误,发现了一个问题:

 Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'LogResult'.  Cause: java.lang.ClassNotFoundException: Cannot find class: LogResult

这个就是错误的根源所在,是这个类型不能返回,找那个文件中定义了这个 LogResult,在我的xml映射文件中找到了:

<mapper namespace="com.youquan.dao.LogDao">
    <resultMap id="LogResult" type="com.youquan.bean.Log" autoMapping="false" >
        <id property="id" column="id" /><!--主键-->
        <result property="uid" column="uid" />
        <result property="createTime" column="createTime" />
        <result property="ip" column="ip" />
    </resultMap>
    <select id="findAll" resultType="LogResult">
        SELECT * FROM log;
    </select>

这里我们看到问题在 查询语句中的resultType="LogResult"这里我们返回了一个由<resultMap>定义的结果集这个是错误的问题所在,解决: 把resultType改成resultMap, 或者把LogResult这个换成实体类,或别名

这里作者就去查看了resultType与resultMap的区别:

restulyType:

对应的是java对象中的属性,大小写不敏感,

如果放的是java.lang.Map,key是查询语句的列名,value是查询的值,大小写敏感

resultMap:

指的是定义好了的id的,是定义好的resyltType的引用

注意:用resultType的时候,要保证结果集的列名与java对象的属性相同,而resultMap则不用,而且resultMap可以用typeHander转换

type:java 对象对应的类,id:在本文件要唯一column :数据库的列名或别名,property:对应java对象的属性,jdbcType:java.sql.Types

查询语句中,resultMap属性指向上面那个属性的标签的id, parameterType:参数类型,只能传一个参数,如果有多个参数要封装,如封装成一个类,要写包名加类名,基本数据类型则可以省略一对1、一对多时,若有表的字段相同必须写别名,不然查询结果无法正常映射,出现某属性为空或者返回的结果与想象中的不同,而这往往是没有报错的。

若有意外中的错误,反复检查以上几点,和认真核查自己的sql语句,mapper.xml文件是否配置正确。

简单来说就是resultType不能返回由resultMap的结果集,

resultMap只能使用由resultMap的结果集

 

更详细的resultType与resultMap的区别请看https://blog.csdn.net/leo3070/article/details/77899574/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值