映射器----使用resultMap映射结果集(一)

    为了支持复杂的映射,select元素提供了resultMap属性,在说明resultMap时,首先有必要区分一下另外一个属性:resultType

元素说明
resultType

定义类的全路径,结果集可以通过java bean的规范映

射;或者定义:int 、double、map等,

注意不能和resultmap同时使用!

resultMap映射集的引用,可以提供自定义映射规则

一个简单的select语句:

<select id="getAge" parameterType="int" resultType="int">
	select age from user where id=#{id}
</select>

    resultType表示该语句的返回的结果类型。也可以是类的全限定名,如:com.lyy.bean.User

如果数据库中对应的的用户表(user),而且其中有个user_name,要与User类中的属性 userName 保持一致,

可以再sql语句中写成

select user_name as userName from user

    这样就和User类保持一致了。resultType的一些知识简单介绍了一下,接下来就开始分析resultMap。

<mapper namespace="com.lyy.dao.PoetriesMapper">
	<resultMap id="poetriesMapper" type="com.lyy.bean.Poetries">
		<id property="id" column="id"/>
		<result property="poetId" column="poet_id"/>
		<result property="content" column="content"/>
		<result property="title" column="title"/>
	</resultMap>
<select id="getContent" resultMap="poetriesMapper" parameterType="com.lyy.bean.Poetries">
		select pr.id,pr.poet_id,pr.content,pr.title,p.id , p.name from poetries pr
		LEFT JOIN poets p on pr.poet_id=p.id
		<where>
			<if test="poetId!=-1"><!-- 先前默认诗人id为-1,在此处通过对poetId值判断,便于连接 -->
				and	poet_id=#{poetId}
			</if>
			<if test="content!=null and !"".equals(content.trim())">
					and content like concat('%',#{content},'%')
			</if>
		</where>
		limit 0,20
	</select>
</mapper>
其中,resultMap元素中的type代表使用的映射类为,可以使用别名或全限定名(别名需要在主配置文件中设置别名)

<id property="id" column="id"/>
id元素代表sql中的主键,result元素代表属性,property为类对应属性,column为表对应字段,这样就可以完成简单的映射了。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盒曰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值