Mybatis中resultType和resultMap的区别

一、事情起因:在springboot整合mybatis时,测试出了异常

 

 

 二、通过排查,发现在xml配置文件中,单表查询中误将resultType错写成resultMap

 

 

 三、resultType和resultMap区别

resultType是SQL语句的返回类型,需要有对应的pojo类,因此在单表查询时,resultType是最合适的。因此将上述错误改为如下即可

<select id="getUserById" resultMap="com.boot.admin.bean.User">
        select * from t_admin where id = #{id} 
</select>

resultMap作SQL语句的返回类型时,需要引用外部resultMap标签,也可以将上述查询改为如下方式

复制代码
<resultMap id="byIdResultMap" type="com.boot.admin.bean.User">
        <!--设置主键映射-->
        <id property="id" column="id" />
     <!--设置列名和pojo属性映射-->
<result property="loginAcct" column="login_acct"/> <result property="userPswd" column="user_pswd"/> <result property="userName" column="user_name"/> <result property="email" column="email"/> <result property="createTime" column="create_time"/> </resultMap> <select id="getUserById" resultMap="byIdResultMap">
select * from t_admin where id = #{id}
</select>
复制代码

注意:在springboot中,如果数据库列名和POJO属性不统一,需要开启驼峰命名来设置映射关系,在application.xml中添加如下配置

mybatis.configuration.map-underscore-to-camel-case=true

 

posted @   哒啦蹦哒啦  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示