mybatis中当实体类属性名和字段名不一致时,如何调整呢?
下文笔者讲述mybatis中实体类属性名和字段名不一致的处理方法分享,如下所示
实现思路:
方式1:
在sql中,定义字段名的别名
即可实现字段名和属性名一致的效果
方式2:
定义一个resultMap定义字段名和属性名的映射
例:
1.使用sql语句的别名
让字段名的别名和实体类的属性名一致
<select id=”selectorder” parametertype=”int”
resultetype=”com.java265.User”>
select user_id id, user_no userno
,user_name name form users where user_id=#
{id};
</select>
方式2:
使用resultMap映射字段名和实体属性
<select id="getUser"
parameterType="int" resultMap="userresultmap">
select * from users where user_id=#{id}
</select>
<resultMap type=”com.java265.User” id=”userresultmap”>
<!–用id属性来映射主键字段–>
<id property=”id” column=”user_id”>
<!–用result属性来映射非主键字段,property为实体类属性名,column为数据表中的属性–>
<result property = “userno” column =”user_no”/>
<result property=”name” column=”user_name” />
</reslutMap>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


