【MyBatis】学习笔记06:各种查询所返回数据的数据类型


目录
  • 准备
    • (1)创建接口
      • (2)创建对应的映射文件
  • 查询一个实体类对象
  • 查询返回一个List集合
    • Eg1:查询出的数据只有一条
    • Eg2:查询出的数据有多条
  • 其他返回数据类型(补充:MyBatis类型别名)
    • Eg3:返回数据为一行一列
    • Eg4:返回一条数据 返回数据类型为Map集合
    • Eg5:返回多条数据 返回数据类型为map集合
      • (1)方式1: 将多个Map放在List中
      • (2)方式2: @MapKey()注解

【总结】

1.查询出的数据只有一条:

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

(2)可以通过List集合接收(Eg1)

(3)可以通过map接收(Eg4)

2.若查询的数据有多条:

(1)可以通过List集合接收(Eg2)

(2)可以通过map类型的List集合接收(Eg5(1))

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

(注意)|(一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException)

准备

(1)创建接口

(2)创建对应的映射文件

查询一个实体类对象

//SelectMapper 接口
public interface SelectMapper {
    /**
     * 查询用户信息
     * @param id 用户id
     * @return 用户对象
     */
    User getUserById(Integer id);
}

    
    @Test
    public void getUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User result  = mapper.getUserById(4);
        System.out.println(result);
    }

cc.mllt.sky.utils.SqlSessionUtils.java

package cc.mllt.sky.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtils {
    public  static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        try{
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
            sqlSession  = sqlSessionFactory.openSession(true);

        }catch (IOException e){
            e.printStackTrace();
        }

        return sqlSession;
    }
}

查询返回一个List集合

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

//SelectMapper 接口
public interface SelectMapper {
    /**
     * 查询用户信息
     * @param id 用户id
     * @return 用户对象
     */
    List getUserById(Integer id);
}

    
    @Test
    public void getUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List result  = mapper.getUserById(4);
        System.out.println(result);
    }

Eg2:查询出的数据有多条

//SelectMapper 接口
    /**
     * 查询所有用户
     * @return List
     */
    List getUsers();

    
    @Test
    public void getUsers(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List result  = mapper.getUsers();
        System.out.println(result);
    }

其他返回数据类型(补充:MyBatis类型别名)

Eg3:返回数据为一行一列

//SelectMapper 接口
    /**
     * 查询用户表行数
     * @return Integer
     */
    Integer getCount();

    

resultType中填写IntegerintegerintInt_int都不会报错

原因在 官方手册Configuration XML > typeAliases(类型别名)

因为MyBatis中设置默认的类型别名

Alias Mapped Type
_int int
int Integer
integer Integer

Alias:别名

Mapped Type:映射地址


java.lang.Integer --> int , Integer

int --> _int , _integer

Map --> map

String --> string

    @Test
    public void getCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Integer result  = mapper.getCount();
        System.out.println(result);
    }

Eg4:返回一条数据 返回数据类型为Map集合

//SelectMapper 接口
    /**
     * 根据用户id获取Map类型的信息
     * @param id 用户id
     * @return Map类型用户信息 Map
     */
    Map getUserByIdToMap(@Param("id") Integer id);

    
   @Test
   public void getUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map result  = mapper.getUserByIdToMap(4);
        System.out.println(result);
    }

获得的map集合结果以字段为键

输出结果如下

{userPassword=123456改了密码, userCount=test001, userGrade=1, userRegDate=2022-03-01T00:00, userName=测试用户改了名, userId=4, userBlance=0}

Eg5:返回多条数据 返回数据类型为map集合

(1)方式1: 将多个Map放在List中

//SelectMapper 接口
    /**
     * 获取所有用户信息
     * @return Map类型用户信息 Map
     */
    List> getusersToMap();
}

    
    @Test
    public void getUsersToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List> result  = mapper.getusersToMap();
        System.out.println(result);
    }

(2)方式2: @MapKey()注解

//SelectMapper 接口
    /**
     * 获取所有用户信息
     * @return Map类型用户信息 Map
     */
    @MapKey("userId")//MySQl中字段名,最好为key
    Map getusersToMap();

    
    @Test
    public void getUsersToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
//        List> result  = mapper.getusersToMap();
        Map result  = mapper.getusersToMap();
        System.out.println(result);
    }

输出结果如下

{1={userPassword=qq2686485465, userCount=mllt9920, userGrade=999, userRegDate=2022-03-20T00:00, userName=萌狼蓝天, userId=1, userBlance=100000000}, 2={userPassword=134679852, userCount=xrilang, userGrade=888, userRegDate=2022-03-25T00:00, userName=向日狼, userId=2, userBlance=10240000}, 4={userPassword=123456改了密码, userCount=test001, userGrade=1, userRegDate=2022-03-01T00:00, userName=测试用户改了名, userId=4, userBlance=0}, 5={userPassword=123, userCount=mllt@xrilang.com, userGrade=0, userName=张三, userId=5, userBlance=0}}