ibatis返回HashMap结果类型与映射

测试代码:/** * 测试返回HashMap */ @SuppressWarnings("unchecked") @Test public void testSelectBlogRetHashMap() { SqlSession session = sqlSessionFactory.openSession(); HashMap blog = (HashMap) 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 posts/*=new ArrayList()*/; public List getPosts() { return posts; } public void setPosts(List 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配置: 测试代码:/** * 测试一对一关联 嵌套查询 */ @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()); }

你可能感兴趣的:(Java,开发)