您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Mybatis如何返回Map數(shù)據(jù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Mybatis如何返回Map數(shù)據(jù)”吧!
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); } } }
import org.apache.ibatis.annotations.MapKey; public interface UserMapper { // 指定的key必須是唯一的,否則重復(fù)的重復(fù)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功能,因此只能通過下面的方式進(jìn)行模擬 --> <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); } }
import org.apache.ibatis.annotations.MapKey; public interface UserMapper { // 指定的key名稱必須是User實(shí)體類中的屬性 @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"> <!--由于最終是把數(shù)據(jù)封裝到User實(shí)體類中,所以返回值的數(shù)據(jù)類型是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數(shù)據(jù)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。