mybatis返回map键值对_使用Mybatis返回Map结果集

建表sql语句:

CREATE TABLE `constant` (

`id` bigint(20) NOT NULL AUTO_INCREMENT ,

`key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,

`value` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,

`type` int(10) NULL DEFAULT NULL ,

PRIMARY KEY (`id`)

)

表结构:

表名称为:constant

POJO:

public class Constant {

private Long id;

private String key;

private String value;

private Integer type;

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getKey() {

return key;

}

public void setKey(String key) {

this.key = key == null ? null : key.trim();

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value == null ? null : value.trim();

}

public Integer getType() {

return type;

}

public void setType(Integer type) {

this.type = type;

}

}

DAO层:

import java.util.Map;

import org.apache.ibatis.annotations.MapKey;

import org.springframework.stereotype.Repository;

import com.jm.model.Constant;

@Repository

public interface ConstantDao {

/**

* 注释@MapKey表示表中那个字段作为Map的key

* @return

*/

@MapKey("id")

Map loadConstant();

}

junitTest:

import java.util.Map;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.jm.dao.ConstantDao;

import com.jm.model.Constant;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })

public class MubatisMapTest {

@Autowired

private ConstantDao constantDao;

@Test

public void mapTest() {

Map constantMap = constantDao.loadConstant();

System.out.println(constantMap);

}

}

当Mapper代码为:

select constant.id,constant.key,constant.value,constant.type from constant

执行结果是:

Paste_Image.png

这时查询出来的map的value是map

当Mapper为:

select * from constant

执行结果为:

Paste_Image.png

这时查询出来的map的value是java对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值