`
53873039oycg
  • 浏览: 837174 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Mybatis返回Map,List<Map>

阅读更多

            上次写[简单]Spring_Mybatis_CRUD简单示例(带数据库)遇到一个问题,在返回Map类型时候没有解析正确,不得不返回一个JavaBean,趁着有空,重新看了下,现在可以用Mybatis返回Map,List<Map>了

            返回Map,Mybatis配置如下

           

<select id="getCountyHashMap" resultType="java.util.HashMap">
		select name,id from
		tsql_test_region where
		id=#{id}
	</select>

    ServiceImpl如下

  

public Map<String, Long> getCountyHashMap(long id) {
		Map<String, Object> regionMap = regionInfoMapper.getCountyHashMap(id);
		Map<String, Long> resultMap = new HashMap<String, Long>();
		String region = null;
		Long vid = null;
		for (Map.Entry<String, Object> entry : regionMap.entrySet()) {
			if ("NAME".equals(entry.getKey())) {
				region = (String) entry.getValue();
			} else if ("ID".equals(entry.getKey())) {
				vid = ((java.math.BigDecimal) entry.getValue()).longValue();
			}
		}
		resultMap.put(region, vid);
		return resultMap;
	}

    Controller如下:

  

@RequestMapping(value = "/region3", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, Long> getCountyMap(@RequestParam(required = true) int regionId) {
		return regionInfoService.getCountyHashMap(regionId);
	}

    结果为

 

   

     返回List<Map>类似:

     Mybatis配置:

  

<select id="getRegionHashMap" resultType="java.util.HashMap">
		select name,id from
		tsql_test_region order by id
	</select>

   ServiceImpl如下:

  

public Map<String, Long> getRegionHashMap() {
		List<Map<String, Object>> regionMap = regionInfoMapper
				.getRegionHashMap();
		Map<String, Long> resultMap = new HashMap<String, Long>();
		for (Map<String, Object> map : regionMap) {
			String region = null;
			Long id = null;
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if ("NAME".equals(entry.getKey())) {
					region = (String) entry.getValue();
				} else if ("ID".equals(entry.getKey())) {
					id = ((java.math.BigDecimal) entry.getValue()).longValue();
				}
			}
			resultMap.put(region, id);
		}
		return resultMap;
	}

   Controller如下:

  

@RequestMapping(value = "/region2", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, Long> getRegionMap() {
		return regionInfoService.getRegionHashMap();
	}

    结果为

 

   

      本文系原创,转载请注明出处,谢谢

      全文完

   

 

  • 大小: 28.6 KB
  • 大小: 56.4 KB
0
5
分享到:
评论

相关推荐

    SpringBoot+Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名

    public static Map&lt;String, Object&gt; toCamelCaseMap(Map&lt;String, Object&gt; map) { return map.entrySet().stream() .collect(Collectors.toMap( entry -&gt; underscoreToCamelCase(entry.getKey()), entry -&gt; ...

    MyBatis传入集合 list 数组 map参数的写法

    在MyBatis中,处理集合参数如list、array以及map是非常常见的操作。这些参数通常用于构建动态SQL,特别是当需要在`IN`语句中使用多个值时。下面将详细解释如何在MyBatis中使用这些参数类型。 1. **List参数**: 当...

    mybatis动态sql(使用<where>标签来处理多个查询条件)

    MyBatis提供了多种动态SQL标签,如`&lt;if&gt;`、`&lt;choose&gt;`、`&lt;when&gt;`、`&lt;otherwise&gt;`、`&lt;trim&gt;`、`&lt;where&gt;`、`&lt;set&gt;`和`&lt;foreach&gt;`等,这些标签可以用来构建灵活多变的SQL语句。 #### 三、`&lt;where&gt;`标签详解 `&lt;where&gt;`...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    Mybatish和Ajax笔记

    public Map&lt;String, Object&gt; handleAjaxRequest(@RequestParam Map&lt;String, String&gt; params) { // 处理业务逻辑 List&lt;Movie&gt; movies = movieDao.getMovies(params); return Collections.singletonMap("movies", ...

    mybatis 多层级collection嵌套.docx

    &lt;select id="selectH5ListByDeployId" resultMap="jxhJSZBH5ListMap"&gt; &lt;!-- SQL查询语句,获取与Car关联的所有Light对象 --&gt; &lt;/select&gt; ``` 在`Car`对象的`resultMap`中,`&lt;collection&gt;`标签引用了上述`selectH...

    mybatis开发步骤

    -- 无条件查询,查询全部,返回值以键值对形式存在List&lt;Map&lt;String,Object&gt;&gt;中 --&gt; &lt;select id="findAll" resultType="java.util.HashMap" &gt; select id,studentId,studentName from t_student &lt;/select&gt; &lt;!-- ...

    Springboot集成MyBatis自动生成代码工具源码

    List&lt;String&gt; warnings = new ArrayList&lt;&gt;(); boolean overwrite = true; InputStream in = MyBatisGeneratorMain.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ...

    MyBatis学习笔记

    Map&lt;String, Object&gt; params = new HashMap&lt;&gt;(); params.put("name", "张三"); params.put("age", 20); List&lt;Student&gt; students = studentMapper.selectByCondition(params); ``` #### 四、MyBatis与Spring整合 **...

    springboot专栏 007springboot整合mybatis-plus增删改查 ModelAndView jsp 分页

    List&lt;User&gt; users = userService.page(new Page&lt;&gt;(pageNum, 10)).getRecords(); ModelAndView modelAndView = new ModelAndView("userList"); modelAndView.addObject("users", users); modelAndView.addObject...

    Intellij IDEA搭建SSM+Bootstrp+分页

    Map&lt;String, Object&gt; result = new HashMap&lt;&gt;(); result.put("total", userPage.getTotal()); result.put("rows", userPage.getList()); return result; } } ``` 前端页面根据返回的分页数据渲染Bootstrap的...

    springboot项目(四)添加mybatis分页插件

    return new PageInfo&lt;&gt;(users); } } ``` 至此,我们已经成功地在Spring Boot项目中集成了MyBatis分页插件。通过这种方式,开发者可以轻松实现数据库查询的分页功能,提高应用性能,同时降低开发复杂度。在实际...

    Spring Boot集成MyBatis与分页插件

    PageInfo&lt;User&gt; pageInfo = new PageInfo&lt;&gt;(users); return pageInfo; } ``` 总结一下,本教程详细介绍了如何在Spring Boot项目中集成MyBatis,并利用PageHelper分页插件实现数据的分页查询。这包括了添加依赖、...

    springmvcmybatis

    &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;!-- 添加 mybatis-config配置上去。--&gt; &lt;property name=...

    查询返回Map

    List&lt;Map&lt;String, Object&gt;&gt; result = jdbcTemplate.queryForList("SELECT * FROM my_table", new Object[]{}); ``` 接下来,这些Map数据需要传递到JSP页面进行展示。在JSP中,我们可以使用EL(Expression Language...

    Mybatis分页插件 PageHelper5.0.0 使用

    public List&lt;Sys_user&gt; selectByPageCondition(Map&lt;String, Object&gt; map) { int pageNo = Integer.parseInt(map.get("pageNo").toString()); int pageSize = Integer.parseInt(map.get("pageSize").toString()); ...

    springboot整合mybatis,接口返回值利用pagehelper实现分页

    PageInfo&lt;User&gt; pageInfo = new PageInfo&lt;&gt;(users); // 获取分页信息 return new PageResult&lt;&gt;(pageInfo); } } ``` 在Controller层,我们可以将`PageResult`对象转换为JSON格式,通过RESTful接口返回给前端: `...

    MyBatis动态拼接SQL

    MyBatis通过`&lt;if&gt;`, `&lt;choose&gt;`, `&lt;when&gt;`, `&lt;otherwise&gt;`, `&lt;where&gt;`, `&lt;set&gt;`, `&lt;foreach&gt;`等标签来实现动态SQL的构建。 1. `&lt;if&gt;`标签:用于判断某个条件是否成立,如果成立则插入相应的SQL片段。例如,当查询...

    mybatis、mybatis详细设计、mybatis配置

    &lt;select id="selectUsers" parameterType="map" resultType="com.example.model.User"&gt; SELECT * FROM user &lt;where&gt; &lt;if test="name != null"&gt; AND name LIKE #{name} &lt;/if&gt; &lt;if test="age != null and age &gt; ...

    MyBatis 深入浅出-动态SQL

    &lt;select id="findActiveUsers" parameterType="map"&gt; SELECT * FROM users &lt;if test="active == true"&gt; WHERE active = #{active} &lt;/if&gt; &lt;/select&gt; ``` 在这个例子中,只有当`active`参数为`true`时,才会添加...

Global site tag (gtag.js) - Google Analytics