zoukankan      html  css  js  c++  java
  • mybatis的实际应用

    简单基本的增删改查语句就不说了,直接从一对一,一对多的关系开始:

    association联合:联合元素用来处理“一对一”的关系;

    collection聚集:聚集元素用来处理“一对多”的关系;

    MyBatis 可以用两种方式加载:

    1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;
    2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

    实例:

    <resultMap id="resultMap" type="***.vo.Article">
      <id column="id" property="id" />
      <result column="user_id" property="userId" />

      //...
      <result column="create_time" property="createTime" />
      <result column="modify_time" property="modifyTime" />
      <result column="delete_time" property="deleteTime" />
      <association property="category" column="id" select="getCategory" />
      <collection property="tags" column="id" javaType="ArrayList"
        ofType="com.zhaozhi.writing.service.vo.Tag" select="getTags" />
    </resultMap>

    <resultMap id="categoryMap" type="***.vo.Category">
      <id column="id" property="id" />
      <result column="name" property="name" />

      //..
      <result column="create_time" property="createTime" />
      <result column="modify_time" property="modifyTime" />
      <result column="delete_time" property="deleteTime" />
    </resultMap>

    <resultMap id="tagMap" type="***.vo.Tag">
      <id column="id" property="id" />
      <result column="name" property="name" />
      <result column="create_time" property="createTime" />
      <result column="modify_time" property="modifyTime" />
      <result column="delete_time" property="deleteTime" />
    </resultMap>

    <select id="getCategory" resultMap="categoryMap" parameterType="java.lang.Integer">
      select c1.* from category as c1,article_categories a2,article a3 where
      c1.id=a2.category_id and c1.user_id = a3.user_id
      and a2.article_id=a3.id and a3.id=#{id} and c1.delete_time is null and
      a2.delete_time is null and a3.delete_time is null
    </select>

    <select id="getTags" resultMap="tagMap" parameterType="java.lang.Integer">
      select t1.* from tag as t1,article_tags a2,article a3 where t1.id=a2.tag_id
      and a2.article_id=a3.id and a3.id=#{id} and a2.delete_time is null
    </select>

     

  • 相关阅读:
    uview int类型数据对required校验不生效
    使用Nexus上传jar包
    centos上使用Selenium
    vue利用checkbox实现页面内容的刷新
    uview this.$u.post 数据格式不是json,报500错误
    nodejs添加某个模块后启动灰屏
    IDEA快捷命令,提高你拔刀的速度
    如何修改Linux服务器日期时间及时区
    使用exe4j生成exe文件并且附带jre运行环境
    写了一个Word和Excel读写有关的小工具,在此记录一下
  • 原文地址:https://www.cnblogs.com/yzf666/p/6373855.html
Copyright © 2011-2022 走看看