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>
- ResultMapWithBLOBs 定义时,继承了BaseResultMap,并且自己特殊的字段,该字段通常是longvarchar类型,本例中content就为特殊字段。
- content字段类型为text。故如需检索的字段中包含大字段类型时,必须用selectByExampleWithBLOBs,不检索大字段时,用selectByExample就足够了。update同样如此。
文章表 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 | 允许评论 |