尚硅谷MyBatis3_select

select 的返回值类型比较多,所以考虑的情况也相对复杂。这里记录一下查询相关知识。

查询的时候 mapper 中没有指定 ResultType 或者 ResultMap,就会在执行 sql 语句的时候出现问题。

image

  • ResultType 是结果类型,设置默认的映射关系
  • ResultMap 是结果映射,设置自定义的映射关系,如果数据库表中字段与 JavaBean 的属性名不一致,需要用 ResultMap

也就是说我们需要指定返回的类型才能和接口指定的类型对上。

  • 接口

        /**
         * 根据id查询用户信息
         */
        User getUserById();
    
        /**
         * 查询所有的用户信息
         */
        List<User> getAllUser();
    
  • mapper

        <!--User getUserById();-->
        <select id="getUserById" resultType="com.atguigu.mybatis.pojo.User">
            select *
            from t_user
            where id = 3
        </select>
        <!--List<User> getAllUser();-->
        <select id="getAllUser" resultType="com.atguigu.mybatis.pojo.User">
            select *
            from t_user
        </select>
    
posted @ 2023-03-19 15:31  ShaunY  阅读(12)  评论(0编辑  收藏  举报