mybatis 11: 通过map获取入参和返回值

2年前 (2022) 程序员胖胖胖虎阿
114 0 0

1. 通过指定参数位置获取

作用

  • 如果入参是多个且实体类无法封装所有的入参,可以通过指定参数位置进行传参,方便对多个参数进行获取

用法

接口

    //指定参数位置
    List<User> getByBirthday(Date start, Date  end);

sql标签

    <!--
        //指定参数位置
        List<User> getByBirthday(Date start, Date  end);
    -->

    <select id="getByBirthday" resultType="user">
        select
            <include refid="allColumns"/>
        from
            users
        where
            birthday between #{arg0} and #{arg1}
    </select>
  • 在当前较高版本中,根据参数位置引用参数时,参数名的写法规定为:arg0,arg1....以此类推

测试

    //指定参数位置测试
    @Test
    public void testGetByBirthday() throws ParseException {
        List<User> users = usersMapper.getByBirthday(
                date.parse("2001-01-01"),
                date.parse("2002-08-23"));
        users.forEach(System.out::println);
    }

输出

==> Preparing: select id, username, birthday, sex, address from users where birthday between ? and ?
==> Parameters: 2001-01-01 00:00:00.0(Timestamp), 2002-08-23 00:00:00.0(Timestamp)
<==    Columns: id, username, birthday, sex, address
<==        Row: 1, 荷包蛋, 2002-08-23, 女, 黑河市
<==        Row: 2, 小王, 2001-07-12, 1, 芜湖市
<==        Row: 5, 段, 2001-03-10, 1, 太原
<==        Row: 6, 范成群, 2002-01-19, 1, 鲅鱼圈
<==        Row: 7, 学委, 2001-05-13, 2, 平顶山市
<==        Row: 31, 西决2, 2001-01-01, 男, 北京
<==        Row: 32, 南音2, 2002-02-02, 女, 北京
<==      Total: 7

缺点

  • 用arg0,arg1等来定位参数时,语义不明确

2. 用map来传递入参

作用

  • 既可以确保参数在存取时的语义,又可以简便的存取参数

用法

接口

    //入参类型是map
    List<User> getByMap(Map<Object, Object> map);

sql标签

    <!--
        //入参类型是map
        List<User> getByMap(Map map);
    -->
    <select id="getByMap" resultType="user">
        select
            <include refid="allColumns"/>
        from
            users
        where
            birthday between #{birthdayBegin} and #{birthdayEnd}
    </select>
  • 通过传入的map的key来获取map中的变量值

测试

    @Test
    public void testGetByMap() throws ParseException {
        Date birthdayBegin = date.parse("2001-01-01");
        Date birthdayEnd = date.parse("2002-08-23");
        Map<Object, Object> birthdayMap = new HashMap<>();
        birthdayMap.put("birthdayBegin", birthdayBegin);
        birthdayMap.put("birthdayEnd", birthdayEnd);

        List<User> users = usersMapper.getByMap(birthdayMap);
        users.forEach(System.out::println);
    }

输出

==>  Preparing: select id, username, birthday, sex, address from users where birthday between ? and ?
==> Parameters: 2001-01-01 00:00:00.0(Timestamp), 2002-08-23 00:00:00.0(Timestamp)
<==    Columns: id, username, birthday, sex, address
<==        Row: 1, 荷包蛋, 2002-08-23, 女, 黑河
<==        Row: 2, 小王, 2001-07-12, 1, 芜湖市
<==        Row: 5, 段, 2001-03-10, 1, 太原
<==        Row: 6, 范成群, 2002-01-19, 1, 鲅鱼圈
<==        Row: 7, 学委, 2001-05-13, 2, 平顶山市
<==        Row: 31, 西决2, 2001-01-01, 男, 北京
<==        Row: 32, 南音2, 2002-02-02, 女, 北京
<==      Total: 7

返回值是map

背景

mybatis 11: 通过map获取入参和返回值

  • 当需要从多张表的字段中获取数据时,没有事先写好的实体类来封装查询到的数据,可以用map作为容器来盛放零碎的数据
    • 各个表的字段的组合可能性太多,提前定义实体类不现实也没有必要

用法

接口

    //返回一行map
    Map<Object, Object> getOneMap(Integer id);

    //返回多行map
    List<Map<Object, Object>> getManyMap();

sql标签

    <!--
        //返回一行map
        Map<Object, Object> getOneMap(Integer id);
    -->
    <select id="getOneMap" parameterType="int" resultType="map">
        select
            username, address
        from
            users
        where
            id=#{id}
    </select>


    <!--
        //返回多行map
        List<Map<Object, Object>> getManyMap();
    -->
    <select id="getManyMap" resultType="map">
        select
            username, address
        from
            users
    </select>
  • 对于返回多行map的查询操作,查询到的每条记录都封装到一个map中,当有多个map时,这些map被封装到最外层的list中
    • 这里的每个map都相当于原先查询数据时,每条数据都封装成一个实体类,再把所有实体类封装到list中返回,原理一样

测试

    //返回一行map测试
    @Test
    public void testGetOneMap(){
        Map<Object, Object> map = usersMapper.getOneMap(1);
        System.out.println(map);
    }

    //返回多行测试
    @Test
    public void testGetManyMap(){
        List<Map<Object, Object>> maps = usersMapper.getManyMap();
        maps.forEach(map -> {
            map.forEach((key, val) -> {
                System.out.println(key + " : " + val);
            });
            System.out.println("-----------------------------");
        });
    }

输出

//返回一行map输出

==>  Preparing: select username, address from users where id=?
==> Parameters: 1(Integer)
<==    Columns: username, address
<==        Row: 荷包蛋, 黑河
<==      Total: 1
{address=黑河, username=荷包蛋}


//返回多行map输出

==>  Preparing: select username, address from users
==> Parameters: 
<==    Columns: username, address
<==        Row: 荷包蛋, 黑河
<==        Row: 小王, 芜湖市
<==        Row: 段, 太原
<==        Row: 范成群, 鲅鱼圈
<==        Row: 学委, 平顶山市
<==        Row: 逻辑, 上海
<==        Row: 小青, 沈阳市
<==        Row: 西决2, 北京
<==        Row: 南音2, 北京
<==        Row: 北北2, 北京
<==      Total: 10
address : 黑河
username : 荷包蛋
-----------------------------
address : 芜湖市
username : 小王
-----------------------------
address : 太原
username : 段
-----------------------------
address : 鲅鱼圈
username : 范成群
-----------------------------
address : 平顶山市
username : 学委
-----------------------------
address : 上海
username : 逻辑
-----------------------------
address : 沈阳市
username : 小青
-----------------------------
address : 北京
username : 西决2
-----------------------------
address : 北京
username : 南音2
-----------------------------
address : 北京
username : 北北2
-----------------------------
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@77a98a6a]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@77a98a6a]
Returned connection 2007599722 to pool.

Process finished with exit code 0
版权声明:程序员胖胖胖虎阿 发表于 2022年9月5日 下午2:08。
转载请注明:mybatis 11: 通过map获取入参和返回值 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...