MyBatis中resultType属性的使用
更新时间:2024年09月03日 16:07:38 作者:一瓶橄榄菜
这篇文章主要介绍了MyBatis中resultType属性的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
MyBatis中resultType属性
resultType:MyBatis中resultType是用来指定查询结果类型的属性
一.返回一般数据类型
对于引用类型一般采用大写字母转小写的方式。
基本类型一般在前面加"_"
下面给了两个例子:
string
- mapper接口:
String getPasswordByName(@Param("name") String name);- xml:
<select id="getPasswordByName" resultType="string">
select password from user where name = #{name}
</select>int
- mapper接口:
int getIdByName(@Param("name") String name);- xml:
<select id="getIdByName" resultType="_int">
select id from user where name = #{name}
</select>二.返回JavaBean 类型
比如要返回一个user:
- mapper接口:
User getUserByName(@Param("name") String name);- xml:
<select id="getUserByName" resultType="user">
select * from user where name = #{name}
</select>三.返回List
一般在mapper接口中返回List,在xml中resultType写上T就可以。
- mapper接口:
List<User> list();
- xml:
<select id="list" resultType="user"> select * from user </select>
四.返回Map类型
1.如果是将某个字段作为key,对象作为value
@MapKey("id")
Map<Integer,User> getUser();- xml:
<select id="getUserById" resultType="User"> select * from user </select>
结果:
{1=User(id=1, babayId=1, name=kail)}
2.如果只有一条记录可以将字段名作为key,值作为value
Map<Integer,Object> getUserById(@Param("name") int id);- xml:
<select id="getUserById" resultType="map">
select * from user where id={id}
</select>结果:
{id=1, babayId=1, name=kail}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring中Bean的加载与SpringBoot的初始化流程详解
这篇文章主要介绍了Spring中Bean的加载与SpringBoot的初始化流程详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-11-11
Springboot 整合 Java DL4J 实现农产品质量检测系统(推荐)
本文详细介绍了系统的搭建过程,包括技术选型、数据处理、模型训练和评估等关键步骤,系统采用卷积神经网络,对水果成熟度和缺陷进行识别,有效解决了传统方法成本高、效率低的问题,有助于提升农产品检测的科技含量和自动化水平2024-10-10


最新评论