标签:springboot resultMap 查询 resultType Mybatis id select
一、事情起因:在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
标签:springboot,resultMap,查询,resultType,Mybatis,id,select 来源: https://www.cnblogs.com/prettychandler/p/16173497.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。