MyBatis学习笔记(六)—— MyBatis的各种查询功能

06、MyBatis的各种查询功能

6.1、查询单条数据

若查询出的数据只有一条:

(1)可以通过实体类对象接收

(2)可以通过list集合接收

(3)可以通过map集合接收

6.1.1、通过实体类对象接收

SelectMapper.java

/**
 * 根据id查询用户信息
 */
User getUserById(@Param("id") Integer id);

SelectMapper.xml

<!--User getUserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
    select * from t_user where id = #{id}
</select>

6.1.2、通过list集合接收

SelectMapper.xml

<!--List<User> getUserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
    select * from t_user where id = #{id}
</select>

6.1.3、通过map集合接收

SelectMapper.java

/**
 * 根据id查询用户信息为一个map集合
 */
Map<String,Object> getUserByIdToMap(@Param("id") Integer id);

SelectMapper.xml

<!--Map<String,Object> getUserByIdToMap(@Param("id") Integer id);-->
<select id="getUserByIdToMap" resultType="map">
    select * from t_user where id = #{id}
</select>

运行结果:

{password=123456, sex=男, id=3, age=23, email=123@qq.com, username=张三}

以字段为键,以字段对应的值为map的值

6.2、查询多条数据

若查询出的数据有多条:

(1)可以通过实体类型的list集合接收

(2)可以通过map类型的list集合接收

(3)可以在mapper接口的方法上添加@MapKey注解,此时可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中。

6.2.1、通过实体类型的list集合接收

SelectMapper.java

/**
 * 查询所有用户信息
 */
List<User> getAllUser();

SelectMapper.xml

<!--List<User> getAllUser();-->
<select id="getAllUser" resultType="User">
    select * from t_user
</select>

注意:一定不能通过实体类对象接收,此时会抛异常TooManyResultsException

6.2.2、通过map类型的list集合接收

SelectMapper.java

/**
 * 查询所有用户信息为map集合
 */
List<Map<String,Object>> getAllUserToMap();

SelectMapper.xml

<!-- List<Map<String,Object>> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
    select * from t_user
</select>

运行结果:

[{password=123456, sex=男, id=3, age=23, email=123@qq.com, username=张三}, {password=123456, sex=男, id=4, age=23, email=123@qq.com, username=admin}]

6.2.3、@MapKey

  • 可以在mapper接口的方法上添加@MapKey注解,此时可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中。

SelectMapper.java

/**
 * 查询所有用户信息为map集合
 */
@MapKey("id")
Map<String, Object> getAllUserToMap();

SelectMapper.xml

<!-- List<Map<String,Object>> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
    select * from t_user
</select>

运行结果:

{3={password=123456, sex=男, id=3, age=23, email=123@qq.com, username=张三}, 4={password=123456, sex=男, id=4, age=23, email=123@qq.com, username=admin}}

6.3、查询记录总数

1、MyBatis中设置了默认的类型别名:

mapped typetypeAliases
java.lang.Integerint、integer
int_int_integer
Mapmap
Stringstring

2、查询用户记录总数

SelectMapper.java

/**
 * 查询用户信息的总记录数
 */
Integer getCount();

SelectMapper.xml

<!--Map<String,Object> getUserByIdToMap(@Param("id") Integer id);-->
<select id="getUserByIdToMap" resultType="map">
    select * from t_user where id = #{id}
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值