MyBatis各种查询功能 list集合 &单行单列&map集合

1.查询一个实体类对象

一条数据对应一个用户对象(实体类),多条数据得用一个容器接收,list或者map 。

若sql语句查询结果为一条时,可以使用实体类类型或list集合类型作为方法的返回值。

①接口方法

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

②映射文件

<mapper namespace="com.atguigu.mybatis.mapper.SelectMapper">

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

</mapper>

③测试类

@Test
    public void testGetUserById(){
    SqlSession session = SqlSessionUtil.getSession();
    SelectMapper mapper = session.getMapper(SelectMapper.class);
    User userById = mapper.getUserById(1);
    System.out.println(userById);
}

2. 查询一个list集合

若sql语句查询的结果为多条时,一定不能实体类类型作为方法的返回值,

否则抛出异常 TooManyResultsException 。

①接口方法

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

②映射文件

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

③测试类

    @Test
    public void testGetAllUser(){
        SqlSession session = SqlSessionUtil.getSession();
        SelectMapper mapper = session.getMapper(SelectMapper.class);
        List<User> allUser = mapper.getAllUser();
        for (User user : allUser){
            System.out.println(user);
        }
    }

3. 查询总记录数等等单行单列(查询单个数据)

查询的是一个单行单列数据,数据要转换为java中的哪一种类型,

在把这种类型或者类型别名设置在resultType中。

Mybatis中为java中常用的类型设置了类型别名 ,可以在Mybatis官方文档中了解。

根据查询内容设置返回值为什么样的类型,把查询的

结果里面resultType内容转换为返回值/别名 。

①接口方法

//    查询用户的总数量
    Integer getCount();

②映射文件

    <!--    Integer getCount();-->
    <!--
例如Integer别名:integer,int
          int:_int,_integer
          Map:map
       String:string
-->
    <select id="getCount" resultType="Integer">
        select count(*) from t_user
    </select>

③测试类

@Test
    public void testGetCout(){
        SqlSession session = SqlSessionUtil.getSession();
        SelectMapper mapper = session.getMapper(SelectMapper.class);
        Integer count = mapper.getCount();
        System.out.println(count);
    }

4. 查询数据为map集合

1)查询一条数据为map集合

例如:查询出来的结果里面有分组函数,查询员工,部门里面的平均薪资/

最高薪资/最低薪资等 这时候可能就没有相对应实体类。

实体类:实体类的属性都是固定的

Map集合:键不固定的,所以当我们查询的结果没有相对应实体类的时候,

就可以查询出来的为一个Map集合。

因为之前查询的是一个实体类,是因为字段名和属性一致,

就可以把字段赋值到相对应的属性,现在查询的是一个Map集合,

Map集合里面是以字段名为键,以字段的值为值,所以查询结果为Map集合,

想查什么就查什么,这种类型可以把任意表,任意字段获取,

不拘束于对象,只要数据表能拿到。

实体类查询的每一个字段都有值,而当前查询结果里面某个字段的值为null,

这个字段是不会放到Map集合里面的。

总结:查询的结果有相对应实体类,用实体类对象接收。

查询的结果无相对应实体类,用map集合方式接收。

①接口方法

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

②映射文件

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

③测试类

 @Test
    public void testGetUserByIdMap(){
        SqlSession session = SqlSessionUtil.getSession();
        SelectMapper mapper = session.getMapper(SelectMapper.class);
        Map<String, Object> userByIdToMap = mapper.getUserByIdToMap(1);
        System.out.println(userByIdToMap);
        //{password=123456, gender=男, id=1, age=22, email=31456@qq.com, username=admin}
    }

2)查询多条数据为map集合

    • 方式一 List集合存储每一条Map集合

一条数据对应(转换) 一个Map,现在查询的是多条数据,返回值只能接收一条数据,

所以要用list集合存储每一条数据所转换的Map集合。

①接口方法

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

②映射文件

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

③测试类

 @Test
    public void testGetAllUserToMap(){
        SqlSession session = SqlSessionUtil.getSession();
        SelectMapper mapper = session.getMapper(SelectMapper.class);
        List<Map<String,Object>> allUserToMap = mapper.getAllUserToMap();
        for (Map map:allUserToMap){
            System.out.println(map);
        }

    • 方式二 @MapKey注解

Map和list不一样,可以直接把每一条数据转换为Map集合放到list中,

不能直接把每一条数据转换为Map集合放到Map中,因为Map是键值对。

查询出来的数据可以作为值,但是谁作为键,所以要用到注解叫做@MapKey,

把当前查询的数据转换的Map集合放到一个大的Map集合中通过注解设置Map集合键

将查询的某个字段的值作为大的map的键。

①接口方法

//    查询所有的用户信息为map集合
//    比如当前所查询的id来作为map的键,值就是当前每一条数据所转换的map集合
    @MapKey("id")
    Map<String, Object> getAllUserToMap();

②映射文件

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

③测试类

 @Test
    public void testGetAllUserToMap(){
        SqlSession session = SqlSessionUtil.getSession();
        SelectMapper mapper = session.getMapper(SelectMapper.class);
        Map<String, Object> allUserToMap = mapper.getAllUserToMap();
        System.out.println(allUserToMap);
        /**  查询出来的结果:
         * {1={password=123456, gender=男, id=1, age=22, email=31456@qq.com, username=admin},
         *  2={password=123456, gender=女, id=3, age=33, email=12345@qq.com, username=root},
         *  3={password=123456, gender=女, id=4, age=33, email=12345@qq.com, username=root}}
         */
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值