小新*

导航

 

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 2022-12-01 19:37  小新*  阅读(39)  评论(0)    收藏  举报