mybatis返回key value map集合方式
mybatis返回key value map集合
XML:文件
<select id="getData" resultMap="userMap" > ? ? SELECT id,name FROM user ? ? ? ? </select> ? <resultMap id="userMap" type="java.util.Map" > ? ? <result column="id" property="key" jdbcType="VARCHAR" /> ? ? <result column="name" property="value" jdbcType="VARCHAR" /> </resultMap>
Service實(shí)現(xiàn)類: namespaceXml根據(jù)自己xml命名取
import org.apache.ibatis.session.SqlSession;
@Autowired
private SqlSession sqlSession;
public Map<String, String> getBasicInformationOfCompanyPersonnel(String status) throws Exception {
Map<String, Object> params = new HashMap<>();
?
? ? params.put("status", status);?
? ? MapResultHandler handler = new MapResultHandler();?
? ? Map result = handler.getMappedResults();?
? ? sqlSession.select("namespaceXml.getData",params,handler);?
? ? return result;
}工具類:
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
public class MapResultHandler implements ResultHandler {?
? ? @SuppressWarnings("rawtypes")
? ? private final Map mappedResults = new HashMap();?
? ? @SuppressWarnings("unchecked")
? ? @Override
? ? public void handleResult(ResultContext context) {
? ? ? ? @SuppressWarnings("rawtypes")
? ? ? ? Map map = (Map) context.getResultObject();
? ? ? ? // xml配置里面的property的值,對應(yīng)的列
? ? ? ? mappedResults.put(map.get("key"), map.get("value"));
? ? }
?
? ? @SuppressWarnings("rawtypes")
? ? public Map getMappedResults() {
? ? ? ? return mappedResults;
? ? }
}mybatis返回map,key為主鍵,value為對象
@MapKey("id")
public Map<String,User> getUserByName(String name);xml中
<select id="getUserByName" resultType = "User">
? ? select * from user where name=#{param1}
</select>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Mybatis詳解動(dòng)態(tài)SQL以及單表多表查詢的應(yīng)用
MyBatis的動(dòng)態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語句中實(shí)現(xiàn)某些邏輯,下面這篇文章主要給大家介紹了關(guān)于Mybatis超級(jí)強(qiáng)大的動(dòng)態(tài)SQL語句的相關(guān)資料,需要的朋友可以參考下2022-06-06
使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring
這篇文章主要介紹了使用IntelliJ IDEA 2017.2.5 x64中的Spring Initializr插件快速創(chuàng)建Spring Boot/Cloud工程(圖解),需要的朋友可以參考下2018-01-01
java_IO向文件中寫入和讀取內(nèi)容代碼實(shí)例
這篇文章主要介紹了java_IO向文件中寫入和讀取內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Springboot實(shí)現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了Springboot實(shí)現(xiàn)發(fā)送郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Java網(wǎng)絡(luò)編程之UDP實(shí)現(xiàn)原理解析
UDP實(shí)現(xiàn)通信非常簡單,沒有服務(wù)器,每個(gè)都是客戶端,每個(gè)客戶端都需要一個(gè)發(fā)送端口和一個(gè)接收端口,本文給大家介紹Java網(wǎng)絡(luò)編程之UDP實(shí)現(xiàn)原理解析,感興趣的朋友一起看看吧2021-09-09

