MyBatis框架学习-各种查询功能-4

 /*
    * MyBatis的各种查询功能
    * 1、若查询出的数据只有一条,可以通过实体类对象或者集合 接收
    * a>可以通过实体类接收
    * b>可以通过list集合接收
    * c>可以通过map集合接收
    * 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin}  map是无序的
    * 2、若查询出的数据有多条
    * a>可以通过list集合接收
    * b>可以通过map类型的list集合接收
    * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换成map集合作为值,以某个字段的值作为键,放在同一个map集合中
    * 注意:一定不能通过实体类对象接收,此时会抛出异常 TooManyResultsException
    *
    * MyBatis中设置了默认的类型名
    * java.lang.Integer-->int,integer
    * int-->_int,_integer
    * */

1、若查询出的数据只有一条

可以通过实体类对象或者集合 接收

a>可以通过实体类接收
b>可以通过list集合接收

在这里插入图片描述

package com.rjs.mybatis.mapper;

import com.rjs.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author: 软件手
 * @date: 2022/7/23 23:00
 * @description:
 */
public interface SelectMapper {

    // 1、根据id查询用户信息
    // User getUserById(@Param("id") Integer id);
    List<User> getUserById(@Param("id") Integer id);
}
 <!--User getUserById(@Param("id") Integer id);-->
    <select id="getUserById" resultType="User">
        select * from t_user where id = #{id}
    </select>
@Test
    public void testGetUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getUserById(3));
    }

在这里插入图片描述

2、查询一个list集合

public interface SelectMapper {
    // 2、查询所有用户信息
    List<User> getAllUser();
}
 <!--List<User> getAllUser();-->
    <select id="getAllUser" resultType="User">
        select * from t_user
    </select>
 @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getAllUser());
    }

在这里插入图片描述

3、查询总记录数

int默认为0,integer默认为null,所以integer只要不是null,就有数据
在这里插入图片描述
MyBatis配置了相关类型映射且不区分大小写,MyBatis中文网

MyBatis中设置了默认的类型名
a> java.lang.Integer–>int,integer
b> int–>_int,_integer

<!--Integer getCount();       resultType="java.lang.Integer   Integer int Int 都行 MyBatis自己设置了     -->
    <select id="getCount" resultType="java.lang.Integer">
        select count(*) from t_user
    </select>
 // 3、查询用户信息的总记录数
    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getCount());
    }

在这里插入图片描述

4、根据id查询用户信息为一个map集合

// 4、根据id查询用户信息为一个map集合
    Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
  <!-- Map<String, Object> getUserByIdToMap();-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>
// 4、根据id查询用户信息为一个map集合
    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getUserByIdToMap(3));
    }

在这里插入图片描述

5、查询所有用户信息为map集合

   // 5、查询所有用户信息为map集合
    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        System.out.println(mapper.getAllUserToMap());
    }
<!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
// 5、查询所有用户信息为map集合
    List<Map<String, Object>> getAllUserToMap();

在这里插入图片描述

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

@MapKey("id")
Map<String, Object> getAllUserToMap();

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值