尚硅谷MyBatis学习笔记第6节MyBatis的各种查询功能

358 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第2天

6、MyBatis的各种查询功能

6.1、查询一个实体类对象

/*
    若sql语句查询的结果为多条时,一定不能以实体类类型作为方法的返回值
    否则会抛出异常TooManyResultsException:

    若sql语句查询的结果为一条时,此时可以使用实体类类型或List集合类型作为方法的返回值
     */
    /**
     *根据id查询用户信息
     * @param id
     * @return
     */
    User getUserById(@Param("id")Integer id);//一个参数也可以加注解,因为它原来获取参数的方式是任意字段
    //加了注解后注意:BindingException: Parameter 'user' not found. Available parameters are [id, param1]
<!--//建议大家写xml时一个方法一个sql-->
    <!--ser getUserById(@Param("id")Integer id);-->
    <select id="getUserById" resultType="User">
        select * from t_user
    </select>
 @Test
    public void getUserById(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User userById = mapper.getUserById(2);
        //查询操作,底层代码是根据我写的mapper接口返回值对象来调用方法
        //比如返回一个User  就会用sqlSession.selectOne()
        //当查询语句返回对象冲突TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
        System.out.println(userById);
    }

6.2、查询一个list集合

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

当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常 TooManyResultsException;但是若查询的数据只有一条,可以使用实体类或集合作为返回值

6.3、查询单个数据

    /**
     * 查询用户的总数量
     * 表格不分组的情况下,查询的是单行单列
     *
     */
    Integer getCount();
    <!--Integer getCount();-->
    <!--
        MyBatis中为Java中常用的类型设置了类型别名
        Integer:Integer,int
        int:_int,_integer
        Map:map
        String:string
    -->
    <select id="getCount" resultType="java.lang.Integer"><!--resultType是int也行,Integer也行,因为Mybatis设置过别名-->
        select count(*) from t_user
        <!--如果count(字段),表格字段为null的记录不会被算-->
    </select>

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

    /**
     * 根据id查询用户信息为Map集合
     * 以后很多查询都没有实体类,而是放到Map里
     */
    Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
    <!--Map<String,Object> getUserByIdToMap(@Param("id") Integer id);-->
    <!--
    查询结果对象为实体类和Map的区别
    实体类属性为固定,Map集合的键不固定,查询出来以字段名为键,字段值为值,非常方便
    -->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>
    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> userByIdToMap = mapper.getUserByIdToMap(4);
        //查询一条数据的结果,查询出来以字段名为键,字段值为值
        // {password=abc900, gender=男, id=4, age=24, email=19500@qq.com, username=zhaozhao}
        //查询结果字段值为null就不会放入Map集合
        System.out.println(userByIdToMap);
    }

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

    /**
     *查询所有用户信息为Map集合
     * 若查询的数据有多条时,并且要将每条数据转换为map集合
     * 此时有两种解决方案:
     * 1.将mapper接口方法的返回值设置为泛型是map的list集合
     * List<Map<String,Object>> getAllUserToMap();
     * 结果:{password=abc900, gender=男, id=4, age=24, email=19500@qq.com, username=zhaozhao}
     * 2.可以将每条数据转换的map集合放在一个大的map中,就是必须要通过@MapKey注解
     * 将查询的某个字段的值作为大的map的键
     * @return
     */
    //List<Map<String,Object>> getAllUserToMap();
    @MapKey("id")//以id为键,一条用户信息为值
    Map<String,Object> getAllUserToMap();
    <!--Map<String,Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
 @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
       // Map<String, Object> userMap = mapper.getAllUserToMap();会报错
        //TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4

        /*
        List<Map<String, Object>> list = mapper.getAllUserToMap();
        System.out.println(list);
         */
        Map<String, Object> allUserToMap = mapper.getAllUserToMap();
        System.out.println(allUserToMap);
        //{
        // 1={password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
        // 2={password=123456, gender=男, id=2, age=23, email=12345@qq.com, username=root},
        // 3={password=abc900, gender=男, id=3, age=24, email=19500@qq.com, username=zhaozhao},
        // 4={password=567900, id=4, username=tt}
        // }
    }