首页 > 其他分享 >尚硅谷MyBatis3_select

尚硅谷MyBatis3_select

时间:2023-03-19 15:46:33浏览次数:40  
标签:MyBatis3 映射 ResultMap 查询 硅谷 id 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>
    

标签:MyBatis3,映射,ResultMap,查询,硅谷,id,select
From: https://www.cnblogs.com/ShaunY/p/17233350.html

相关文章