小新*

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

1.问题image-20221112190949038

image-20221112191001522

出现以上情况,会出现password显示为null

解决办法:

  • 起别名,将数据库表中的名字在UserMapper中改为在实体类中定义的名字,修改sql
<select id="getUserById" resultType="com.kuang.pojo.User" parameterType="int">
        select id,name,pwd as password from mybatis.user where id=#{id}
   </select>

2.resultMap

结果集映射

id  name  pwd 
id  name  password
<!--结果集映射-->
 <resultMap id="UserMap" type="User">
     <!--property是实体类中的属性,column数据库中的字段-->
     <result column="id" property="id"/>
     <result column="name" property="name"/>
     <result column="pwd" property="password"/>
 </resultMap>

 <select id="getUserById" resultMap="UserMap" >
     select * from mybatis.user where id=#{id}
</select>
  • resultMap 元素是 MyBatis 中最重要最强大的元素
  • ResultMap 的设计思想是,对简单的语句根本不需要配置显示的结果映射,对于复杂一点的语句,只需要描述语句之间的关系就行了。
  • ResultMap最优秀的地方在于,虽然你对他已经相当了解,但是根本就不需要显示地用到他们
posted on   小新*  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
 
点击右上角即可分享
微信分享提示