1、动态SQL片段
通过SQL片段达到代码复用
<!--
动态条件分页查询 -->
< sql id ="sql_count" >
select count(*)
</ sql >
< sql id ="sql_select" >
select *
</ sql >
< sql id ="sql_where" >
from icp
< dynamic prepend ="where" >
< isNotEmpty prepend ="and" property ="name" >
name like '%$name$%'
</ isNotEmpty >
< isNotEmpty prepend ="and" property ="path" >
path like '%path$%'
</ isNotEmpty >
< isNotEmpty prepend ="and" property ="area_id" >
area_id = #area_id#
</ isNotEmpty >
< isNotEmpty prepend ="and" property ="hided" >
hided = #hided#
</ isNotEmpty >
</ dynamic >
< dynamic prepend="" >
< isNotNull property ="_start" >
< isNotNull property ="_size" >
limit #_start#, #_size#
</ isNotNull >
</ isNotNull >
</ dynamic >
</ sql >
< select id ="findByParamsForCount" parameterClass ="map" resultClass ="int" >
< include refid ="sql_count" />
< include refid ="sql_where" />
</ select >
< select id ="findByParams" parameterClass ="map" resultMap ="icp.result_base" >
< include refid ="sql_select" />
< include refid ="sql_where" />
</ select >
< sql id ="sql_count" >
select count(*)
</ sql >
< sql id ="sql_select" >
select *
</ sql >
< sql id ="sql_where" >
from icp
< dynamic prepend ="where" >
< isNotEmpty prepend ="and" property ="name" >
name like '%$name$%'
</ isNotEmpty >
< isNotEmpty prepend ="and" property ="path" >
path like '%path$%'
</ isNotEmpty >
< isNotEmpty prepend ="and" property ="area_id" >
area_id = #area_id#
</ isNotEmpty >
< isNotEmpty prepend ="and" property ="hided" >
hided = #hided#
</ isNotEmpty >
</ dynamic >
< dynamic prepend="" >
< isNotNull property ="_start" >
< isNotNull property ="_size" >
limit #_start#, #_size#
</ isNotNull >
</ isNotNull >
</ dynamic >
</ sql >
< select id ="findByParamsForCount" parameterClass ="map" resultClass ="int" >
< include refid ="sql_count" />
< include refid ="sql_where" />
</ select >
< select id ="findByParams" parameterClass ="map" resultMap ="icp.result_base" >
< include refid ="sql_select" />
< include refid ="sql_where" />
</ select >
2、数字范围查询
所传参数名称是捏造所得,非数据库字段,比如_img_size_ge、_img_size_lt字段
<
isNotEmpty
prepend
="and"
property
="_img_size_ge"