欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

# Mybatis返回Map集合数据

程序员文章站 2022-06-30 20:48:02
...

Mybatis返回Map集合数据

entity—— Easyuser

package com.example.test1.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Easyuser {
    private int id;
    private String  username;
    private String password;
    private String email;
    private String role;
    private boolean state;
}

mapper——EasyMapper

package com.example.test1.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.test1.entity.Easyuser;
import org.apache.ibatis.annotations.MapKey;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface EasyUserMapper extends BaseMapper<Easyuser> {
    //返回单条Map  @MapKey("id")用于设置Map中的key
    @MapKey("id")
    Map<Integer, Easyuser> selectByMap(Integer id);

    //返回多条Map
    @MapKey("id") @MapKey("id")用于设置Map中的key
    Map<Integer, Easyuser> searchMap(List<Integer> list);
}

mapper.xml——EasyMapper.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.test1.mapper.EasyUserMapper">

    <resultMap id="BaseResultMap" type="com.example.test1.entity.Easyuser">
       <result property="id" column="id"></result>
        <result property="username" column="username"></result>
    </resultMap>
<!--BaseResultMap来映射我们Map集合中的value值,存的是对象类型-->
    <select id="selectByMap" resultMap="BaseResultMap" resultType="java.util.HashMap">
        select  * from easyuser where id=#{id}
    </select>

    <select id="searchMap"  resultMap="BaseResultMap" resultType="java.util.HashMap" >
        select * from easyuser where id in
        <foreach collection="list" item="id" index="index"  open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>

测试Test

package com.example.test1;

import com.example.test1.entity.Easyuser;
import com.example.test1.mapper.EasyUserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootTest
class Test1ApplicationTests {
    @Autowired
    EasyUserMapper easyUserMapper;
    @Test
    void contextLoads() {

        System.out.println("***********单条map******************");
        Map<Integer, Easyuser> map = easyUserMapper.selectByMap(1);
        System.out.println(map);
        System.out.println("***********多条map******************");
        List<Integer> list= Arrays.asList(1,2,3);
        Map<Integer, Easyuser> maps = easyUserMapper.searchMap(list);
        System.out.println(maps);
    }
}

输出结果


***********单条map******************
2021-04-14 19:25:29.340  INFO 13692 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
{1=Easyuser(id=1, username=admin, password=123456, email=[email protected], role=超级管理员, state=true)}
***********多条map******************
{1=Easyuser(id=1, username=admin, password=123456, email=[email protected], role=超级管理员, state=true), 
2=Easyuser(id=2, username=naughty, password=888888, email=[email protected], role=普通管理员, state=false), 
3=Easyuser(id=3, username=flysky01, password=987654, email=[email protected], role=普通用户, state=true)}
相关标签: java