抽取公共的sql语句
在mybatis的动态sql编写中,通过<sql>标签抽取可重用的sql片段,常见于可重复利用的查询语句,实现语句复用;在使用时,通过<include refid="sql_id"></include>引入,可达到跟写在同一个查询语句中一样的效果。
<!-- =====================公共sql语句===================== -->
<!-- 用于读者移动端,根据关键字搜索(类型,图书名,作者) -->
<sql id="searchBy">
<where>
<if test="bookType!= null and bookType !=''">
and instr(book_type,#{bookType})>0
</if>
<if test="searchContent!= null and searchContent !=''">
and instr(book_name,#{searchContent})>0 or instr(book_auth,#{searchContent})>0
</if>
and book_status=0
</where>
</sql>
<!-- =====================读者移动端====================== -->
<!--按分类或关键字查找 -->
<select id="findBooksBy" resultMap="BookResultMap">
select * from t_ts_book
<include refid="searchBy"></include>
limit #{start},#{end}
</select>
mysql中INSTR函数的用法
INS