【MyBatis-03】MyBatis的各种查询功能

一、查询一个实体类对象

  1. mapper接口中(SelectMapper)
    返回类型为User
User getUserById(@Param("id") int id);
  1. mapper映射文件中(SelectMapper.xml)
	<!--User getUserById(@Param("id") int id);-->
    <select id="getUserById" resultType="User">
        select * from t_user where id=#{id}
    </select>
  1. test测试文件中(SelectMapperTest)
	@Test
    public void testGetUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User user = mapper.getUserById(4);
        System.out.println(user);
    }

二、查询一个List集合

  1. mapper接口中(SelectMapper)
    返回类型为List<User>
User getUserById(@Param("id") int id);
  1. mapper映射文件中(SelectMapper.xml)
	<!--List<User> getUserList();-->
    <select id="getUserList" resultType="User">
        select * from t_user
    </select>
  1. test测试文件中(SelectMapperTest)
	//查询一个List集合
    @Test
    public void testGetUserL(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<User> list = mapper.getUserList();
        System.out.println(list);
    }

三、查询单个数据

  1. mapper接口中(SelectMapper)
    返回类型为int
	//查询用户总数(总记录数)
    int getCount();
  1. mapper映射文件中(SelectMapper.xml)
	<!--int getCount();-->
    <select id="getCount" resultType="_int">
        select  count(id) from t_user
    </select>

注:在MyBatis中,Java中常用的类型的类型别名百度即可
3. test测试文件中(SelectMapperTest)

	//查询单个数据
    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        int a = mapper.getCount();
        System.out.println(a);
    }

四、查询一条数据为map集合

  1. mapper接口中(SelectMapper)
    返回类型为Map<String,Object>,可参考map类型参数
	//根据用户id查询用户信息为map集合
    Map<String, Object> getUserToMap(@Param("id") int id);
  1. mapper映射文件中(SelectMapper.xml)
	<!--Map<String, Object> getUserToMap(@Param("id") int id);-->
    <select id="getUserToMap" resultType="map">
        select  * from t_user where id=#{id}
    </select>
  1. test测试文件中(SelectMapperTest)
	//查询User实体类对象
    @Test
    public void testGetUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map map = mapper.getUserToMap(4);
        System.out.println(map);
    }

五、查询多条数据为map集合

方法1:List<Map<String,Object>>

此时可以将这些map放在一个list集合中获取:

  1. mapper接口中(SelectMapper)
    返回类型为List<Map<String,Object>>
	//查询所有用户信息为map集合
    List<Map<String, Object>> getAllUserToMap();
  1. mapper映射文件中(SelectMapper.xml)
	<!--List<Map<String, Object>> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
  1. test测试文件中(SelectMapperTest)
	//查询所有用户信息为map集合
    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<Map<String,Object>> list = mapper.getAllUserToMap();
        System.out.println(list);
    }
方法2:@MapKey

此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的map集合

  1. mapper接口中(SelectMapper)
    返回类型为List<Map<String,Object>>
	//查询所有用户信息为map集合
    @MapKey("id")
    Map<String, Object> getAllUserToMap2();

注意:此时@MapKey(“id”)中的id仅仅是设置id为Map的键,不要理解为方法参数。
2. mapper映射文件中(SelectMapper.xml)

	<!--Map<String, Object> getAllUserToMap2();-->
    <select id="getAllUserToMap2" resultType="map">
        select * from t_user
    </select>
  1. test测试文件中(SelectMapperTest)
	//查询所有用户信息为map集合
    @Test
    public void testGetAllUserToMap2(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map map = mapper.getAllUserToMap2();
        System.out.println(map);
    }

本文主要参考:
【尚硅谷】2022版MyBatis教程(细致全面,快速上手)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值