Spring Boot学习之关于 resultType ,resultMap与 parameterType的基本使用的区别

本文探讨了Spring Boot中关于resultType、resultMap和parameterType的区别和使用场景。resultType用于返回数据库查询结果的类型,如获取记录总数;resultMap则用于返回结果集,例如根据主键获取复杂对象;parameterType则用于指定存入数据库的参数类型,如新增用户时传入的对象。文中通过实例展示了它们的应用,并提到了在使用@Param注解时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.resultType:从数据库中提取数据,返回结果的类型

2.resultMap:从数据库中提取数据,返回结果集

3.parametresulerType:将信息存入数据库中,传入结果集

案例1. resultType

在mapper文件中,如下

 

        <select id="getTotalAdminUser" parameterType="Map" resultType="int">
        select count(*) from tb_admin_user
        where is_deleted=0
        </select>

 检索数据库,返回tb_admin_user中的记录的总数,返回类型为int。

案例2. resultMap

    <select id="getAdminUserById" resultMap="AdminUserResult">
		select user_name
		from tb_admin_user
        where id=#{id}
        and is_deleted = 0
		ORDER BY  id DESC limit 1
	</select>

根据用户主键检索用户,返回对象为AdminUserResult对象

案例3. parametresulerType

<insert id="addUser" parameterType="xxx.entity.AdminUser">
        insert into tb_admin_user(user_name,password_md5)
        values(#{userName},#{password})
</insert>

新增用户,将前端传过来的AdminUser对象存入数据库中,所

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值