MyBatis之selectByExample与selectBy

2018-10-05  本文已影响332人  PC_Repair

mapper接口

List<Content> selectByExampleWithBLOBs(ContentExample example);
List<Content> selectByExample(ContentExample example);

mapper.xml

<select id="selectByExampleWithBLOBs" parameterType="com.ljf.blog.pojo.ContentExample" resultMap="ResultMapWithBLOBs">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from t_contents
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
</select>

<select id="selectByExample" parameterType="com.ljf.blog.pojo.ContentExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_contents
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
</select>

两段SQL查询返回的resultMap不同,一个是BaseResultMap,另一个是ResultMapWithBLOBs

<resultMap id="BaseResultMap" type="com.ljf.blog.pojo.Content">
    <id column="cid" jdbcType="INTEGER" property="cid" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="slug" jdbcType="VARCHAR" property="slug" />
    <result column="created" jdbcType="INTEGER" property="created" />
    <result column="modified" jdbcType="INTEGER" property="modified" />
    <result column="author_id" jdbcType="INTEGER" property="authorId" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="tags" jdbcType="VARCHAR" property="tags" />
    <result column="categories" jdbcType="VARCHAR" property="categories" />
    <result column="thumbImg" jdbcType="VARCHAR" property="thumbimg" />
    <result column="hits" jdbcType="INTEGER" property="hits" />
    <result column="comments_num" jdbcType="INTEGER" property="commentsNum" />
    <result column="allow_comment" jdbcType="BIT" property="allowComment" />
    <result column="allow_ping" jdbcType="BIT" property="allowPing" />
    <result column="allow_feed" jdbcType="BIT" property="allowFeed" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.ljf.blog.pojo.Content">
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
</resultMap>

文章表 t_contents

名称 类型 长度 主键 非空 描述
cid int 10 true true 主键,自增
title varchar 200 false false 文章标题
slug varchar 200 false false url地址
creted int 10 false false 创建时间
modified int 10 false false 修改时间
content text 无限制 false false 文章内容
author_id int 10 false false 作者ID
type varchar 16 false false 文章类型
status varchar 16 false false 文章状态
categories varchar 200 false false 分类
thumbImg varchar 512 false false 缩略图地址
hits int 10 false false 文章点击量
comments_num int 10 false false 评论数量
allow_comment int 1 false false 允许评论
上一篇下一篇

猜你喜欢

热点阅读