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 ...
随机推荐
- ***解决PHP输出多余的空格或换行
用CI框架写APP后台接口的时候,返回的JSON前面有多余的2哥换行,首先排查的是BOM,结果问题依旧 再就是排查<?php ?> 标签外没有多余的回车.换行,结果发现确实有多余的换行,去 ...
- POJ 2240 Arbitrage(floyd)
http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...
- DJANGO变动库的一次真实手动经历
在变更库时,由于对字段规划和约束性没考虑完全,需要手工操作数据库,以便可以重复执行. 有以下三点要注意. 1,先迎合错误输出,增删对应的表或字段. 2,必要时,修改migrations文件,以去除唯一 ...
- lintcode 中等题:subSets 子集
题目 子集 给定一个含不同整数的集合,返回其所有的子集 样例 如果 S = [1,2,3],有如下的解: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], ...
- lintcode:Unique Characters 判断字符串是否没有重复字符
题目: 判断字符串是否没有重复字符 实现一个算法确定字符串中的字符是否均唯一出现 样例 给出"abc",返回 true 给出"aab",返回 false 挑战 ...
- PHP魔术方法小结.md
说明 魔术方法就是在特定场景下不需要调用而自动执行的方法.因为有魔术方法,所以我们的类可以写得很灵活~ __construct #构造方法,在类被实例化时自动调用,一般用于初始化操作; __destr ...
- 阿里巴巴fastJson
FastJson解析 一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java ...
- darwin转发时,摄像机在3G和4G模式下的参数设置
darwin转发时,摄像机在3G和4G模式下的参数设置 我们转发的是摄像机的子码流,因为在不同的网络环境下,为了达到当前网络环境下最清晰,最流畅的目标,在转发前要根据使用的是3G还是4G及信号强度来自 ...
- Java学习笔记之:Java数组
一.介绍 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如number ...
- *windows下安装以及配置nginx
1.从nginx官网下载相应的安装包. http://nginx.org/