@Results与@ResultMap的使用与注意事项

import com.epson.vipwp.domain.InfotypeName;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IInfotypeNameDAO {

	@Results(id="infotypeName", value = {
			@Result(property = "infotypeCode", column = "infotype_code"),
			@Result(property = "languageCode", column = "language_code"),
			@Result(property = "typeName", column = "type_name")
	})

	@Select("SELECT * FROM t_infotype_name where language_code = #{lang} order by type_name")
	//@ResultMap("infotypeName")  因Results定义在当前接口上方,因此当前接口一定不能有@ResultMap("infotypeName")否则项目启动可能不会报错,但是一旦访问到当前接口就会报错提示找不到infotypeName
	List<InfotypeName> getAllByLang(String lang);

	@Select("SELECT * FROM t_infotype_name where type_name = #{typeName} and language_code = #{languageCode}")
	@ResultMap("infotypeName")
	InfotypeName selectByTypenameLang(InfotypeName infotypeName);
}


注意事项:

 该接口一定不要继承其他没意义的接口,否则会导致启动报错,提示infotypeName找不到
@Results在接口里面最上方定义,然后在需要的地方使用 @ResultMap("infotypeName") 即可指定结果集 ,但是切记第一个接口不能使用 @ResultMap()因为本身就定义在第一个接口上方,一定要使用 @ResultMap那么项目会报错

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis中,resultType和resultMap都是用于查询结果映射的方式。 resultType是直接指定查询结果的类型。它可以是一个基本类型(比如String、Integer等),也可以是一个自定义的实体类。当使用resultType时,MyBatis会根据查询结果的字段名和实体类的属性名进行自动映射。 resultMap则是对外部ResultMap的引用。它是一种更加灵活和可重用的方式,可以定义在Mapper文件中,以供多个查询语句共享。resultMap通过映射规则,将查询结果的列和实体类的属性进行映射。 所以,resultType和resultMap的区别在于: - resultType适用于简单的查询结果映射,通过指定查询结果的类型来进行自动映射。 - resultMap适用于复杂的查询结果映射,通过外部ResultMap的引用来进行映射,并可以定义更加灵活和可重用的映射规则。 总结起来,resultType适用于简单的查询,而resultMap适用于复杂的查询,并且可以提供更灵活的映射规则。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [resultMapresultType的区别](https://blog.csdn.net/LMY0210/article/details/128238290)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis中关于resultType和resultMap的区别介绍](https://download.csdn.net/download/weixin_38717031/12793901)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

往事不堪回首..

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

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

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

打赏作者

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

抵扣说明:

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

余额充值