- 浏览: 1255422 次
- 性别:
- 来自: 常州
文章分类
- 全部博客 (499)
- java (101)
- linux (82)
- mysql (30)
- javascript (45)
- Oracle (12)
- ext (14)
- 虚拟机 (1)
- 搜索引擎 (2)
- struts2 (11)
- 设计模式 (9)
- nginx (17)
- tomcat (12)
- 随想 (10)
- spring (18)
- svn (1)
- flash (3)
- UML (1)
- 数据结构 (7)
- 算法 (2)
- 网摘 (9)
- 数据库 (15)
- ibatis (3)
- jquery (31)
- lucene (1)
- hibernate (14)
- Myeclipse (4)
- 线程 (7)
- jbpm (4)
- 重构 (1)
- mantis (3)
- MediaWiki (4)
- ExtMail (1)
- MDaemon (1)
- egit (1)
- dwr (7)
- sitemesh (2)
- mybatis (1)
- ico (1)
- hadoop (5)
- jsoup (1)
- urlrewrite (2)
- jstl (1)
- spring3 (2)
- aop (2)
- 定时器 (1)
- Quartz (2)
- apache (1)
- php (1)
- security (1)
- iptables (2)
- QQ (1)
- mysqldump (1)
- vim (1)
- memcached (4)
- jad (1)
- 微博 (1)
- html5 (1)
- css3 (1)
- httpclient (10)
- google (1)
- shortUrl (1)
- json (2)
- virtualBox (1)
- mantisBT (2)
- htmlunit (1)
- selenium (2)
- mail (1)
- 正则表达式 (4)
- html (3)
- css (2)
- jatoolsPrinter (1)
- 图片处理 (1)
- hql (1)
- webservice (1)
- 分词 (3)
- 短信 (1)
- VPS (1)
- 事务 (1)
- 广告 (1)
- 画廊 (1)
- git (3)
- github (1)
- openshift (1)
- 缓存 (1)
- web (3)
- android (3)
- c3p0 (1)
- 邮箱 (1)
- memcache (2)
- windows (2)
- js (14)
- 编辑器 (1)
- 打印 (1)
- centos (5)
- boneCP (1)
- 连接池 (1)
- sql (1)
- nosql (1)
- MongoDB (1)
- 浏览器 (1)
- node (1)
- node.js (1)
- backbone.js (1)
- lazyload (1)
- Switch Off (1)
- Titanium (1)
- 网站架构 (1)
- WebDriver (1)
- APJP (1)
- 代理 (1)
- comet (1)
- kendoui (1)
- UI (2)
- 互联网 (1)
- localStorage (1)
- 记录 (1)
- 微信 (2)
- Sphinx (1)
- netty (1)
- js,mvvm,Avalon (1)
- 安卓 (1)
- Tengine (1)
- 大数据 (1)
- 手机 (1)
- paypal (1)
- SaaS (1)
- gitlab (1)
- nodejs (1)
- React (1)
- shadowsocks (0)
- vpn (0)
- 验证码 (1)
- SSL (2)
- SEO (1)
- IntelliJ (1)
- 敏捷开发 (1)
- 项目管理 (1)
- 爬虫 (1)
- 正则 (1)
- owncloud (1)
- 云存储 (1)
- ajax (1)
- pjax (1)
- jdk (1)
- zookeeper (1)
- phantomjs (1)
- ELK (1)
- springcloud (1)
- IDEA (1)
- hexo (1)
- ss (1)
- letencrypt (1)
最新评论
-
peakandyuri:
这个是有BUG的,数字小体现不出来,数字大了就不对了,但是Ja ...
java十进制转换N进制并反转换的工具类 -
ginolai:
然后是相关配置:/etc/sysconfig/iptables ...
Linux中iptables设置详细 -
bzhao:
我测试没啥区别啊!
Thread.sleep()和Thread.currentThread().sleep()区别 -
zhl549342097:
match == false
Spring Security 3.1 中功能强大的加密工具 PasswordEncoder -
hellotieye:
renzhengzhi 写道drager 写道用jsoup后解 ...
jsoup select 选择器
Xhtml代码
- <!-- 返回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>
Java代码
- /**
- * 测试返回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高级映射:
表结构:
Java代码
- 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)
- )
Java代码
- 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
- + "]";
- }
- }
Java代码
- 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+"]";
- }
- }
Java代码
- 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
- + "]";
- }
- }
Xhtml代码
- <!-- 高级结果映射 -->
- <!--
- 一对一关联 嵌套查询 应用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>
Java代码
- /**
- * 测试一对一关联 嵌套查询
- */
- @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());
- }
相关推荐
5. Executor执行器要处理的SQL信息是封装到一个底层对象MappedStatement中 该对象包括SQL语句 输入参数映射信息 输出结果映射信息 其中输入参数和输出结果的映射类型包括HashMap集合对象 POJO对象类型
HashMap关系数据映射技术软件(简称PVO_v1.1)已通过国家版权局著作权登记,自公布之日起,任何单位和个人均可免费使用。PVO软件包可用于简化JDBC数据库编程,同传统的数据库编程技术相比,可以将数据库编程效率提高数倍...
Mybatis
主要介绍了mybatis使用char类型字段查询oracle数据库时结果返回null问题的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
mybatis – MyBatis 3 _ XML 映射器.html
使用Mybatis-Generator帮助生成数据库映射文件使用文档和使用插件, 使用Mybatis-Generator帮助生成数据库映射文件使用文档和使用插件
Mybatis 处理 CLOB、BLOB 类型数据
mybatis返回int会报错,改成Integer封装类型可以解决,具体参考我的博客
“Mybatis课程实验报告及源码-实验名称:Mybatis完成一对一关系映射(可不用修改直接使用).zip” 是一份关于Mybatis框架实验的报告,附带了完整的源代码和示例项目。以下是对这份实验报告的详细描述: 这份实验...
Mybatis关联映射 Mybatis关联映射 Mybatis关联映射 Mybatis关联映射 Mybatis关联映射
Redis应用场景、Spring框架、RabbitMQ、Java线程、Mybatis框架、HashMap、JVM、MySQL相关问题的示例代码,供参考学习理解使用。
Mybatis实体映射文件+接口一起的内容,代码所需导入的jar包
MyBatis关联映射代码
主要介绍了Mybatis返回int或者Integer类型报错的解决办法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
java+mybatis+mysql查询结果集的映射问题
使用MyBatis写的一对一关联映射完整代码,在eclipse上写的,使用的Mysql进行数据连接,内含数据库脚本,直接导入数据库即可建数据库和建表,不用再编写,非常方便
在之前的章节中, 我们并未对返回结果做任何关系映射,但依然工作的很好,那是因为我们的java类字段名与mysql列名是一致的。但实际情况是mysql命名规则是**login_name**, 但java中却是**loginName**, 这就引出了...
mybatis自动生成映射
个人认为,这个东西,网上的资料很多都是坑人的,我这个完成了功能
Java逆向工程,使用mybatis生成实体类跟Mapper映射,包含所需的jar包