Mybatis的ResultMap用法

如果从数据库中查询出来的字段名与pojo类的属性值不一致,通过定义一个resultMap对列名和pojo的属性名之间做一个映射关系

<mapper namespace="cn.itcast.mybatis.mapper.UserMapper">
	<!-- 定义resultMap
		type:ResultMap最终映射的java对象类型,可以使用别名
		id:resultMap的唯一标识
	 -->
	<resultMap type="cn.itcast.mybatis.po.UserCustom" id="findByName">
		<!-- id:映射主键  column:字段名  property:pojo类属性名 -->
		<id column="_id" property="id"/>
		<result column="_sex" property="sex"/>
		<result column="_username" property="username"/>
		<result column="_birhtday" property="birthday"/>
		<result column="_address" property="address"/>
	</resultMap>
	
	<!-- 由于查询出来的字段与pojo类属性值不对应,通过resultMap属性映射 -->
	<select id="findUserById" parameterType="java.lang.String" resultMap="findByName">
        <!--从数据库中查询出来的字段名和pojo类的属性名不一致-->
		select id _id,username _username, sex _sex,birthday _birthday,address _address from user where username like '${value}'
	</select>
</mapper>

<!--public List<UserCustom> findUserById(String username) throws Exception;-->


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值