mybatis返回值是map_mybatis返回类型为list

mybatis返回值是map_mybatis返回类型为list建表sql语句:CREATETABLE`constant`(`id`bigint(20)NOTNULLAUTO_INCREMENT,`key`varchar(128)CHARACTERSETutf8COLLATEutf8_general_ciNULLDEFAULTNULL,`value`varchar(128)CHARACTERSETutf8CO…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

建表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`)

)

表结构:

mybatis返回值是map_mybatis返回类型为list

表名称为: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

执行结果是:

mybatis返回值是map_mybatis返回类型为list

Paste_Image.png

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

当Mapper为:

select * from constant

执行结果为:

mybatis返回值是map_mybatis返回类型为list

Paste_Image.png

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185931.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PHP经纬度距离计算「建议收藏」

    PHP经纬度距离计算「建议收藏」//根据两点经纬度计算距离//纬度1、纬度2、经度1、经度2functionget_two_point_distance($lat1,$lat2,$lng1,$lng2){$radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度$radLat2=deg2rad($lat2);…

    2022年9月23日
    3
  • Ubuntu安装JDK教程

    Ubuntu安装JDK教程Ubuntu安装JDK教程jdk的下载和安装:1、打开浏览器,输入jdk的官网地址:https://www.oracle.com/java/technologies/javase-downloads.html,如图:2、找到需要下载的jdk版本,这里以1.8版为例,找到JavaSE8,点击JDKDownload,跳转到JavaSE8的下载页面,如图:3、找到对应的下载版本,我这里是Ubuntu64位的系统,需要找到linux64的jdk版本,点击进行下载,如图:4、下载成之后

    2022年7月12日
    19
  • Vue 后台管理系统 Demo

    Vue 后台管理系统 DemoGITHUB 原文地址 https github com lin xin vue manage system 前言该方案作为一套多功能的后台框架模板 适用于绝大部分的后台管理系统 WebManagemen 开发 基于 vue js 使用 vue cli3 脚手架 引用 ElementUI 组件库 方便开发快速简洁好看的组件 分离颜色样式 支持手动切换主题色 而且很方便

    2026年2月1日
    0
  • 新版Pycharm中Matplotlib图像不在弹出独立的显示窗口「建议收藏」

    新版Pycharm中Matplotlib图像不在弹出独立的显示窗口「建议收藏」官方说明链接:https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-in-PyCharm-2017-3-reduces-functionality-of-Matplotlibpycharm从2017.3版之后,将matplotlib的绘图的结果默认显示在SciView窗口中…

    2022年8月27日
    7
  • HTTP.SYS远程代码执行漏洞测试

    HTTP.SYS远程代码执行漏洞测试今天中午收到命令让下午两点出一个网站的测试报告 这么急也没详细的测试就用awvs跑了一下 没想到跑出来一个高危HTTP.SYS远程代码执行漏洞测试开始跑出来的时候很兴奋 一个高危也不错了   但是验证的时候可给我愁坏了这里我非常推荐这篇博客 太详细了http://www.cnblogs.com/peterpan0707007/p/8529261.html然后我就想着用…

    2022年7月25日
    14
  • 彩票APP将演绎“快鱼吃慢鱼”的发展轨迹

    彩票APP将演绎“快鱼吃慢鱼”的发展轨迹

    2021年11月16日
    31

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号