mybatis返回HashMap结果类型与映射
- <!-- 返回HashMap结果 类型-->
- <!-- 如果想返回JavaBean,只需将resultType设置为JavaBean的别名或全限定名 -->
- <!-- TypeAliasRegistry类初始化时注册了一些常用的别名,如果忘记了别名可以在这里面查看 -->
- <select id="selectBlogRetHashMap" parameterType="int" resultType="map">
- SELECT id AS "id", title AS "title", content AS "content" FROM Blog WHERE id = #{id}
- </select>
测试代码:
- /**
- * 测试返回HashMap
- */
- @SuppressWarnings("unchecked")
- @Test
- public void testSelectBlogRetHashMap() {
- SqlSession session = sqlSessionFactory.openSession();
- HashMap<String,Object> blog = (HashMap<String,Object>) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogRetHashMap", 15);
- session.close();
- System.out.println(blog.get("title"));
- }
ibatis高级映射:
表结构:
- create table BLOG
- (
- ID NUMBER(20),
- TITLE VARCHAR2(50),
- CONTENT VARCHAR2(4000),
- BLOG_AUTHOR_ID NUMBER(20)
- )
- create table AUTHOR
- (
- ID NUMBER(20),
- AUTHOR_NAME VARCHAR2(50)
- );
- create table POSTS
- (
- ID NUMBER(20),
- SUBJECT VARCHAR2(50),
- BODY VARCHAR2(4000),
- BLOG_ID NUMBER(20)
- )
bean信息:
- package cn.enjoylife.domain;
- import java.util.List;
- public class Blog {
- private Integer id;
- private String title;
- private String content;
- private Author author;
- private List<Post> posts/*=new ArrayList<Post>()*/;
- public List<Post> getPosts() {
- return posts;
- }
- public void setPosts(List<Post> posts) {
- this.posts = posts;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public Author getAuthor() {
- return author;
- }
- public void setAuthor(Author author) {
- this.author = author;
- }
- @Override
- public String toString() {
- return "Blog [content=" + content + ", id=" + id + ", title=" + title
- + "]";
- }
- }
- package cn.enjoylife.domain;
- public class Author {
- private Integer id;
- private String name;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- // System.out.println("Author[id="+id+",name="+name+"]");
- return "Author[id="+id+",name="+name+"]";
- }
- }
- package cn.enjoylife.domain;
- public class Post {
- private Integer id;
- private String subject;
- private String postContent;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public String getPostContent() {
- return postContent;
- }
- public void setPostContent(String postContent) {
- this.postContent = postContent;
- }
- @Override
- public String toString() {
- return "Post [postContent=" + postContent + ", id=" + id + ", subject=" + subject
- + "]";
- }
- }
Blog.xmll配置:
- <!-- 高级结果映射 -->
- <!--
- 一对一关联 嵌套查询 应用select属性
- 1)id="selectBlogAndRelatedAuthor" 中的 blog_author_id 对应到
- <association property="author" column="blog_author_id"/>中的column="blog_author_id"
- 2)我们执行selectBlogAndRelatedAuthor 1次之后,所产生的每条记录都要再进行一个查询来获取author信息(N),
- 这就是N+1问题,应该使用延迟加载的方式,否则这有可能产生致命的后果。
- 3)对于一对一关联,我设置了
- <settings>
- <setting name="lazyLoadingEnabled" value="true"/>
- </settings>
- 却没有对Author进行延迟加载,不知为何。。。
- -->
- <resultMap id="blogResult" type="Blog" >
- <association property="author" column="blog_author_id"
- javaType="Author" select="selectAuthor" />
- </resultMap>
- <select id="selectBlogAndRelatedAuthor" parameterType="int" resultMap="blogResult" >
- SELECT id,title,content,blog_author_id FROM blog WHERE id = #{id}
- </select>
- <select id="selectAuthor" parameterType="int" resultType="Author">
- SELECT id,author_name as "name" FROM author WHERE id = #{id}
- </select>
- <!--
- 一对一关联 嵌套结果
- -->
- <resultMap id="blogResult2" type="Blog">
- <id property="id" column="id" />
- <result property="title" column="title"/>
- <association property="author" column="blog_author_id"
- javaType="Author">
- <id property="id" column="author_id"/>
- <result property="name" column="author_name"/>
- </association>
- </resultMap>
- <select id="selectBlogAndRelatedAuthor2" parameterType="int" resultMap="blogResult2" >
- SELECT t.ID, t.TITLE, t.CONTENT, a.id as "author_id", a.author_name
- FROM blog t
- INNER JOIN author a ON t.BLOG_AUTHOR_ID = a.ID
- AND t.ID = #{id}
- </select>
- <!--
- 一对多关联 嵌套查询,应用select属性
- <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
- select="selectPosts"/>中的column指得是Post所对应表中的引用的主表中的主键id,
- 注意:这个column指的是主表(Blog)中的id,我在这犯了个错,写为Post所对应表中的外键id,这是不对的,
- 应为所引用主表的主键id。
- property="posts" javaType="ArrayList" ofType="Post" 指属性posts为元素为Post的ArrayList类型
- 同样没有进行延迟加载,不知为何。。。
- -->
- <resultMap type="Blog" id="blogResult3" >
- <id property="id" column="id"/>
- <result property="title" column="title"/>
- <result property="content" column="content"/>
- <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
- select="selectPosts"/>
- </resultMap>
- <select id="selectBlogAndRelatedPosts" parameterType="int" resultMap="blogResult3" >
- SELECT id, title, content FROM blog WHERE id = #{id}
- </select>
- <select id="selectPosts" parameterType="int" resultType="Post" >
- SELECT p.id,p.subject,p.body as "postContent" FROM posts p WHERE p.blog_id =#{id}
- </select>
- <!--
- 一对多关联 嵌套结果,在使用这种方式时,sql语句应该使用别名以保证不重复,如果不这样,可能
- 出现结果不正确的现象,比如以下的post_id别名
- -->
- <resultMap type="Blog" id="blogResultVersion" >
- <id property="id" column="id" />
- <result property="title" column="title"/>
- <result property="content" column="content"/>
- <collection property="posts" javaType="ArrayList" ofType="Post" column="id">
- <id property="id" column="post_id"/>
- <result property="subject" column="subject"/>
- <result property="postContent" column="postContent"/>
- </collection>
- </resultMap>
- <select id="selectBlogAndRelatedPosts2" parameterType="int" resultMap="blogResultVersion">
- SELECT t.id, t.title, t.content, p.id as "post_id", p.subject, p.BODY as "postContent"
- FROM blog t
- left outer JOIN posts p ON t.id = p.blog_id
- WHERE t.ID = #{id}
- </select>
测试代码:
- /**
- * 测试一对一关联 嵌套查询
- */
- @Test
- public void testSelectBlogAndRelatedAuthor() {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor", 15);
- System.out.println(blog.toString());
- session.close();
- System.out.println(blog.getAuthor());
- }
- /**
- * 测试一对一关联 嵌套结果
- */
- @Test
- public void testSelectBlogAndRelatedAuthor2() {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor2", 15);
- session.close();
- System.out.println(blog.toString());
- System.out.println(blog.getAuthor());
- }
- /**
- * 测试一对多关联 嵌套查询
- */
- @Test
- public void testSelectBlogAndRelatedPosts() throws Exception {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts", 15);
- System.out.println(blog.toString());
- session.close();
- System.out.println(blog.getPosts());
- }
- /**
- * 测试一对多关联 嵌套结果
- */
- @Test
- public void testSelectBlogAndRelatedPosts2() throws Exception {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts2", 15);
- session.close();
- System.out.println(blog.toString());
- System.out.println(blog.getPosts());
- }
mybatis返回HashMap结果类型与映射的更多相关文章
- 云笔记项目-MyBatis返回自增类型&堆栈对象补充理解
在云笔记项目中,讲到了MySql的自增,MyBatis查询到自增类型数据后可以设置返回到参数属性,其中学习了MySql的自增写法,堆栈对象等知识. MySql数据类型自增 建立一张Person表,其中 ...
- Mybatis返回HashMap时,某个字段值为null时,不会保存key
转载: http://blog.csdn.net/little2z/article/details/38525327 mybatis 的 callSettersOnNulls 问题项目用到mybati ...
- mybatis返回map类型数据空值字段不显示的解决方法
在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...
- MyBatis 返回类型resultType为map时的null值不返回问题
问题一: 查询结果集中 某字段 的值为null,在map中不包含该字段的key-value对 解决:在mybatis.xml中添加setting参数 <!-- 在null时也调用 sett ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
- 笔记:MyBatis Mapper XML文件详解 - 映射和参数
MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...
- Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名
每次使用mybatis的时候,简单的连表查询,用Map接收的时候,都是像DB定义的字段一样,类似以下 student_name,student_id,没有转换为驼峰,但是又不能因为这一个定义一个jav ...
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap[转]
上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...
随机推荐
- grunt安装失败处理
1.官网 Grunt官网 http://gruntjs.com 2.前言 前段时间一不小心升级了win10(万恶的360),各种不适应各种问题各种软件bug,最终决定回退到win7,然后悲催的发现系统 ...
- 学习笔记--Grunt、安装、图文详解
学习笔记--Git安装.图文详解 安装Git成功后,现在安装Gruntjs,官网:http://gruntjs.com/ 一.安装node 参考node.js 安装.图文详解 (最新的node会自动安 ...
- 文件夹和文件、Path类、流、序列化
循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...
- 嘿嘿,JAVA里第一次运行单元测试成功,立存
按书上写的单元测试. 居然一次过,爽!!! package org.smart4j.chapter2.test; import java.util.HashMap; import java.util. ...
- httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数
一.利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单 package com.test.httpclient; imp ...
- *[hackerrank]Girlfriend & Necklace
https://www.hackerrank.com/contests/w8/challenges/gneck 有点意思.是DP,最优解包含最优子问题.F(X)=F(X-1)+F(X-3).因为F(X ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-003Dao层
1. package com.sanqing.dao; import java.util.List; import com.sanqing.po.Student; public interface S ...
- App应用与思考
我为什么没有加入苹果的iOS APP移动大军?http://blog.csdn.net/Code_GodFather/article/details/7956858 ----------------- ...
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- Struts2的简单案例
第一步:首先下载struts2的架包(链接地址:http://download.csdn.net/detail/ahy1024/4626028) 第二步:新建web project项目 DemoStr ...