首頁 > 軟體

Mybatis返回Map資料方式範例

2022-06-06 22:03:25

一. 方式1

介面

public interface UserMapper {

    List<Map<String, String>> selectTestData1();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.UserMapper">

    <select id="selectTestData1" resultType="java.util.Map">
        SELECT
          t_user.id as id,
	      t_user.email as email,
	      t_user.avatar as avatar
        FROM
	      t_user
    </select>
</mapper>

呼叫

@Service
public class MapTest implements CommandLineRunner {

    @Autowired
    private UserMapper mapper;

    @Override
    public void run(String... args) throws Exception {

        List<Map<String, String>> listData1 = mapper.selectTestData1();
        for (Map<String, String> map : listData1) {
            System.out.println(map);
        }
    }
}

二. 方式2

介面

import org.apache.ibatis.annotations.MapKey;

public interface UserMapper {

    // 指定的key必須是唯一的,否則重複的重複map的key會覆蓋,如果查詢的欄位中沒有唯一值,可以通過rowno來指定
    @MapKey("rowno")
    Map<String, Map<String,String>> selectTestData2();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.UserMapper">
	<!--
		❗❗❗由於mysql沒有oracle中的ROWNUM功能,因此只能通過下面的方式進行模擬
	-->
    <select id="selectTestData2" resultType="java.util.Map">
        SELECT
	      @rowno := @rowno + 1 AS rowno,
	      t_user.email AS email,
	      t_user.avatar AS avatar
        FROM
	      t_user,
	      ( SELECT @rowno := 0 ) t
        ORDER BY
	      rowno DESC
    </select>
</mapper>

呼叫

@Service
public class MapTest implements CommandLineRunner {

    @Autowired
    private UserMapper mapper;

    @Override
    public void run(String... args) throws Exception {

        Map<String, Map<String, String>> mapData1 = mapper.selectTestData2();
        System.out.println(mapData1);
    }
}

三. 方式3

介面

import org.apache.ibatis.annotations.MapKey;

public interface UserMapper {

    // 指定的key名稱必須是User實體類中的屬性
    @MapKey("id")
    Map<String, User> selectTestData3();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.mapper.UserMapper">

    <!--由於最終是把資料封裝到User實體類中,所以返回值的資料型別是User而不是Map-->
    <select id="selectTestData3" resultType="com.example.demo.transactionCom.entity.User">
        SELECT
          t_user.id as id,
          t_user.email as email,
          t_user.avatar as avatar
        FROM
          t_user
    </select>
</mapper>

呼叫

@Service
public class MapTest implements CommandLineRunner {

    @Autowired
    private UserMapper mapper;

    @Override
    public void run(String... args) throws Exception {

        Map<String, User> mapData2 = mapper.selectTestData3();
        Set<Map.Entry<String, User>> entries = mapData2.entrySet();
        for (Map.Entry<String, User> entry : entries) {
            User user = entry.getValue();
            System.out.println(user);
        }
    }
}

以上就是Mybatis返回Map資料方式範例的詳細內容,更多關於Mybatis返回Map資料的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com