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

浙公网安备 33010602011771号