mybatis(9)——当实体类的成员变量名称和数据库表中名称不一样的两种处理方式:起别名和使用resultMap标签
·
1.在sql语句之中起别名
<select id="getUserList" resultType="User">
select id, name, pwd as password from mybatis_test.user
</select>
这样User类的成员变量password就可以接收到表中pwd这一列的值了
2.定义resultMap标签
<resultMap id="UserMap" type="User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserList" resultMap="UserMap">
select id, name, pwd as password from mybatis_test.user
</select>
- resultMap标签与select标签是并列关系,resultMap标签不一定非要写在select标签的前面
- select的resultType属性变成resultMap属性,对应的值不再是User类,而是对应resultMap标签的id属性
- resultMap的type对应着User类
- resultMap里面的子标签的column对应着表的列名,property对应着类的成员变量
- 表的列名和类的成员变量名字已经是相同的时候,不需要写出来,只写名称不对应的result标签就可以了,如下:
<resultMap id="UserMap" type="User">
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserList" resultMap="UserMap">
select id, name, pwd as password from mybatis_test.user
</select>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)